fix: tree with sort field (#1822)
This commit is contained in:
parent
7fcdc5d336
commit
088acd906d
@ -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',
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user