fix: lazy loading belongs to association (#3559)
* chore: lazy loading belongs to association * chore: test * chore: console.log
This commit is contained in:
parent
6262409184
commit
aa2117f654
@ -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();
|
||||
});
|
||||
});
|
||||
|
@ -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',
|
||||
|
Loading…
Reference in New Issue
Block a user