From e8e9c038e3b8230a8154f97e6b8e1746e63ec154 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Wed, 7 Jun 2023 15:31:29 +0800 Subject: [PATCH] fix: eager load with nested association (#2002) --- .../__tests__/inhertits/collection-inherits.test.ts | 12 ++++++++++-- .../database/src/eager-loading/eager-loading-tree.ts | 6 ++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/core/database/src/__tests__/inhertits/collection-inherits.test.ts b/packages/core/database/src/__tests__/inhertits/collection-inherits.test.ts index 11f45bb71..72c499503 100644 --- a/packages/core/database/src/__tests__/inhertits/collection-inherits.test.ts +++ b/packages/core/database/src/__tests__/inhertits/collection-inherits.test.ts @@ -73,7 +73,10 @@ pgOnly()('collection inherits', () => { it('should append collection name in eager load', async () => { const rootCollection = db.collection({ name: 'assoc', - fields: [{ name: 'name', type: 'string' }], + fields: [ + { name: 'name', type: 'string' }, + { type: 'belongsToMany', name: 'other-assocs' }, + ], }); const childCollection = db.collection({ @@ -81,6 +84,11 @@ pgOnly()('collection inherits', () => { inherits: ['assoc'], }); + const otherAssoc = db.collection({ + name: 'other-assocs', + fields: [{ name: 'name', type: 'string' }], + }); + const User = db.collection({ name: 'users', fields: [ @@ -116,7 +124,7 @@ pgOnly()('collection inherits', () => { }); const users = await User.repository.find({ - appends: ['assocs'], + appends: ['assocs.other-assocs'], }); const user = users[0]; diff --git a/packages/core/database/src/eager-loading/eager-loading-tree.ts b/packages/core/database/src/eager-loading/eager-loading-tree.ts index 57d27a436..184e63277 100644 --- a/packages/core/database/src/eager-loading/eager-loading-tree.ts +++ b/packages/core/database/src/eager-loading/eager-loading-tree.ts @@ -27,6 +27,12 @@ const EagerLoadingNodeProto = { const collection = db.modelCollection.get(this.model); if (collection && collection.isParent()) { + if (!this.attributes) { + this.attributes = { + include: [], + }; + } + OptionsParser.appendInheritInspectAttribute(this.attributes.include, collection); this.inspectInheritAttribute = true; }