parent
258f4e980d
commit
54432a4314
@ -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 });
|
||||
|
@ -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) {
|
||||
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user