diff --git a/package.json b/package.json index d2fce58..187f7cf 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/models/schemas/plugins/pagintae.plugin.ts b/src/models/schemas/plugins/pagintae.plugin.ts index 0349154..31874ad 100644 --- a/src/models/schemas/plugins/pagintae.plugin.ts +++ b/src/models/schemas/plugins/pagintae.plugin.ts @@ -21,7 +21,7 @@ function paginate(schema: any) { * @returns {Promise} */ 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; @@ -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