From 97b63b9b90941f4d5a126de2bb22be00e91942b3 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Sun, 10 Nov 2024 10:05:53 +0800 Subject: [PATCH 1/2] fix: import with m2m id field (#5623) --- .../src/interfaces/to-many-interface.ts | 5 +- .../server/__tests__/xlsx-importer.test.ts | 88 +++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/packages/core/database/src/interfaces/to-many-interface.ts b/packages/core/database/src/interfaces/to-many-interface.ts index 664ff692f..072be9b87 100644 --- a/packages/core/database/src/interfaces/to-many-interface.ts +++ b/packages/core/database/src/interfaces/to-many-interface.ts @@ -15,10 +15,13 @@ export class ToManyInterface extends BaseInterface { return null; } + str = `${str}`.trim(); + const items = str.split(','); const { filterKey, targetCollection, transaction } = ctx; + console.log({ filterKey }); const targetInstances = await targetCollection.repository.find({ filter: { [filterKey]: items, @@ -28,7 +31,7 @@ export class ToManyInterface extends BaseInterface { // check if all items are found items.forEach((item) => { - if (!targetInstances.find((targetInstance) => targetInstance[filterKey] === item)) { + if (!targetInstances.find((targetInstance) => targetInstance[filterKey] == item)) { throw new Error(`"${item}" not found in ${targetCollection.model.name} ${filterKey}`); } }); 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 f8f0d0bef..64b9d9dce 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 @@ -429,6 +429,8 @@ describe('xlsx importer', () => { describe('import with associations', () => { let User; let Post; + let Tag; + beforeEach(async () => { User = app.db.collection({ name: 'users', @@ -460,12 +462,98 @@ describe('xlsx importer', () => { target: 'users', interface: 'm2o', }, + { + type: 'belongsToMany', + name: 'tags', + target: 'tags', + interface: 'm2m', + through: 'postsTags', + }, + ], + }); + + Tag = app.db.collection({ + name: 'tags', + fields: [ + { + type: 'string', + name: 'name', + }, + { + type: 'belongsToMany', + name: 'posts', + target: 'posts', + interface: 'm2m', + through: 'postsTags', + }, ], }); await app.db.sync(); }); + it('should import many to many with id', async () => { + await Tag.repository.create({ + values: [ + { + title: 't1', + }, + { + title: 't2', + }, + ], + }); + + const columns = [ + { + dataIndex: ['title'], + defaultTitle: '名称', + }, + { + dataIndex: ['tags', 'id'], + defaultTitle: 'IDS', + }, + ]; + + const templateCreator = new TemplateCreator({ + collection: Post, + columns, + }); + + const template = await templateCreator.run(); + + const worksheet = template.Sheets[template.SheetNames[0]]; + + XLSX.utils.sheet_add_aoa( + worksheet, + [ + ['test', '1,2'], + ['test2', 1], + ], + { + origin: 'A2', + }, + ); + + const importer = new XlsxImporter({ + collectionManager: app.mainDataSource.collectionManager, + collection: Post, + columns, + workbook: template, + }); + + await importer.run(); + + const posts = await Post.repository.find({ + appends: ['tags'], + }); + + expect(posts.length).toBe(2); + + expect(posts[0]['tags'].map((item: any) => item.id)).toEqual([1, 2]); + expect(posts[1]['tags'].map((item: any) => item.id)).toEqual([1]); + }); + it('should validate to many association', async () => { const columns = [ { From 4441602149067cf14dff1534bb726b2609cecc0d Mon Sep 17 00:00:00 2001 From: Chareice Date: Sun, 10 Nov 2024 10:08:54 +0800 Subject: [PATCH 2/2] chore: console.log --- packages/core/database/src/interfaces/to-many-interface.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/core/database/src/interfaces/to-many-interface.ts b/packages/core/database/src/interfaces/to-many-interface.ts index 072be9b87..8b6aef65a 100644 --- a/packages/core/database/src/interfaces/to-many-interface.ts +++ b/packages/core/database/src/interfaces/to-many-interface.ts @@ -21,7 +21,6 @@ export class ToManyInterface extends BaseInterface { const { filterKey, targetCollection, transaction } = ctx; - console.log({ filterKey }); const targetInstances = await targetCollection.repository.find({ filter: { [filterKey]: items,