fix: appends belongs to association (#1893)
* fix: merge stage in eager loading tree * chore: test
This commit is contained in:
parent
cb2feb304d
commit
ccdc05b30b
@ -54,6 +54,7 @@ describe('repository.find', () => {
|
|||||||
let User: Collection;
|
let User: Collection;
|
||||||
let Post: Collection;
|
let Post: Collection;
|
||||||
let Comment: Collection;
|
let Comment: Collection;
|
||||||
|
let Tag: Collection;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
db = mockDatabase();
|
db = mockDatabase();
|
||||||
@ -70,8 +71,18 @@ describe('repository.find', () => {
|
|||||||
{ type: 'string', name: 'name' },
|
{ type: 'string', name: 'name' },
|
||||||
{ type: 'belongsTo', name: 'user' },
|
{ type: 'belongsTo', name: 'user' },
|
||||||
{ type: 'hasMany', name: 'comments' },
|
{ type: 'hasMany', name: 'comments' },
|
||||||
|
{ type: 'belongsToMany', name: 'tags' },
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Tag = db.collection({
|
||||||
|
name: 'tags',
|
||||||
|
fields: [
|
||||||
|
{ type: 'string', name: 'name' },
|
||||||
|
{ type: 'belongsToMany', name: 'posts' },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
Comment = db.collection({
|
Comment = db.collection({
|
||||||
name: 'comments',
|
name: 'comments',
|
||||||
fields: [
|
fields: [
|
||||||
@ -80,6 +91,10 @@ describe('repository.find', () => {
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
await db.sync();
|
await db.sync();
|
||||||
|
|
||||||
|
const tags = await Tag.repository.create({
|
||||||
|
values: [{ name: 't1' }, { name: 't2' }],
|
||||||
|
});
|
||||||
await User.repository.createMany({
|
await User.repository.createMany({
|
||||||
records: [
|
records: [
|
||||||
{
|
{
|
||||||
@ -88,18 +103,22 @@ describe('repository.find', () => {
|
|||||||
{
|
{
|
||||||
name: 'post11',
|
name: 'post11',
|
||||||
comments: [{ name: 'comment111' }, { name: 'comment112' }, { name: 'comment113' }],
|
comments: [{ name: 'comment111' }, { name: 'comment112' }, { name: 'comment113' }],
|
||||||
|
tags: [{ id: tags[0].get('id') }],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'post12',
|
name: 'post12',
|
||||||
comments: [{ name: 'comment121' }, { name: 'comment122' }, { name: 'comment123' }],
|
comments: [{ name: 'comment121' }, { name: 'comment122' }, { name: 'comment123' }],
|
||||||
|
tags: [{ id: tags[1].get('id') }, { id: tags[0].get('id') }],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'post13',
|
name: 'post13',
|
||||||
comments: [{ name: 'comment131' }, { name: 'comment132' }, { name: 'comment133' }],
|
comments: [{ name: 'comment131' }, { name: 'comment132' }, { name: 'comment133' }],
|
||||||
|
tags: [{ id: tags[0].get('id') }],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'post14',
|
name: 'post14',
|
||||||
comments: [{ name: 'comment141' }, { name: 'comment142' }, { name: 'comment143' }],
|
comments: [{ name: 'comment141' }, { name: 'comment142' }, { name: 'comment143' }],
|
||||||
|
tags: [{ id: tags[1].get('id') }],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
@ -109,6 +128,7 @@ describe('repository.find', () => {
|
|||||||
{
|
{
|
||||||
name: 'post21',
|
name: 'post21',
|
||||||
comments: [{ name: 'comment211' }, { name: 'comment212' }, { name: 'comment213' }],
|
comments: [{ name: 'comment211' }, { name: 'comment212' }, { name: 'comment213' }],
|
||||||
|
tags: [{ id: tags[0].get('id') }, { id: tags[1].get('id') }],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'post22',
|
name: 'post22',
|
||||||
@ -144,6 +164,16 @@ describe('repository.find', () => {
|
|||||||
await db.close();
|
await db.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should appends with belongs to association', async () => {
|
||||||
|
const posts = await Post.repository.find({
|
||||||
|
appends: ['user'],
|
||||||
|
});
|
||||||
|
|
||||||
|
posts.forEach((post) => {
|
||||||
|
expect(post.get('user')).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('find pk with filter', async () => {
|
test('find pk with filter', async () => {
|
||||||
const Test = db.collection({
|
const Test = db.collection({
|
||||||
name: 'tests',
|
name: 'tests',
|
||||||
|
@ -224,11 +224,11 @@ export class EagerLoadingTree {
|
|||||||
const targetKey = association.targetKey;
|
const targetKey = association.targetKey;
|
||||||
|
|
||||||
for (const instance of node.instances) {
|
for (const instance of node.instances) {
|
||||||
const parentInstance = node.parent.instances.find(
|
const parentInstances = node.parent.instances.filter(
|
||||||
(parentInstance) => parentInstance.get(foreignKey) == instance.get(targetKey),
|
(parentInstance) => parentInstance.get(foreignKey) == instance.get(targetKey),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (parentInstance) {
|
for (const parentInstance of parentInstances) {
|
||||||
parentInstance.setDataValue(association.as, instance);
|
parentInstance.setDataValue(association.as, instance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user