From 2008bfa54ad06bacbbdb61aca3b09778662fb437 Mon Sep 17 00:00:00 2001 From: chenos Date: Tue, 31 Oct 2023 12:11:15 +0800 Subject: [PATCH] fix(import): filter non-existent fields --- packages/core/database/src/database.ts | 11 ++++++----- .../src/server/actions/importXlsx.ts | 17 ++++++++++------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/packages/core/database/src/database.ts b/packages/core/database/src/database.ts index ac1c98e55..00bf9c620 100644 --- a/packages/core/database/src/database.ts +++ b/packages/core/database/src/database.ts @@ -5,6 +5,7 @@ import { EventEmitter } from 'events'; import { backOff } from 'exponential-backoff'; import glob from 'glob'; import lodash from 'lodash'; +import { nanoid } from 'nanoid'; import { basename, isAbsolute, resolve } from 'path'; import semver from 'semver'; import { @@ -40,6 +41,7 @@ import QueryInterface from './query-interface/query-interface'; import buildQueryInterface from './query-interface/query-interface-builder'; import { RelationRepository } from './relation-repository/relation-repository'; import { Repository } from './repository'; +import { SqlCollection } from './sql-collection/sql-collection'; import { AfterDefineCollectionListener, BeforeDefineCollectionListener, @@ -70,8 +72,6 @@ import { import { patchSequelizeQueryInterface, snakeCase } from './utils'; import { BaseValueParser, registerFieldValueParsers } from './value-parsers'; import { ViewCollection } from './view-collection'; -import { SqlCollection } from './sql-collection/sql-collection'; -import { nanoid } from 'nanoid'; export type MergeOptions = merge.Options; @@ -614,9 +614,10 @@ export class Database extends EventEmitter implements AsyncEmitter { } buildFieldValueParser(field: Field, ctx: any) { - const Parser = this.fieldValueParsers.has(field.type) - ? this.fieldValueParsers.get(field.type) - : this.fieldValueParsers.get('default'); + const Parser = + field && this.fieldValueParsers.has(field.type) + ? this.fieldValueParsers.get(field.type) + : this.fieldValueParsers.get('default'); const parser = new Parser(field, ctx); return parser as T; } diff --git a/packages/plugins/@nocobase/plugin-import/src/server/actions/importXlsx.ts b/packages/plugins/@nocobase/plugin-import/src/server/actions/importXlsx.ts index e2a779041..4a258e71f 100644 --- a/packages/plugins/@nocobase/plugin-import/src/server/actions/importXlsx.ts +++ b/packages/plugins/@nocobase/plugin-import/src/server/actions/importXlsx.ts @@ -1,5 +1,6 @@ import { Context, Next } from '@nocobase/actions'; import { Collection, Repository } from '@nocobase/database'; +import { uid } from '@nocobase/utils'; import xlsx from 'node-xlsx'; import XLSX from 'xlsx'; import { namespace } from '../../'; @@ -39,12 +40,14 @@ class Importer { if (typeof columns === 'string') { columns = JSON.parse(columns); } - this.columns = columns.map((column) => { - return { - ...column, - field: this.collection.fields.get(column.dataIndex[0]), - }; - }); + this.columns = columns + .map((column) => { + return { + ...column, + field: this.collection.fields.get(column.dataIndex[0]), + }; + }) + .filter((col) => col.field); const str = this.columns.map((column) => column.defaultTitle).join('||'); for (const row of rows) { if (this.hasHeaderRow()) { @@ -152,7 +155,7 @@ export async function importXlsx(ctx: Context, next: Next) { ctx.body = { rows: xlsx.build([ { - name: ctx.file.originalname, + name: `${uid()}.xlsx`, data: [importer.headerRow].concat(failure), }, ]),