c2-mongoose 2.1.94 → 2.1.96
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.
- package/dist/flow/SearchFlow.d.ts +1 -0
- package/dist/flow/SearchFlow.js +26 -31
- package/package.json +1 -1
- package/src/flow/SearchFlow.ts +44 -23
|
@@ -41,6 +41,7 @@ declare abstract class SearchFlow {
|
|
|
41
41
|
findOne(repository: mongoose.Model<any>, model: any, params?: any): Promise<any>;
|
|
42
42
|
sumBy(model: mongoose.Model<any>, _sum: any, _by: any, items?: any, session?: ClientSession): Promise<any[]>;
|
|
43
43
|
sumByPaging(model: mongoose.Model<any>, fieldToSum: any, fieldToGroup: any, items?: any): Promise<SearchResponse<any>>;
|
|
44
|
+
isValidObjectId(value: string): boolean;
|
|
44
45
|
buildDefaultFilters(objectSearch: any): any;
|
|
45
46
|
addFilterModel(model: any, filters: any): void;
|
|
46
47
|
}
|
package/dist/flow/SearchFlow.js
CHANGED
|
@@ -46,15 +46,6 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
46
46
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
47
|
}
|
|
48
48
|
};
|
|
49
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
50
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
51
|
-
if (ar || !(i in from)) {
|
|
52
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
53
|
-
ar[i] = from[i];
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
57
|
-
};
|
|
58
49
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
59
50
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
60
51
|
};
|
|
@@ -191,34 +182,34 @@ var SearchFlow = /** @class */ (function () {
|
|
|
191
182
|
SearchFlow.prototype.searchPageable = function (model, options) {
|
|
192
183
|
var _a;
|
|
193
184
|
return __awaiter(this, void 0, void 0, function () {
|
|
194
|
-
var stagesItems, result, items, _i, _b, populate;
|
|
185
|
+
var stagesItems, stagesPaging, result, items, _i, _b, populate;
|
|
195
186
|
return __generator(this, function (_c) {
|
|
196
187
|
switch (_c.label) {
|
|
197
188
|
case 0:
|
|
198
|
-
stagesItems =
|
|
199
|
-
|
|
200
|
-
(
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
{ $
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
189
|
+
stagesItems = [{ $match: this.filters }];
|
|
190
|
+
if ((0, Utils_1.isNotEmpty)(options.unions)) {
|
|
191
|
+
stagesItems.push.apply(stagesItems, options.unions);
|
|
192
|
+
}
|
|
193
|
+
if ((0, Utils_1.isNotEmpty)(this.projection)) {
|
|
194
|
+
stagesItems.push({ $project: this.projection });
|
|
195
|
+
}
|
|
196
|
+
if ((0, Utils_1.isNotEmpty)(options.pipelines)) {
|
|
197
|
+
stagesItems.push.apply(stagesItems, options.pipelines);
|
|
198
|
+
}
|
|
199
|
+
stagesItems.push({ $sort: this.sort });
|
|
200
|
+
stagesItems.push({ $skip: ((this.page - 1) * this.limit) || 0 });
|
|
201
|
+
stagesItems.push({ $limit: this.limit });
|
|
202
|
+
stagesPaging = [{ $match: this.filters }];
|
|
203
|
+
if ((0, Utils_1.isNotEmpty)(options.unions)) {
|
|
204
|
+
stagesPaging.push.apply(stagesPaging, options.unions);
|
|
205
|
+
}
|
|
206
|
+
stagesPaging.push({ $count: "total" });
|
|
207
|
+
stagesPaging.push({ $addFields: { page: this.page || 1, limit: this.limit } });
|
|
207
208
|
return [4 /*yield*/, model.aggregate([
|
|
208
209
|
{
|
|
209
210
|
$facet: {
|
|
210
211
|
items: stagesItems,
|
|
211
|
-
paging:
|
|
212
|
-
{ $match: this.filters },
|
|
213
|
-
(0, Utils_1.isNotEmpty)(options.unions) ? options.unions : undefined,
|
|
214
|
-
{ $count: "total" },
|
|
215
|
-
{
|
|
216
|
-
$addFields: {
|
|
217
|
-
page: this.page || 1,
|
|
218
|
-
limit: this.limit
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
]
|
|
212
|
+
paging: stagesPaging
|
|
222
213
|
}
|
|
223
214
|
}
|
|
224
215
|
]).session(options === null || options === void 0 ? void 0 : options.session)];
|
|
@@ -413,6 +404,10 @@ var SearchFlow = /** @class */ (function () {
|
|
|
413
404
|
});
|
|
414
405
|
});
|
|
415
406
|
};
|
|
407
|
+
SearchFlow.prototype.isValidObjectId = function (value) {
|
|
408
|
+
var objectIdPattern = /^[0-9a-fA-F]{24}$/;
|
|
409
|
+
return objectIdPattern.test(value);
|
|
410
|
+
};
|
|
416
411
|
SearchFlow.prototype.buildDefaultFilters = function (objectSearch) {
|
|
417
412
|
var _this = this;
|
|
418
413
|
var filters = { $and: [] };
|
|
@@ -423,7 +418,7 @@ var SearchFlow = /** @class */ (function () {
|
|
|
423
418
|
if (['projection', 'pageable', 'order', 'orderBy', 'properties', 'populate', 'page', 'limit', 'model', 'select', 'searchText', 'isPageable', 'searchPageable'].includes(key)) {
|
|
424
419
|
return;
|
|
425
420
|
}
|
|
426
|
-
if (typeof value === 'string' &&
|
|
421
|
+
if (typeof value === 'string' && _this.isValidObjectId(value)) {
|
|
427
422
|
value = new mongoose_1.Types.ObjectId(value);
|
|
428
423
|
}
|
|
429
424
|
if (value === 'true') {
|
package/package.json
CHANGED
package/src/flow/SearchFlow.ts
CHANGED
|
@@ -118,33 +118,49 @@ abstract class SearchFlow {
|
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
async searchPageable(model: mongoose.Model<any>, options: SearchOptions) {
|
|
121
|
-
let stagesItems = [
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
]
|
|
121
|
+
// let stagesItems = [
|
|
122
|
+
// { $match: this.filters },
|
|
123
|
+
// isNotEmpty(options.unions) ? options.unions : undefined,
|
|
124
|
+
// this.projection ? { $project: this.projection } : { $addFields: {} },
|
|
125
|
+
// ...options.pipelines,
|
|
126
|
+
// { $sort: this.sort },
|
|
127
|
+
// { $skip: ((this.page - 1) * this.limit) || 0 },
|
|
128
|
+
// { $limit: this.limit },
|
|
129
|
+
// ]
|
|
130
|
+
|
|
131
|
+
let stagesItems: any[] = [{ $match: this.filters }]
|
|
132
|
+
|
|
133
|
+
if (isNotEmpty(options.unions)) {
|
|
134
|
+
stagesItems.push(...options.unions)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (isNotEmpty(this.projection)) {
|
|
138
|
+
stagesItems.push({ $project: this.projection })
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (isNotEmpty(options.pipelines)) {
|
|
142
|
+
stagesItems.push(...options.pipelines)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
stagesItems.push({ $sort: this.sort })
|
|
146
|
+
stagesItems.push({ $skip: ((this.page - 1) * this.limit) || 0 })
|
|
147
|
+
stagesItems.push({ $limit: this.limit })
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
let stagesPaging: any[] = [{ $match: this.filters }]
|
|
151
|
+
if (isNotEmpty(options.unions)) {
|
|
152
|
+
stagesPaging.push(...options.unions)
|
|
153
|
+
}
|
|
154
|
+
stagesPaging.push({ $count: "total" })
|
|
155
|
+
stagesPaging.push({ $addFields: { page: this.page || 1, limit: this.limit } })
|
|
156
|
+
|
|
130
157
|
|
|
131
158
|
const result = await model.aggregate(
|
|
132
159
|
[
|
|
133
160
|
{
|
|
134
161
|
$facet: {
|
|
135
|
-
items: stagesItems
|
|
136
|
-
paging:
|
|
137
|
-
{ $match: this.filters },
|
|
138
|
-
isNotEmpty(options.unions) ? options.unions as any : undefined,
|
|
139
|
-
{ $count: "total" },
|
|
140
|
-
{
|
|
141
|
-
$addFields:
|
|
142
|
-
{
|
|
143
|
-
page: this.page || 1,
|
|
144
|
-
limit: this.limit
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
]
|
|
162
|
+
items: stagesItems,
|
|
163
|
+
paging: stagesPaging
|
|
148
164
|
}
|
|
149
165
|
}
|
|
150
166
|
]
|
|
@@ -305,6 +321,11 @@ abstract class SearchFlow {
|
|
|
305
321
|
}
|
|
306
322
|
}
|
|
307
323
|
|
|
324
|
+
public isValidObjectId(value: string) {
|
|
325
|
+
const objectIdPattern = /^[0-9a-fA-F]{24}$/;
|
|
326
|
+
return objectIdPattern.test(value);
|
|
327
|
+
}
|
|
328
|
+
|
|
308
329
|
public buildDefaultFilters(objectSearch: any) {
|
|
309
330
|
let filters = { $and: [] } as any
|
|
310
331
|
Object.entries(objectSearch.model).forEach(([key, value]) => {
|
|
@@ -314,7 +335,7 @@ abstract class SearchFlow {
|
|
|
314
335
|
return
|
|
315
336
|
}
|
|
316
337
|
|
|
317
|
-
if (typeof value === 'string' &&
|
|
338
|
+
if (typeof value === 'string' && this.isValidObjectId(value as string)) {
|
|
318
339
|
value = new Types.ObjectId(value as string)
|
|
319
340
|
}
|
|
320
341
|
|