@konplit-services/common 1.0.24 → 1.0.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,11 @@
1
- import { Model } from "mongoose";
1
+ import { Model, PopulateOptions } from "mongoose";
2
2
  export interface PaginationOptions {
3
3
  page?: number;
4
4
  limit?: number;
5
5
  sort?: Record<string, "asc" | "desc">;
6
6
  select?: string | Record<string, any>;
7
7
  query?: Record<string, any>;
8
+ populate?: (string | PopulateOptions)[];
8
9
  }
9
10
  export interface PaginationResult<T> {
10
11
  current_page: number;
@@ -30,7 +31,7 @@ export interface FilterOptions {
30
31
  accountId?: string;
31
32
  [key: string]: any;
32
33
  }
33
- export declare const generateQueryObject: (filters: FilterOptions, searchesFieldNames?: string[], select?: string | Record<string, any>) => {
34
+ export declare const generateQueryObject: (filters: FilterOptions, searchesFieldNames?: string[], select?: string | Record<string, any>, populate?: (string | PopulateOptions)[]) => {
34
35
  page: number;
35
36
  limit: number;
36
37
  query: any;
@@ -38,4 +39,5 @@ export declare const generateQueryObject: (filters: FilterOptions, searchesField
38
39
  [key: string]: "asc" | "desc";
39
40
  };
40
41
  select: string | Record<string, any>;
42
+ populate: (string | PopulateOptions)[];
41
43
  };
@@ -18,6 +18,7 @@ function paginate(model, options = {}) {
18
18
  const query = options.query || {};
19
19
  const sort = options.sort || {};
20
20
  const select = options.select || "";
21
+ const populate = options.populate || "";
21
22
  const keys = Object.keys(query);
22
23
  let totalDocs;
23
24
  if (keys.length > 0) {
@@ -31,6 +32,10 @@ function paginate(model, options = {}) {
31
32
  if (select) {
32
33
  queryBuilder.select(select);
33
34
  }
35
+ // Populate specified fields
36
+ if (populate) {
37
+ queryBuilder.populate(populate);
38
+ }
34
39
  const results = yield queryBuilder.skip(skip).limit(limit);
35
40
  const hasNextPage = page < totalPages;
36
41
  const hasPreviousPage = page > 1;
@@ -48,7 +53,7 @@ function paginate(model, options = {}) {
48
53
  });
49
54
  }
50
55
  exports.paginate = paginate;
51
- const generateQueryObject = (filters, searchesFieldNames = [], select = "") => {
56
+ const generateQueryObject = (filters, searchesFieldNames = [], select = "", populate = []) => {
52
57
  const { status, limit, page, sort_by = "createdAt", order = "desc", merchantId, accountId, } = filters;
53
58
  const excludes = [
54
59
  "status",
@@ -112,6 +117,7 @@ const generateQueryObject = (filters, searchesFieldNames = [], select = "") => {
112
117
  query: query,
113
118
  sort: sort,
114
119
  select: select,
120
+ populate,
115
121
  };
116
122
  };
117
123
  exports.generateQueryObject = generateQueryObject;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@konplit-services/common",
3
- "version": "1.0.24",
3
+ "version": "1.0.26",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",