Skip to content

Commit

Permalink
Merge pull request #133 from TogetherCrew/add-populate-to-paginate-pl…
Browse files Browse the repository at this point in the history
…ugin

Add populate to paginate plugin
  • Loading branch information
cyri113 authored Dec 1, 2023
2 parents 248f62a + 603dc87 commit d9a617c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@togethercrew.dev/db",
"version": "3.0.17",
"version": "3.0.18",
"description": "All interactions with DB",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
12 changes: 11 additions & 1 deletion src/models/schemas/plugins/pagintae.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function paginate(schema: any) {
* @returns {Promise<QueryResult>}
*/
schema.statics.paginate = async function (filter: any, options: any) {
let { limit, page, skip } = options;
let { limit, page, skip, populate } = options;
const { sortBy } = options;
limit = limit && parseInt(limit, 10) > 0 ? parseInt(limit, 10) : 10;
page = page && parseInt(page, 10) > 0 ? parseInt(page, 10) : 1;
Expand All @@ -42,6 +42,16 @@ function paginate(schema: any) {
const countPromise = this.countDocuments(filter).exec();
let docsPromise = this.find(filter).sort(sort).skip(skip).limit(limit);

if (populate) {
if (Array.isArray(populate)) {
populate.forEach((p) => {
docsPromise = docsPromise.populate(p);
});
} else {
docsPromise = docsPromise.populate(populate);
}
}

docsPromise = docsPromise.exec();

// eslint-disable-next-line @typescript-eslint/return-await
Expand Down

0 comments on commit d9a617c

Please sign in to comment.