From 221c90d8355eb0978e7b21f9adcf8a6be273925a Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Sun, 16 Apr 2023 17:30:39 +0800 Subject: [PATCH] fix: find fields arg (#1710) --- .../src/__tests__/repository/find.test.ts | 33 +++++++++++++++++++ packages/core/database/src/options-parser.ts | 12 +++---- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/packages/core/database/src/__tests__/repository/find.test.ts b/packages/core/database/src/__tests__/repository/find.test.ts index 816d819b6a..c33bc79df7 100644 --- a/packages/core/database/src/__tests__/repository/find.test.ts +++ b/packages/core/database/src/__tests__/repository/find.test.ts @@ -9,6 +9,7 @@ describe('repository find', () => { let Post: Collection; let Comment: Collection; let Tag: Collection; + let Profile: Collection; let A1: Collection; let A2: Collection; @@ -26,6 +27,7 @@ describe('repository find', () => { { type: 'string', name: 'name' }, { type: 'integer', name: 'age' }, { type: 'hasMany', name: 'posts' }, + { type: 'hasOne', name: 'profile' }, { type: 'belongsToMany', name: 'a1' }, { type: 'belongsToMany', name: 'a2' }, ], @@ -41,6 +43,15 @@ describe('repository find', () => { fields: [{ type: 'string', name: 'name' }], }); + Profile = db.collection({ + name: 'profiles', + fields: [ + { type: 'integer', name: 'salary' }, + { type: 'belongsTo', name: 'user' }, + { type: 'string', name: 'description' }, + ], + }); + Post = db.collection({ name: 'posts', fields: [ @@ -90,6 +101,7 @@ describe('repository find', () => { posts: [{ title: 'u1t1', comments: [{ content: 'u1t1c1' }], abc1: [{ name: 't1' }] }], a1: [{ name: 'u1a11' }, { name: 'u1a12' }], a2: [{ name: 'u1a21' }, { name: 'u1a22' }], + profile: { salary: 1000 }, }, { name: 'u2', @@ -97,16 +109,37 @@ describe('repository find', () => { posts: [{ title: 'u2t1', comments: [{ content: 'u2t1c1' }] }], a1: [{ name: 'u2a11' }, { name: 'u2a12' }], a2: [{ name: 'u2a21' }, { name: 'u2a22' }], + profile: { salary: 2000 }, }, { name: 'u3', age: 30, posts: [{ title: 'u3t1', comments: [{ content: 'u3t1c1' }] }], + profile: { salary: 3000 }, }, ], }); }); + it('should only output filed in fields args', async () => { + const resp = await User.model.findOne({ + attributes: [], + include: [ + { + association: 'profile', + attributes: ['salary'], + }, + ], + }); + + const users = await User.repository.find({ + fields: ['profile', 'profile.salary', 'profile.id'], + }); + + const firstUser = users[0].toJSON(); + expect(Object.keys(firstUser)).toEqual(['profile']); + }); + it('append with associations', async () => { const users = await User.repository.findAndCount({ appends: ['posts', 'posts.comments'], diff --git a/packages/core/database/src/options-parser.ts b/packages/core/database/src/options-parser.ts index 343b34b464..4e0ae9a4f5 100644 --- a/packages/core/database/src/options-parser.ts +++ b/packages/core/database/src/options-parser.ts @@ -146,6 +146,11 @@ export class OptionsParser { } if (this.options?.fields) { + attributes = []; + if (this.collection.isParent()) { + this.inheritFromSubQuery(attributes); + } + // 将fields拆分为 attributes 和 appends for (const field of this.options.fields) { if (this.isAssociationPath(field)) { @@ -153,13 +158,6 @@ export class OptionsParser { appends.push(field); } else { // field is model attribute, change attributes to array type - if (!Array.isArray(attributes)) { - attributes = []; - if (this.collection.isParent()) { - this.inheritFromSubQuery(attributes); - } - } - attributes.push(field); } }