Fix/empty tree query (#1814)

* fix: empty tree query

* fix: traverse get
This commit is contained in:
ChengLei Shao 2023-05-07 23:08:20 +08:00 committed by GitHub
parent 8aee0eef9b
commit b3d4e47ef3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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;