fix: lazy loading belongs to association (#3559)

* chore: lazy loading belongs to association

* chore: test

* chore: console.log
This commit is contained in:
ChengLei Shao 2024-02-23 15:12:57 +08:00 committed by GitHub
parent 6262409184
commit aa2117f654
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 47 additions and 17 deletions

View File

@ -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<CollectionRepository>('collections').load();
} catch (e) {
error = e;
}
expect(error).toBeUndefined();
});
});

View File

@ -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',