Skip to content

Commit

Permalink
Safer shape detection for find() orderBy.
Browse files Browse the repository at this point in the history
This way is more verbose which is what I was fighting against at first.
But the advantage of being fully field name safe seems worth it and
I made it not the most horrible thing, eventually.
  • Loading branch information
vampirical committed Mar 8, 2024
1 parent 851a7c0 commit 6ecb237
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Record.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,16 @@ class Record extends Object {
instance.offset(offset);
}
if (orderBy) {
if (Array.isArray(orderBy) && !Object.values(sort).includes(orderBy[1])) {
instance.orderBy(...orderBy);
} else {
const isStringOrderBy = typeof orderBy === 'string' || orderBy instanceof String;
const isSingleOrderByArray =
!isStringOrderBy &&
Array.isArray(orderBy) &&
orderBy.length === 2 &&
sort[orderBy[1]] !== undefined;
if (isStringOrderBy || isSingleOrderByArray) {
instance.orderBy(orderBy);
} else {
instance.orderBy(...orderBy);
}
}
instance.options(options);
Expand Down

0 comments on commit 6ecb237

Please sign in to comment.