diff --git a/packages/core/database/src/interfaces/to-many-interface.ts b/packages/core/database/src/interfaces/to-many-interface.ts index 664ff692f7..072be9b877 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 f8f0d0beff..64b9d9dce0 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 = [ {