fix: find fields arg (#1710)

This commit is contained in:
ChengLei Shao 2023-04-16 17:30:39 +08:00 committed by GitHub
parent 6abe97291e
commit 221c90d835
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 7 deletions

View File

@ -9,6 +9,7 @@ describe('repository find', () => {
let Post: Collection; let Post: Collection;
let Comment: Collection; let Comment: Collection;
let Tag: Collection; let Tag: Collection;
let Profile: Collection;
let A1: Collection; let A1: Collection;
let A2: Collection; let A2: Collection;
@ -26,6 +27,7 @@ describe('repository find', () => {
{ type: 'string', name: 'name' }, { type: 'string', name: 'name' },
{ type: 'integer', name: 'age' }, { type: 'integer', name: 'age' },
{ type: 'hasMany', name: 'posts' }, { type: 'hasMany', name: 'posts' },
{ type: 'hasOne', name: 'profile' },
{ type: 'belongsToMany', name: 'a1' }, { type: 'belongsToMany', name: 'a1' },
{ type: 'belongsToMany', name: 'a2' }, { type: 'belongsToMany', name: 'a2' },
], ],
@ -41,6 +43,15 @@ describe('repository find', () => {
fields: [{ type: 'string', name: 'name' }], 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({ Post = db.collection({
name: 'posts', name: 'posts',
fields: [ fields: [
@ -90,6 +101,7 @@ describe('repository find', () => {
posts: [{ title: 'u1t1', comments: [{ content: 'u1t1c1' }], abc1: [{ name: 't1' }] }], posts: [{ title: 'u1t1', comments: [{ content: 'u1t1c1' }], abc1: [{ name: 't1' }] }],
a1: [{ name: 'u1a11' }, { name: 'u1a12' }], a1: [{ name: 'u1a11' }, { name: 'u1a12' }],
a2: [{ name: 'u1a21' }, { name: 'u1a22' }], a2: [{ name: 'u1a21' }, { name: 'u1a22' }],
profile: { salary: 1000 },
}, },
{ {
name: 'u2', name: 'u2',
@ -97,16 +109,37 @@ describe('repository find', () => {
posts: [{ title: 'u2t1', comments: [{ content: 'u2t1c1' }] }], posts: [{ title: 'u2t1', comments: [{ content: 'u2t1c1' }] }],
a1: [{ name: 'u2a11' }, { name: 'u2a12' }], a1: [{ name: 'u2a11' }, { name: 'u2a12' }],
a2: [{ name: 'u2a21' }, { name: 'u2a22' }], a2: [{ name: 'u2a21' }, { name: 'u2a22' }],
profile: { salary: 2000 },
}, },
{ {
name: 'u3', name: 'u3',
age: 30, age: 30,
posts: [{ title: 'u3t1', comments: [{ content: 'u3t1c1' }] }], 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 () => { it('append with associations', async () => {
const users = await User.repository.findAndCount({ const users = await User.repository.findAndCount({
appends: ['posts', 'posts.comments'], appends: ['posts', 'posts.comments'],

View File

@ -146,6 +146,11 @@ export class OptionsParser {
} }
if (this.options?.fields) { if (this.options?.fields) {
attributes = [];
if (this.collection.isParent()) {
this.inheritFromSubQuery(attributes);
}
// 将fields拆分为 attributes 和 appends // 将fields拆分为 attributes 和 appends
for (const field of this.options.fields) { for (const field of this.options.fields) {
if (this.isAssociationPath(field)) { if (this.isAssociationPath(field)) {
@ -153,13 +158,6 @@ export class OptionsParser {
appends.push(field); appends.push(field);
} else { } else {
// field is model attribute, change attributes to array type // field is model attribute, change attributes to array type
if (!Array.isArray(attributes)) {
attributes = [];
if (this.collection.isParent()) {
this.inheritFromSubQuery(attributes);
}
}
attributes.push(field); attributes.push(field);
} }
} }