chore(database): return emtpy fields when attributes not specified (#2034)

* test: append nested field

* chore: return emtpy fields when attributes not specified

* fix: test

* fix: data template error

---------

Co-authored-by: chenos <chenlinxh@gmail.com>
This commit is contained in:
ChengLei Shao 2023-06-12 18:44:43 +08:00 committed by GitHub
parent 82ebd0eb44
commit 97106c28e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 3 deletions

View File

@ -151,10 +151,11 @@ describe('associated field order', () => {
});
const u1 = await db.getRepository('users').findOne({
appends: ['posts.tags'],
appends: ['posts.tags', 'posts.title'],
});
const u1Json = u1.toJSON();
const u1Posts = u1Json['posts'];
expect(u1Posts.map((p) => p['title'])).toEqual(['a', 'b', 'c']);

View File

@ -488,6 +488,17 @@ describe('repository find', () => {
expect(user['posts']).toBeDefined();
});
test('find with nested fields', async () => {
const user = await User.repository.findOne({
fields: ['posts.comments.content'],
});
const userData = user.toJSON();
const post = userData['posts'][0];
expect(Object.keys(post)).toEqual(['comments']);
});
describe('find with appends', () => {
test('toJSON', async () => {
const user = await User.repository.findOne({

View File

@ -343,7 +343,8 @@ export class EagerLoadingTree {
});
}
const nodeRawAttributes = node.rawAttributes;
// if no attributes are specified, return empty fields
const nodeRawAttributes = node.rawAttributes || [];
if (!lodash.isArray(nodeRawAttributes)) {
return;

View File

@ -37,7 +37,7 @@ const traverseBelongsToMany = (arr: any[], { collection, exclude = [], through }
const throughCollection = collection.db.getCollection(through);
return arr.map((item) => {
const data = traverseJSON(item[through], { collection: throughCollection, exclude });
if (Object.keys(data).length) {
if (data && Object.keys(data).length) {
item[through] = data;
} else {
delete item[through];