From d37a9e8c3b7d9dd3780d11af1bc1d857a3467a3e Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Fri, 1 Dec 2023 16:27:46 +0330 Subject: [PATCH 1/2] [FEATURE]: add populate feature to paginate plugin --- package.json | 2 +- src/models/schemas/plugins/pagintae.plugin.ts | 23 ++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) 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..f122a2e 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; @@ -39,9 +39,30 @@ function paginate(schema: any) { sort = '-createdAt'; } + if (options.populate) { + options.populate.split(',').forEach((populateOption: string) => { + docsPromise = docsPromise.populate( + populateOption + .split('.') + .reverse() + .reduce((a: any, b: string) => ({ path: b, populate: a }), {}), + ); + }); + } + 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 From 603dc87643ab52c6e1432307c7aaebbc987a5cbb Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Fri, 1 Dec 2023 16:28:46 +0330 Subject: [PATCH 2/2] [FEATURE]: add populate feature to paginate plugin --- src/models/schemas/plugins/pagintae.plugin.ts | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/models/schemas/plugins/pagintae.plugin.ts b/src/models/schemas/plugins/pagintae.plugin.ts index f122a2e..31874ad 100644 --- a/src/models/schemas/plugins/pagintae.plugin.ts +++ b/src/models/schemas/plugins/pagintae.plugin.ts @@ -39,17 +39,6 @@ function paginate(schema: any) { sort = '-createdAt'; } - if (options.populate) { - options.populate.split(',').forEach((populateOption: string) => { - docsPromise = docsPromise.populate( - populateOption - .split('.') - .reverse() - .reduce((a: any, b: string) => ({ path: b, populate: a }), {}), - ); - }); - } - const countPromise = this.countDocuments(filter).exec(); let docsPromise = this.find(filter).sort(sort).skip(skip).limit(limit);