@konplit-services/common 1.0.24 → 1.0.25
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;
|
|
@@ -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;
|