diff --git a/packages/plugins/@nocobase/plugin-collection-manager/src/server/__tests__/collections.repository.test.ts b/packages/plugins/@nocobase/plugin-collection-manager/src/server/__tests__/collections.repository.test.ts index 1b15f6c22..6c7372bc5 100644 --- a/packages/plugins/@nocobase/plugin-collection-manager/src/server/__tests__/collections.repository.test.ts +++ b/packages/plugins/@nocobase/plugin-collection-manager/src/server/__tests__/collections.repository.test.ts @@ -20,6 +20,106 @@ describe('collections repository', () => { await app.destroy(); }); + it('should load through table with foreignKey', async () => { + await Collection.repository.create({ + values: { + name: 'postsTags', + autoGenId: false, + fields: [ + { + type: 'integer', + name: 'postId', + primaryKey: true, + }, + { + type: 'integer', + name: 'tagId', + primaryKey: true, + }, + { + type: 'boolean', + name: 'default', + }, + ], + }, + context: {}, + }); + + await Collection.repository.create({ + values: { + name: 'posts', + fields: [ + { + type: 'string', + name: 'title', + }, + { + type: 'belongsToMany', + target: 'tags', + name: 'tags', + through: 'postsTags', + foreignKey: 'postId', + otherKey: 'tagId', + sourceKey: 'id', + targetKey: 'id', + }, + ], + }, + context: {}, + }); + + await Collection.repository.create({ + values: { + name: 'tags', + fields: [ + { + type: 'string', + name: 'name', + }, + { + type: 'belongsToMany', + target: 'posts', + name: 'posts', + through: 'postsTags', + foreignKey: 'tagId', + otherKey: 'postId', + sourceKey: 'id', + targetKey: 'id', + }, + ], + }, + context: {}, + }); + + const postsCollection = db.getCollection('posts'); + + const p1 = await postsCollection.repository.create({ + values: { + title: 'test', + tags: [ + { + name: 't1', + }, + { + name: 't2', + }, + ], + }, + }); + + const throughCollection = db.getCollection('postsTags'); + console.log(throughCollection.model.primaryKeyAttributes); + + await throughCollection.repository.update({ + filter: { + postId: p1.get('id'), + }, + values: { + default: true, + }, + }); + }); + it('should create collection with sortable option', async () => { await Collection.repository.create({ values: { diff --git a/packages/plugins/@nocobase/plugin-collection-manager/src/server/repositories/collection-repository.ts b/packages/plugins/@nocobase/plugin-collection-manager/src/server/repositories/collection-repository.ts index fb17f96b0..eea623a8e 100644 --- a/packages/plugins/@nocobase/plugin-collection-manager/src/server/repositories/collection-repository.ts +++ b/packages/plugins/@nocobase/plugin-collection-manager/src/server/repositories/collection-repository.ts @@ -62,14 +62,13 @@ export class CollectionRepository extends Repository { // sort graph nodes const sortedNames = graphlib.alg.topsort(graph); - const lazyCollectionFields: { - [key: string]: Array; - } = {}; + const lazyCollectionFields = new Map>(); for (const instanceName of sortedNames) { if (!nameMap[instanceName]) continue; // skip load collection field + // can be a true value or an array of field names const skipField = (() => { // skip load collection field if collection is view if (viewCollections.includes(instanceName)) { @@ -88,7 +87,7 @@ export class CollectionRepository extends Repository { })(); if (lodash.isArray(skipField) && skipField.length) { - lazyCollectionFields[instanceName] = skipField; + lazyCollectionFields.set(instanceName, skipField); } this.database.logger.debug(`load ${instanceName} collection`); @@ -105,7 +104,7 @@ export class CollectionRepository extends Repository { } // load lazy collection field - for (const [collectionName, skipField] of Object.entries(lazyCollectionFields)) { + for (const [collectionName, skipField] of lazyCollectionFields) { this.database.logger.debug(`load ${collectionName} collection fields`); this.app.setMaintainingMessage(`load ${collectionName} collection fields`); await nameMap[collectionName].loadFields({ includeFields: skipField });