Skip to content

Commit

Permalink
refactor: shallow copy orderBy args when changing order
Browse files Browse the repository at this point in the history
  • Loading branch information
olavim committed Feb 21, 2020
1 parent 75ec3fd commit cabfa79
Showing 1 changed file with 3 additions and 13 deletions.
16 changes: 3 additions & 13 deletions lib/query-builder/operations/CursorPageOperation.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,16 @@ class CursorPageOperation extends QueryBuilderOperation {

// Swap orderBy directions when going backward
if (this.before) {
builder.forEachOperation(/^(orderBy|orderByExplicit)$/, op => {
op.args[1] = op.args[1] === 'asc' ? 'desc' : 'asc';
builder.forEachOperation(/orderBy/, op => {
const [column, order, ...args] = op.args;
op.args = [column, order === 'asc' ? 'desc' : 'asc', ...args];
});
}

// Save copy of current builder for pageInfo (hasNext, remaining, etc.)
this.resultSizeBuilder = builder.clone().mergeContext({[RESULTSIZE_BUILDER]: true});
}

onAfter1(builder, result) {
// Swap orderBy directions back if we changed them previously
if (this.before) {
builder.forEachOperation(/^(orderBy|orderByExplicit)$/, op => {
op.args[1] = op.args[1] === 'asc' ? 'desc' : 'asc';
});
}

return result;
}

onAfter3(_builder, result) {
// We want to always return results in the same order, as if turning pages in a book
if (this.before) {
Expand Down

0 comments on commit cabfa79

Please sign in to comment.