fix: add sticky parameter to sort action

This commit is contained in:
chenos 2021-03-25 16:03:43 +08:00
parent b101760092
commit eba121b998
2 changed files with 5 additions and 3 deletions

View File

@ -533,7 +533,7 @@ export async function sort(ctx: Context, next: Next) {
const Model = ctx.db.getModel(resourceName);
const table = ctx.db.getTable(resourceName);
const { field, target } = values;
const { sticky, field, target } = values;
if (!values.field || typeof target === 'undefined') {
return next();
}
@ -559,6 +559,7 @@ export async function sort(ctx: Context, next: Next) {
},
transaction
});
if (!source) {
await transaction.rollback();
throw new Error(`resource(${resourceKey}) does not exist`);
@ -627,6 +628,7 @@ export async function sort(ctx: Context, next: Next) {
} else {
Object.assign(updates, {
[sortAttr]: await sortField.getNextValue({
next: sticky ? 'min' : 'max',
where: targetScopeWhere,
transaction
})

View File

@ -789,10 +789,10 @@ export class SORT extends NUMBER {
return DataTypes.INTEGER;
}
public async getNextValue(this: SORT, { where, transaction }) {
public async getNextValue(this: SORT, { where, transaction, next: n = 'max' }) {
const table = this.context.sourceTable;
const Model = table.getModel();
const { name, next = 'max' } = this.options;
const { name, next = n } = this.options;
const extremum: number = await Model[next](name, { where, transaction }) || 0;
return extremum + (next === 'max' ? 1 : -1);
}