diff --git a/packages/database/src/__tests__/respsitory/find.test.ts b/packages/database/src/__tests__/respsitory/find.test.ts index 58984a865c..2d64808409 100644 --- a/packages/database/src/__tests__/respsitory/find.test.ts +++ b/packages/database/src/__tests__/respsitory/find.test.ts @@ -71,6 +71,16 @@ describe('repository find', () => { }); }); + it('append with associations', async () => { + const users = await User.repository.findAndCount({ + appends: ['posts', 'posts.comments'], + }); + + const user = users[0][0]; + // @ts-ignore + expect(user.get('posts')[0].get('comments')).toBeDefined(); + }); + describe('findOne', () => { test('find one with attribute', async () => { const user = await User.repository.findOne({ diff --git a/packages/database/src/options-parser.ts b/packages/database/src/options-parser.ts index 06f6c1c74a..d201c34c96 100644 --- a/packages/database/src/options-parser.ts +++ b/packages/database/src/options-parser.ts @@ -195,7 +195,18 @@ export class OptionsParser { // appends: ['posts'] // appends: ['posts.title'] // All of these can be seen as last level - const lastLevel = appendFields.length <= 2; + let lastLevel: boolean = false; + + if (appendFields.length == 1) { + lastLevel = true; + } + + if (appendFields.length == 2) { + const associationModel = associations[appendFields[0]].target; + if (associationModel.rawAttributes[appendFields[1]]) { + lastLevel = true; + } + } // find association index if (queryParams['include'] == undefined) { @@ -230,8 +241,10 @@ export class OptionsParser { attributes = []; } + const attributeName = appendFields[1]; + // push field to it - attributes.push(appendFields[1]); + attributes.push(attributeName); } else { // if attributes is empty array, change it to object if (Array.isArray(attributes) && attributes.length == 0) { diff --git a/packages/database/src/repository.ts b/packages/database/src/repository.ts index d09d1b03f8..15656d87f1 100644 --- a/packages/database/src/repository.ts +++ b/packages/database/src/repository.ts @@ -10,7 +10,7 @@ import { ModelCtor, Op, Transaction, - UpdateOptions as SequelizeUpdateOptions + UpdateOptions as SequelizeUpdateOptions, } from 'sequelize'; import { Collection } from './collection'; import { Database } from './database';