mirror of
https://gitee.com/nocobase/nocobase.git
synced 2024-12-05 05:38:23 +08:00
fix: eager load with belongs to many with custom source key (#1913)
This commit is contained in:
parent
3b732d6db7
commit
6fb569cf0d
@ -82,6 +82,11 @@ export class EagerLoadingTree {
|
|||||||
pushAttribute(child, targetKey);
|
pushAttribute(child, targetKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (associationType == 'BelongsToMany') {
|
||||||
|
const { sourceKey } = association;
|
||||||
|
pushAttribute(eagerLoadingTreeParent, sourceKey);
|
||||||
|
}
|
||||||
|
|
||||||
eagerLoadingTreeParent.children.push(child);
|
eagerLoadingTreeParent.children.push(child);
|
||||||
|
|
||||||
if (include.include) {
|
if (include.include) {
|
||||||
@ -150,13 +155,16 @@ export class EagerLoadingTree {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (associationType == 'BelongsToMany') {
|
if (associationType == 'BelongsToMany') {
|
||||||
|
const foreignKeyValues = node.parent.instances.map((instance) => instance.get(association.sourceKey));
|
||||||
|
|
||||||
instances = await node.model.findAll({
|
instances = await node.model.findAll({
|
||||||
transaction,
|
transaction,
|
||||||
|
attributes: node.attributes,
|
||||||
include: [
|
include: [
|
||||||
{
|
{
|
||||||
association: association.oneFromTarget,
|
association: association.oneFromTarget,
|
||||||
where: {
|
where: {
|
||||||
[association.foreignKey]: ids,
|
[association.foreignKey]: foreignKeyValues,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
@ -0,0 +1,69 @@
|
|||||||
|
import Database, { Collection as DBCollection } from '@nocobase/database';
|
||||||
|
import Application from '@nocobase/server';
|
||||||
|
import { createApp } from './index';
|
||||||
|
|
||||||
|
describe('Collection categories', () => {
|
||||||
|
let db: Database;
|
||||||
|
let app: Application;
|
||||||
|
let Collection: DBCollection;
|
||||||
|
let Field: DBCollection;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
app = await createApp({
|
||||||
|
database: { tablePrefix: '' },
|
||||||
|
});
|
||||||
|
db = app.db;
|
||||||
|
Collection = db.getCollection('collections');
|
||||||
|
Field = db.getCollection('fields');
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
await app.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create collection categories', async () => {
|
||||||
|
const category = await db.getRepository('collectionCategories').create({
|
||||||
|
values: {
|
||||||
|
color: 'green',
|
||||||
|
name: 'test',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const category2 = await db.getRepository('collectionCategories').create({
|
||||||
|
values: {
|
||||||
|
color: 'yellow',
|
||||||
|
name: 'test2',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const category3 = await db.getRepository('collectionCategories').create({
|
||||||
|
values: {
|
||||||
|
color: 'red',
|
||||||
|
name: 'test3',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await db.getRepository('collections').create({
|
||||||
|
values: {
|
||||||
|
name: 'testCollection',
|
||||||
|
category: [category.get('id'), category2.get('id')],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await db.getRepository('collections').create({
|
||||||
|
values: {
|
||||||
|
name: 'testCollection2',
|
||||||
|
category: [category3.get('id'), category2.get('id')],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const list = await db.getRepository('collections').find({
|
||||||
|
fields: ['category.name', 'key'],
|
||||||
|
filter: {
|
||||||
|
name: 'testCollection',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(list[0].get('category').length).toBe(2);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user