@konplit-services/common 1.24.0 → 1.24.2
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,4 +1,7 @@
|
|
|
1
|
-
import { Model, PopulateOptions } from "mongoose";
|
|
1
|
+
import { Model, PopulateOptions, Cursor } from "mongoose";
|
|
2
|
+
type Lean<T> = T & {
|
|
3
|
+
[key: string]: any;
|
|
4
|
+
};
|
|
2
5
|
export interface PaginationOptions {
|
|
3
6
|
page?: number;
|
|
4
7
|
limit?: number;
|
|
@@ -18,6 +21,7 @@ export interface PaginationResult<T> {
|
|
|
18
21
|
results: T[];
|
|
19
22
|
}
|
|
20
23
|
export declare function paginate<T>(model: Model<T>, options?: PaginationOptions): Promise<PaginationResult<T>>;
|
|
24
|
+
export declare function paginateV2Cursor<T>(model: Model<T>, options?: PaginationOptions): Cursor<Lean<T>>;
|
|
21
25
|
export interface FilterOptions {
|
|
22
26
|
search?: string;
|
|
23
27
|
status?: string;
|
|
@@ -66,3 +70,4 @@ export interface DefaultFilterOptions {
|
|
|
66
70
|
min_amount?: string;
|
|
67
71
|
max_amount?: string;
|
|
68
72
|
}
|
|
73
|
+
export {};
|
|
@@ -11,11 +11,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.generateQueryObjectV2 = exports.generateQueryObject = void 0;
|
|
13
13
|
exports.paginate = paginate;
|
|
14
|
+
exports.paginateV2Cursor = paginateV2Cursor;
|
|
14
15
|
const date_processing_1 = require("./date-processing");
|
|
16
|
+
const MAX_LIMIT = 1000;
|
|
15
17
|
function paginate(model_1) {
|
|
16
18
|
return __awaiter(this, arguments, void 0, function* (model, options = {}) {
|
|
19
|
+
var _a;
|
|
17
20
|
const page = options.page || 1;
|
|
18
|
-
const limit = options.limit
|
|
21
|
+
const limit = Math.min((_a = options.limit) !== null && _a !== void 0 ? _a : 200, MAX_LIMIT);
|
|
19
22
|
const skip = (page - 1) * limit;
|
|
20
23
|
const query = options.query || {};
|
|
21
24
|
const sort = options.sort || {};
|
|
@@ -54,6 +57,23 @@ function paginate(model_1) {
|
|
|
54
57
|
return paginationInfo;
|
|
55
58
|
});
|
|
56
59
|
}
|
|
60
|
+
// return a cursor to be use for filtering
|
|
61
|
+
function paginateV2Cursor(model, options = {}) {
|
|
62
|
+
const query = options.query || {};
|
|
63
|
+
const sort = options.sort || {};
|
|
64
|
+
const select = options.select || "";
|
|
65
|
+
const populate = options.populate || "";
|
|
66
|
+
const queryBuilder = model.find(query).sort(sort);
|
|
67
|
+
if (select) {
|
|
68
|
+
queryBuilder.select(select);
|
|
69
|
+
}
|
|
70
|
+
// Populate specified fields
|
|
71
|
+
if (populate) {
|
|
72
|
+
queryBuilder.populate(populate);
|
|
73
|
+
}
|
|
74
|
+
// @ts-ignore
|
|
75
|
+
return queryBuilder.lean().cursor({ batchSize: 50 });
|
|
76
|
+
}
|
|
57
77
|
const generateQueryObject = (filters, searchesFieldNames = [], select = "", populate = []) => {
|
|
58
78
|
const { status, limit, page, sort_by = "createdAt", order = "desc", merchantId, accountId, dateField, amountField, } = filters;
|
|
59
79
|
const excludes = [
|