From ec92727a208982e85f514a2ff5a98f74c767f0aa Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Thu, 9 Jan 2025 09:19:02 +0800 Subject: [PATCH] fix: import with associations (#6024) * fix: import with associations * chore: console.log --- .../src/interfaces/to-many-interface.ts | 16 +++- .../server/__tests__/xlsx-importer.test.ts | 80 +++++++++++++++++++ 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/packages/core/database/src/interfaces/to-many-interface.ts b/packages/core/database/src/interfaces/to-many-interface.ts index 8b6aef65ad..06b7f8669c 100644 --- a/packages/core/database/src/interfaces/to-many-interface.ts +++ b/packages/core/database/src/interfaces/to-many-interface.ts @@ -19,7 +19,7 @@ export class ToManyInterface extends BaseInterface { const items = str.split(','); - const { filterKey, targetCollection, transaction } = ctx; + const { filterKey, targetCollection, transaction, field } = ctx; const targetInstances = await targetCollection.repository.find({ filter: { @@ -36,7 +36,19 @@ export class ToManyInterface extends BaseInterface { }); const primaryKeyAttribute = targetCollection.model.primaryKeyAttribute; + const targetKey = field.options.targetKey; - return targetInstances.map((targetInstance) => targetInstance[primaryKeyAttribute]); + const values = targetInstances.map((targetInstance) => { + const result = { + [targetKey]: targetInstance[targetKey], + }; + + if (targetKey !== primaryKeyAttribute) { + result[primaryKeyAttribute] = targetInstance[primaryKeyAttribute]; + } + + return result; + }); + return values; } } diff --git a/packages/plugins/@nocobase/plugin-action-import/src/server/__tests__/xlsx-importer.test.ts b/packages/plugins/@nocobase/plugin-action-import/src/server/__tests__/xlsx-importer.test.ts index d5bb10dd9b..f4a3de2f6e 100644 --- a/packages/plugins/@nocobase/plugin-action-import/src/server/__tests__/xlsx-importer.test.ts +++ b/packages/plugins/@nocobase/plugin-action-import/src/server/__tests__/xlsx-importer.test.ts @@ -1739,4 +1739,84 @@ describe('xlsx importer', () => { expect(await User.repository.count()).toBe(0); expect(error).toBeTruthy(); }); + + it('should import with associations by target key', async () => { + const Post = app.db.collection({ + name: 'posts', + fields: [ + { + name: 'title', + type: 'string', + }, + { + name: 'tags', + type: 'belongsToMany', + through: 'post_tag', + targetKey: 'name', + interface: 'm2m', + }, + ], + }); + + const Tag = app.db.collection({ + name: 'tags', + fields: [ + { + name: 'name', + type: 'string', + unique: true, + }, + ], + }); + + await app.db.sync(); + + await Tag.repository.create({ + values: { + name: 't1', + }, + }); + + const templateCreator = new TemplateCreator({ + collection: Post, + columns: [ + { + dataIndex: ['title'], + defaultTitle: '标题', + }, + { + dataIndex: ['tags', 'name'], + defaultTitle: 'TagsName', + }, + ], + }); + + const template = await templateCreator.run(); + + const worksheet = template.Sheets[template.SheetNames[0]]; + + XLSX.utils.sheet_add_aoa(worksheet, [['Post1', 't1']], { + origin: 'A2', + }); + + const importer = new XlsxImporter({ + collectionManager: app.mainDataSource.collectionManager, + collection: Post, + columns: [ + { + dataIndex: ['title'], + defaultTitle: '标题', + }, + { + dataIndex: ['tags', 'name'], + defaultTitle: 'TagsName', + }, + ], + workbook: template, + }); + + await importer.run(); + + expect(await Post.repository.count()).toBe(1); + }); });