fix: update tree node (#1812)

* fix: update tree node

* chore: test
This commit is contained in:
ChengLei Shao 2023-05-06 15:14:05 +08:00 committed by GitHub
parent 258f4e980d
commit 54432a4314
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 22 deletions

View File

@ -471,6 +471,7 @@ export class Repository<TModelAttributes extends {} = any, TCreationAttributes e
records: options.values,
});
}
const transaction = await this.getTransaction(options);
const guard = UpdateGuard.fromOptions(this.model, { ...options, underscored: this.collection.options.underscored });

View File

@ -2,7 +2,14 @@ import { FindOptions, Repository } from '../repository';
import lodash from 'lodash';
export class AdjacencyListRepository extends Repository {
async find(options?: FindOptions): Promise<any> {
async update(options): Promise<any> {
return super.update({
...(options || {}),
addIndex: false,
});
}
async find(options?: FindOptions & { addIndex?: boolean }): Promise<any> {
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) {

View File

@ -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');
});
});