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:
parent
82ebd0eb44
commit
97106c28e8
@ -151,10 +151,11 @@ describe('associated field order', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const u1 = await db.getRepository('users').findOne({
|
const u1 = await db.getRepository('users').findOne({
|
||||||
appends: ['posts.tags'],
|
appends: ['posts.tags', 'posts.title'],
|
||||||
});
|
});
|
||||||
|
|
||||||
const u1Json = u1.toJSON();
|
const u1Json = u1.toJSON();
|
||||||
|
|
||||||
const u1Posts = u1Json['posts'];
|
const u1Posts = u1Json['posts'];
|
||||||
expect(u1Posts.map((p) => p['title'])).toEqual(['a', 'b', 'c']);
|
expect(u1Posts.map((p) => p['title'])).toEqual(['a', 'b', 'c']);
|
||||||
|
|
||||||
|
@ -488,6 +488,17 @@ describe('repository find', () => {
|
|||||||
expect(user['posts']).toBeDefined();
|
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', () => {
|
describe('find with appends', () => {
|
||||||
test('toJSON', async () => {
|
test('toJSON', async () => {
|
||||||
const user = await User.repository.findOne({
|
const user = await User.repository.findOne({
|
||||||
|
@ -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)) {
|
if (!lodash.isArray(nodeRawAttributes)) {
|
||||||
return;
|
return;
|
||||||
|
@ -37,7 +37,7 @@ const traverseBelongsToMany = (arr: any[], { collection, exclude = [], through }
|
|||||||
const throughCollection = collection.db.getCollection(through);
|
const throughCollection = collection.db.getCollection(through);
|
||||||
return arr.map((item) => {
|
return arr.map((item) => {
|
||||||
const data = traverseJSON(item[through], { collection: throughCollection, exclude });
|
const data = traverseJSON(item[through], { collection: throughCollection, exclude });
|
||||||
if (Object.keys(data).length) {
|
if (data && Object.keys(data).length) {
|
||||||
item[through] = data;
|
item[through] = data;
|
||||||
} else {
|
} else {
|
||||||
delete item[through];
|
delete item[through];
|
||||||
|
Loading…
Reference in New Issue
Block a user