fix(import): filter non-existent fields

This commit is contained in:
chenos 2023-10-31 12:11:15 +08:00
parent c3a1538435
commit 2008bfa54a
2 changed files with 16 additions and 12 deletions

View File

@ -5,6 +5,7 @@ import { EventEmitter } from 'events';
import { backOff } from 'exponential-backoff'; import { backOff } from 'exponential-backoff';
import glob from 'glob'; import glob from 'glob';
import lodash from 'lodash'; import lodash from 'lodash';
import { nanoid } from 'nanoid';
import { basename, isAbsolute, resolve } from 'path'; import { basename, isAbsolute, resolve } from 'path';
import semver from 'semver'; import semver from 'semver';
import { import {
@ -40,6 +41,7 @@ import QueryInterface from './query-interface/query-interface';
import buildQueryInterface from './query-interface/query-interface-builder'; import buildQueryInterface from './query-interface/query-interface-builder';
import { RelationRepository } from './relation-repository/relation-repository'; import { RelationRepository } from './relation-repository/relation-repository';
import { Repository } from './repository'; import { Repository } from './repository';
import { SqlCollection } from './sql-collection/sql-collection';
import { import {
AfterDefineCollectionListener, AfterDefineCollectionListener,
BeforeDefineCollectionListener, BeforeDefineCollectionListener,
@ -70,8 +72,6 @@ import {
import { patchSequelizeQueryInterface, snakeCase } from './utils'; import { patchSequelizeQueryInterface, snakeCase } from './utils';
import { BaseValueParser, registerFieldValueParsers } from './value-parsers'; import { BaseValueParser, registerFieldValueParsers } from './value-parsers';
import { ViewCollection } from './view-collection'; import { ViewCollection } from './view-collection';
import { SqlCollection } from './sql-collection/sql-collection';
import { nanoid } from 'nanoid';
export type MergeOptions = merge.Options; export type MergeOptions = merge.Options;
@ -614,9 +614,10 @@ export class Database extends EventEmitter implements AsyncEmitter {
} }
buildFieldValueParser<T extends BaseValueParser>(field: Field, ctx: any) { buildFieldValueParser<T extends BaseValueParser>(field: Field, ctx: any) {
const Parser = this.fieldValueParsers.has(field.type) const Parser =
? this.fieldValueParsers.get(field.type) field && this.fieldValueParsers.has(field.type)
: this.fieldValueParsers.get('default'); ? this.fieldValueParsers.get(field.type)
: this.fieldValueParsers.get('default');
const parser = new Parser(field, ctx); const parser = new Parser(field, ctx);
return parser as T; return parser as T;
} }

View File

@ -1,5 +1,6 @@
import { Context, Next } from '@nocobase/actions'; import { Context, Next } from '@nocobase/actions';
import { Collection, Repository } from '@nocobase/database'; import { Collection, Repository } from '@nocobase/database';
import { uid } from '@nocobase/utils';
import xlsx from 'node-xlsx'; import xlsx from 'node-xlsx';
import XLSX from 'xlsx'; import XLSX from 'xlsx';
import { namespace } from '../../'; import { namespace } from '../../';
@ -39,12 +40,14 @@ class Importer {
if (typeof columns === 'string') { if (typeof columns === 'string') {
columns = JSON.parse(columns); columns = JSON.parse(columns);
} }
this.columns = columns.map((column) => { this.columns = columns
return { .map((column) => {
...column, return {
field: this.collection.fields.get(column.dataIndex[0]), ...column,
}; field: this.collection.fields.get(column.dataIndex[0]),
}); };
})
.filter((col) => col.field);
const str = this.columns.map((column) => column.defaultTitle).join('||'); const str = this.columns.map((column) => column.defaultTitle).join('||');
for (const row of rows) { for (const row of rows) {
if (this.hasHeaderRow()) { if (this.hasHeaderRow()) {
@ -152,7 +155,7 @@ export async function importXlsx(ctx: Context, next: Next) {
ctx.body = { ctx.body = {
rows: xlsx.build([ rows: xlsx.build([
{ {
name: ctx.file.originalname, name: `${uid()}.xlsx`,
data: [importer.headerRow].concat(failure), data: [importer.headerRow].concat(failure),
}, },
]), ]),