Skip to content

Commit

Permalink
feat: Add support for pagination in lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
aerotoad committed Jul 27, 2023
1 parent 9ef9b94 commit 1a416f8
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/classes/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,19 @@ export class Query<T = any> {

// Apply lookups
for (let lookup of this.lookups) {
const { from, localField, foreignField, as } = lookup;
const { from, localField, foreignField, as, limit, skip, sort } = lookup;
const lookupCollection = new Collection(this._knex, from);
const lookupQuery = lookupCollection.query();
if (Array.isArray(parsedDocument[localField])) {
lookupQuery.containedIn(foreignField, parsedDocument[localField]);
if (limit) lookupQuery.limit(limit);
if (skip) lookupQuery.skip(skip);
if (sort) {
Object.keys(sort).forEach((field) => {
if (sort[field] === 'asc') lookupQuery.ascending(field);
if (sort[field] === 'desc') lookupQuery.descending(field);
});
}
} else {
lookupQuery.equalTo(foreignField, parsedDocument[localField]);
}
Expand Down

0 comments on commit 1a416f8

Please sign in to comment.