Skip to content

Commit

Permalink
fix: Import excel relative bugs (#676)
Browse files Browse the repository at this point in the history
* fix: import generated repeated dbFieldName error

* feat: excel import support chunk
  • Loading branch information
caoxing9 authored Jun 26, 2024
1 parent 1554b3d commit 13b4463
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
18 changes: 14 additions & 4 deletions apps/nestjs-backend/src/features/import/open-api/import.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BadRequestException } from '@nestjs/common';
import { getUniqName, FieldType } from '@teable/core';
import type { IValidateTypes, IAnalyzeVo } from '@teable/openapi';
import { SUPPORTEDTYPE, importTypeMap } from '@teable/openapi';
import { zip, toString, intersection } from 'lodash';
import { zip, toString, intersection, chunk as chunkArray } from 'lodash';
import fetch from 'node-fetch';
import sizeof from 'object-sizeof';
import Papa from 'papaparse';
Expand Down Expand Up @@ -317,10 +317,20 @@ export class ExcelImporter extends Importer {

if (options && chunk) {
const { skipFirstNLines, key } = options;
if (skipFirstNLines) {
parseResult[key].splice(0, 1);

const parseResults = chunkArray(parseResult[key], Importer.MAX_CHUNK_LENGTH);

for (let i = 0; i < parseResults.length; i++) {
const currentChunk = parseResults[i];
if (i === 0 && skipFirstNLines) {
currentChunk.splice(0, 1);
}
try {
await chunk({ [key]: currentChunk });
} catch (e) {
onError?.((e as Error)?.message || Importer.DEFAULT_ERROR_MESSAGE);
}
}
await chunk(parseResult);
onFinished?.();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import type {
ITableVo,
IUpdateOrderRo,
} from '@teable/openapi';
import { nanoid } from 'nanoid';
import { ThresholdConfig, IThresholdConfig } from '../../../configs/threshold.config';
import { InjectDbProvider } from '../../../db-provider/db.provider';
import { IDbProvider } from '../../../db-provider/db.provider.interface';
Expand Down Expand Up @@ -117,7 +118,22 @@ export class TableOpenApiService {
)
);
}
return fields;

const repeatedDbFieldNames = fields
.map((f) => f.dbFieldName)
.filter((value, index, self) => self.indexOf(value) !== index);

// generator dbFieldName may repeat, this is fix it.
return fields.map((f) => {
const newField = { ...f };
const { dbFieldName } = newField;

if (repeatedDbFieldNames.includes(dbFieldName)) {
newField.dbFieldName = `${dbFieldName}_${nanoid(3)}`;
}

return newField;
});
}

async createTable(baseId: string, tableRo: ICreateTableWithDefault): Promise<ITableFullVo> {
Expand Down

0 comments on commit 13b4463

Please sign in to comment.