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 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();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -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',
|
||||||
|
Loading…
Reference in New Issue
Block a user