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 Database, { Collection as DBCollection } from '@nocobase/database';
import Application from '@nocobase/server'; import Application from '@nocobase/server';
import { createApp } from '..'; import { createApp } from '..';
import { CollectionRepository } from '../../index';
describe('belongsTo', () => { describe('belongsTo', () => {
let db: Database; let db: Database;
@ -13,23 +14,55 @@ describe('belongsTo', () => {
db = app.db; db = app.db;
Collection = db.getCollection('collections'); Collection = db.getCollection('collections');
Field = db.getCollection('fields'); Field = db.getCollection('fields');
await Collection.repository.create({
values: {
name: 'tests',
},
});
await Collection.repository.create({
values: {
name: 'foos',
},
});
}); });
afterEach(async () => { afterEach(async () => {
await app.destroy(); await app.destroy();
}); });
it('a', () => { it('should load belongsTo field', async () => {
expect(true).toBe(true); 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'); const fields = nameMap[instanceName].get('fields');
return fields return fields
.filter( .filter((field) => field['type'] === 'belongsTo' || field['type'] === 'belongsToMany')
(field) =>
(field['type'] === 'belongsTo' && viewCollections.includes(field.options?.['target'])) ||
field['type'] === 'belongsToMany',
)
.map((field) => field.get('name')); .map((field) => field.get('name'));
})(); })();
if (lodash.isArray(skipField) && skipField.length) { if (lodash.isArray(skipField) && skipField.length) {
lazyCollectionFields.set(instanceName, skipField); lazyCollectionFields.set(instanceName, skipField);
} }
this.database.logger.debug(`load collection`, { this.database.logger.debug(`load collection`, {
instanceName, instanceName,
submodule: 'CollectionRepository', submodule: 'CollectionRepository',