From 83bca97db0030b476d61c34c59b6c56f179e29b9 Mon Sep 17 00:00:00 2001 From: chenos Date: Wed, 15 Feb 2023 11:37:19 +0800 Subject: [PATCH] fix: optimize import performance --- .../src/server/hooks/after-create.ts | 3 +++ .../20221121111110-update-fk-type.ts | 4 ++++ .../20221121111113-update-id-to-bigint.ts | 4 ++++ .../import/src/server/actions/importXlsx.ts | 18 ++++++++++++++---- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/packages/plugins/audit-logs/src/server/hooks/after-create.ts b/packages/plugins/audit-logs/src/server/hooks/after-create.ts index 4776ca534..0cafdfad9 100644 --- a/packages/plugins/audit-logs/src/server/hooks/after-create.ts +++ b/packages/plugins/audit-logs/src/server/hooks/after-create.ts @@ -3,6 +3,9 @@ import { LOG_TYPE_CREATE } from '../constants'; export function afterCreate(app: Application) { return async (model, options) => { + if (options.logging === false) { + return; + } const db = app.db; const collection = db.getCollection(model.constructor.name); if (!collection || !collection.options.logging) { diff --git a/packages/plugins/collection-manager/src/migrations/20221121111110-update-fk-type.ts b/packages/plugins/collection-manager/src/migrations/20221121111110-update-fk-type.ts index 39a299a73..97810a7cb 100644 --- a/packages/plugins/collection-manager/src/migrations/20221121111110-update-fk-type.ts +++ b/packages/plugins/collection-manager/src/migrations/20221121111110-update-fk-type.ts @@ -3,6 +3,10 @@ import { Migration } from '@nocobase/server'; export default class UpdateIdToBigIntMigrator extends Migration { async up() { + const result = await this.app.version.satisfies('<0.9.0-alpha.1'); + if (!result) { + return; + } const db = this.app.db; await db.getCollection('fields').repository.update({ diff --git a/packages/plugins/collection-manager/src/migrations/20221121111113-update-id-to-bigint.ts b/packages/plugins/collection-manager/src/migrations/20221121111113-update-id-to-bigint.ts index 48e7cabbe..0247a30e6 100644 --- a/packages/plugins/collection-manager/src/migrations/20221121111113-update-id-to-bigint.ts +++ b/packages/plugins/collection-manager/src/migrations/20221121111113-update-id-to-bigint.ts @@ -3,6 +3,10 @@ import { Migration } from '@nocobase/server'; export default class UpdateIdToBigIntMigrator extends Migration { async up() { + const result = await this.app.version.satisfies('<0.9.0-alpha.1'); + if (!result) { + return; + } const db = this.app.db; await db.getCollection('fields').repository.update({ diff --git a/packages/plugins/import/src/server/actions/importXlsx.ts b/packages/plugins/import/src/server/actions/importXlsx.ts index a1290925a..91a45086d 100644 --- a/packages/plugins/import/src/server/actions/importXlsx.ts +++ b/packages/plugins/import/src/server/actions/importXlsx.ts @@ -23,7 +23,7 @@ export async function importXlsx(ctx: Context, next: Next) { } = xlsx.parse(file.buffer); const failureData = originalList.splice(IMPORT_LIMIT_COUNT + 1); const titles = originalList.shift(); - const legalList = []; + const legalList: any[] = []; if (originalList.length > 0 && titles?.length === columns.length) { // const results = ( // await Promise.allSettled( @@ -38,12 +38,12 @@ export async function importXlsx(ctx: Context, next: Next) { // }), // ) // ).filter((item) => 'value' in item && item.value !== undefined); - const values = []; + const values: any[] = []; for (const item of originalList) { try { const transformResult = await transform({ ctx, record: item, columns, fields: collectionFields }); values.push(transformResult); - legalList.push(cloneDeep(item)); + legalList.push(cloneDeep(item)); } catch (error) { failureData.unshift([...item, error.message]); } @@ -51,14 +51,24 @@ export async function importXlsx(ctx: Context, next: Next) { //@ts-ignore // const values = results.map((r) => r.value); const result = await ctx.db.sequelize.transaction(async (transaction) => { + let sort: number = 0; + if (collection.options.sortable) { + sort = await repository.model.max('sort', { transaction }); + } for (const [index, val] of values.entries()) { if (val === undefined || val === null) { continue; } try { + let values = { ...val }; + if (collection.options.sortable) { + sort += 1; + values['sort'] = sort; + } await repository.create({ - values: { ...val }, + values, transaction, + logging: false, }); } catch (error) { const failData = legalList[index];