From aa2117f654ebdd4faa03907c8553e226ad6ecdee Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Fri, 23 Feb 2024 15:12:57 +0800 Subject: [PATCH] fix: lazy loading belongs to association (#3559) * chore: lazy loading belongs to association * chore: test * chore: console.log --- .../server/__tests__/fields/belongsTo.test.ts | 57 +++++++++++++++---- .../repositories/collection-repository.ts | 7 +-- 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/packages/plugins/@nocobase/plugin-collection-manager/src/server/__tests__/fields/belongsTo.test.ts b/packages/plugins/@nocobase/plugin-collection-manager/src/server/__tests__/fields/belongsTo.test.ts index 1786056811..66a61afb6c 100644 --- a/packages/plugins/@nocobase/plugin-collection-manager/src/server/__tests__/fields/belongsTo.test.ts +++ b/packages/plugins/@nocobase/plugin-collection-manager/src/server/__tests__/fields/belongsTo.test.ts @@ -1,6 +1,7 @@ import Database, { Collection as DBCollection } from '@nocobase/database'; import Application from '@nocobase/server'; import { createApp } from '..'; +import { CollectionRepository } from '../../index'; describe('belongsTo', () => { let db: Database; @@ -13,23 +14,55 @@ describe('belongsTo', () => { db = app.db; Collection = db.getCollection('collections'); Field = db.getCollection('fields'); - await Collection.repository.create({ - values: { - name: 'tests', - }, - }); - await Collection.repository.create({ - values: { - name: 'foos', - }, - }); }); afterEach(async () => { await app.destroy(); }); - it('a', () => { - expect(true).toBe(true); + it('should load belongsTo field', async () => { + await Collection.repository.create({ + values: { + name: 'orders', + fields: [ + { + type: 'integer', + name: 'amount', + }, + { + type: 'belongsTo', + name: 'users', + targetKey: 'uid', + foreignKey: 'userId', + }, + ], + }, + }); + + await Collection.repository.create({ + values: { + name: 'users', + fields: [ + { + type: 'string', + name: 'name', + }, + { + type: 'string', + name: 'uid', + }, + ], + }, + }); + + let error; + + try { + await db.getRepository('collections').load(); + } catch (e) { + error = e; + } + + expect(error).toBeUndefined(); }); }); 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 3dd4626e76..962c8d9b08 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 @@ -78,17 +78,14 @@ export class CollectionRepository extends Repository { const fields = nameMap[instanceName].get('fields'); return fields - .filter( - (field) => - (field['type'] === 'belongsTo' && viewCollections.includes(field.options?.['target'])) || - field['type'] === 'belongsToMany', - ) + .filter((field) => field['type'] === 'belongsTo' || field['type'] === 'belongsToMany') .map((field) => field.get('name')); })(); if (lodash.isArray(skipField) && skipField.length) { lazyCollectionFields.set(instanceName, skipField); } + this.database.logger.debug(`load collection`, { instanceName, submodule: 'CollectionRepository',