From 54432a4314f2bd50539d252e035b665500becd70 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Sat, 6 May 2023 15:14:05 +0800 Subject: [PATCH] fix: update tree node (#1812) * fix: update tree node * chore: test --- packages/core/database/src/repository.ts | 1 + .../adjacency-list-repository.ts | 39 +++++++++---------- .../src/__tests__/http-api/tree.test.ts | 16 +++++++- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/packages/core/database/src/repository.ts b/packages/core/database/src/repository.ts index d49773255..191fe6bea 100644 --- a/packages/core/database/src/repository.ts +++ b/packages/core/database/src/repository.ts @@ -471,6 +471,7 @@ export class Repository { + async update(options): Promise { + return super.update({ + ...(options || {}), + addIndex: false, + }); + } + + async find(options?: FindOptions & { addIndex?: boolean }): Promise { const parentNodes = await super.find(lodash.omit(options)); if (parentNodes.length === 0) { @@ -75,34 +82,26 @@ export class AdjacencyListRepository extends Repository { parent.setDataValue(childrenKey, children); } - this.addIndex(parentNodes, childrenKey); + this.addIndex(parentNodes, childrenKey, options); return parentNodes; } - private addIndex(treeArray, childrenKey = 'children') { + private addIndex(treeArray, childrenKey, options) { function traverse(node, index) { - let children; - - if (lodash.isPlainObject(node)) { - node['__index'] = `${index}`; - children = node[childrenKey]; - if (children.length === 0) { - delete node[childrenKey]; - } - } else { - // patch for sequelize toJSON - if (node._options.includeNames && !node._options.includeNames.includes(childrenKey)) { - node._options.includeNames.push(childrenKey); - } + // patch for sequelize toJSON + if (node._options.includeNames && !node._options.includeNames.includes(childrenKey)) { + node._options.includeNames.push(childrenKey); + } + if (options.addIndex !== false) { node.setDataValue('__index', `${index}`); + } - children = node.getDataValue(childrenKey); + const children = node.getDataValue(childrenKey); - if (children.length === 0) { - node.setDataValue(childrenKey, undefined); - } + if (children.length === 0) { + node.setDataValue(childrenKey, undefined); } if (children && children.length > 0) { diff --git a/packages/plugins/collection-manager/src/__tests__/http-api/tree.test.ts b/packages/plugins/collection-manager/src/__tests__/http-api/tree.test.ts index ed5ddb4af..fd454d7c7 100644 --- a/packages/plugins/collection-manager/src/__tests__/http-api/tree.test.ts +++ b/packages/plugins/collection-manager/src/__tests__/http-api/tree.test.ts @@ -1,7 +1,6 @@ import { MockServer } from '@nocobase/test'; import { Database } from '@nocobase/database'; import { createApp } from '../index'; -import lodash from 'lodash'; describe('tree', () => { let app: MockServer; @@ -67,6 +66,19 @@ describe('tree', () => { expect(listResponse.statusCode).toBe(200); - console.log(listResponse.body); + // update c1 + await db.getRepository('categories').update({ + filter: { + name: 'c1', + }, + values: { + __index: '1231', // should ignore + name: 'c11', + }, + }); + + await c1.reload(); + + expect(c1.get('name')).toBe('c11'); }); });