diff --git a/packages/actions/src/actions/common.ts b/packages/actions/src/actions/common.ts index 22c794920..069cdbd7a 100644 --- a/packages/actions/src/actions/common.ts +++ b/packages/actions/src/actions/common.ts @@ -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 }) diff --git a/packages/database/src/fields/field-types.ts b/packages/database/src/fields/field-types.ts index 1b90efd65..4acc70806 100644 --- a/packages/database/src/fields/field-types.ts +++ b/packages/database/src/fields/field-types.ts @@ -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); }