chore: should not return children property when child nodes are empty (#1825)
This commit is contained in:
parent
088acd906d
commit
8183ff5564
@ -16,6 +16,63 @@ describe('tree test', function () {
|
||||
await db.close();
|
||||
});
|
||||
|
||||
it('should not return children property when child nodes are empty', async () => {
|
||||
const collection = db.collection({
|
||||
name: 'categories',
|
||||
tree: 'adjacency-list',
|
||||
fields: [
|
||||
{ type: 'string', name: 'name' },
|
||||
{
|
||||
type: 'belongsTo',
|
||||
name: 'parent',
|
||||
treeParent: true,
|
||||
},
|
||||
{
|
||||
type: 'hasMany',
|
||||
name: 'children',
|
||||
treeChildren: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await db.sync();
|
||||
|
||||
await collection.repository.create({
|
||||
values: [
|
||||
{
|
||||
name: 'c1',
|
||||
children: [
|
||||
{
|
||||
name: 'c11',
|
||||
},
|
||||
{
|
||||
name: 'c12',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'c2',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const tree = await collection.repository.find({
|
||||
filter: {
|
||||
parentId: null,
|
||||
},
|
||||
});
|
||||
|
||||
const c2 = tree.find((item) => item.name === 'c2');
|
||||
expect(c2.toJSON()['children']).toBeUndefined();
|
||||
|
||||
const c11 = tree
|
||||
.find((item) => item.name === 'c1')
|
||||
.get('children')
|
||||
.find((item) => item.name === 'c11');
|
||||
|
||||
expect(c11.toJSON()['children']).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should add sort field', async () => {
|
||||
const Tasks = db.collection({
|
||||
name: 'tasks',
|
||||
|
@ -263,7 +263,7 @@ export class Repository<TModelAttributes extends {} = any, TCreationAttributes e
|
||||
* find
|
||||
* @param options
|
||||
*/
|
||||
async find(options?: FindOptions) {
|
||||
async find(options: FindOptions = {}) {
|
||||
const model = this.collection.model;
|
||||
const transaction = await this.getTransaction(options);
|
||||
|
||||
|
@ -9,7 +9,7 @@ export class AdjacencyListRepository extends Repository {
|
||||
});
|
||||
}
|
||||
|
||||
async find(options?: FindOptions & { addIndex?: boolean }): Promise<any> {
|
||||
async find(options: FindOptions & { addIndex?: boolean } = {}): Promise<any> {
|
||||
const parentNodes = await super.find(lodash.omit(options));
|
||||
|
||||
if (options.raw) {
|
||||
@ -80,7 +80,10 @@ export class AdjacencyListRepository extends Repository {
|
||||
}
|
||||
|
||||
return children.map((child) => {
|
||||
child.setDataValue(childrenKey, buildTree(child.id));
|
||||
const childrenValues = buildTree(child.id);
|
||||
if (childrenValues.length > 0) {
|
||||
child.setDataValue(childrenKey, childrenValues);
|
||||
}
|
||||
return child;
|
||||
});
|
||||
}
|
||||
@ -88,8 +91,10 @@ export class AdjacencyListRepository extends Repository {
|
||||
for (const parent of parentNodes) {
|
||||
const parentId = parent[primaryKey];
|
||||
const children = buildTree(parentId);
|
||||
if (children.length > 0) {
|
||||
parent.setDataValue(childrenKey, children);
|
||||
}
|
||||
}
|
||||
|
||||
this.addIndex(parentNodes, childrenKey, options);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user