fix: tree with sort field (#1822)

This commit is contained in:
ChengLei Shao 2023-05-09 12:10:53 +08:00 committed by GitHub
parent 7fcdc5d336
commit 088acd906d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 1 deletions

View File

@ -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',

View File

@ -12,12 +12,17 @@ export class AdjacencyListRepository extends Repository {
async find(options?: FindOptions & { addIndex?: boolean }): Promise<any> {
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;