From 088acd906ddc3cf91b68b512b51bc1415ab5e398 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Tue, 9 May 2023 12:10:53 +0800 Subject: [PATCH] fix: tree with sort field (#1822) --- .../core/database/src/__tests__/tree.test.ts | 54 +++++++++++++++++++ .../adjacency-list-repository.ts | 7 ++- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/packages/core/database/src/__tests__/tree.test.ts b/packages/core/database/src/__tests__/tree.test.ts index 2ef144f0b..498c260ff 100644 --- a/packages/core/database/src/__tests__/tree.test.ts +++ b/packages/core/database/src/__tests__/tree.test.ts @@ -16,6 +16,60 @@ describe('tree test', function () { await db.close(); }); + it('should add sort field', async () => { + const Tasks = db.collection({ + name: 'tasks', + tree: 'adjacency-list', + fields: [ + { + type: 'string', + name: 'name', + }, + { + type: 'belongsTo', + name: 'parent', + treeParent: true, + }, + { + type: 'hasMany', + name: 'children', + treeChildren: true, + }, + { + type: 'string', + name: 'status', + }, + ], + }); + + await db.sync(); + + await Tasks.repository.create({ + values: { + name: 'task1', + status: 'doing', + }, + }); + + await Tasks.repository.create({ + values: { + name: 'task2', + status: 'pending', + }, + }); + + await Tasks.repository.create({ + values: { + name: 'task3', + status: 'pending', + }, + }); + + Tasks.setField('sort', { type: 'sort', scopeKey: 'status' }); + + await db.sync(); + }); + it('should be auto completed', () => { const collection = db.collection({ name: 'categories', 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 78321b91e..39bfb80de 100644 --- a/packages/core/database/src/tree-repository/adjacency-list-repository.ts +++ b/packages/core/database/src/tree-repository/adjacency-list-repository.ts @@ -12,12 +12,17 @@ export class AdjacencyListRepository extends Repository { async find(options?: FindOptions & { addIndex?: boolean }): Promise { const parentNodes = await super.find(lodash.omit(options)); + if (options.raw) { + return parentNodes; + } + if (parentNodes.length === 0) { return []; } const templateModel = parentNodes[0]; - const collection = this.database.modelCollection.get(templateModel.constructor); + + const collection = this.collection; const primaryKey = collection.model.primaryKeyAttribute; const { treeParentField } = collection; const foreignKey = treeParentField.options.foreignKey;