From b3d4e47ef384c497d94cfee6c91c9bad33dfe71d Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Sun, 7 May 2023 23:08:20 +0800 Subject: [PATCH] Fix/empty tree query (#1814) * fix: empty tree query * fix: traverse get --- .../tree-repository/adjacency-list-repository.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/core/database/src/tree-repository/adjacency-list-repository.ts b/packages/core/database/src/tree-repository/adjacency-list-repository.ts index 673988e790..78321b91ee 100644 --- a/packages/core/database/src/tree-repository/adjacency-list-repository.ts +++ b/packages/core/database/src/tree-repository/adjacency-list-repository.ts @@ -24,10 +24,14 @@ export class AdjacencyListRepository extends Repository { const childrenKey = collection.treeChildrenField?.name ?? 'children'; - const sql = this.querySQL( - parentNodes.map((node) => node.id), - collection, - ); + const parentIds = parentNodes.map((node) => node[primaryKey]); + + if (parentIds.length == 0) { + this.database.logger.warn('parentIds is empty'); + return parentNodes; + } + + const sql = this.querySQL(parentIds, collection); const childNodes = await this.database.sequelize.query(sql, { type: 'SELECT', @@ -100,7 +104,7 @@ export class AdjacencyListRepository extends Repository { const children = node.getDataValue(childrenKey); - if (children.length === 0) { + if (children && children.length === 0) { node.setDataValue(childrenKey, undefined); } @@ -117,7 +121,7 @@ export class AdjacencyListRepository extends Repository { } private querySQL(rootIds, collection) { - const { treeChildrenField, treeParentField } = collection; + const { treeParentField } = collection; const foreignKey = treeParentField.options.foreignKey; const foreignKeyField = collection.model.rawAttributes[foreignKey].field;