c2-mongoose 2.1.305 → 2.1.307

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.
@@ -18,6 +18,8 @@ declare class SearcherFlow<D> {
18
18
  searchTextFields: string[];
19
19
  orderSense: string;
20
20
  orderBy: string;
21
+ groupBy: string[];
22
+ sumFields: string[];
21
23
  page: number;
22
24
  limit: number;
23
25
  pageable: boolean;
@@ -35,5 +37,6 @@ declare class SearcherFlow<D> {
35
37
  isValidObjectId(value: string): boolean;
36
38
  buildOrdenation(): any;
37
39
  buildRegex(searchText: string): RegExp;
40
+ buildGrouping(fieldsToGroup: string[], sumFields: string[], countFields: string[]): void;
38
41
  }
39
42
  export default SearcherFlow;
@@ -70,6 +70,8 @@ var SearcherFlow = /** @class */ (function () {
70
70
  this.searchTextFields = [];
71
71
  this.orderSense = "desc";
72
72
  this.orderBy = "_id";
73
+ this.groupBy = [];
74
+ this.sumFields = [];
73
75
  this.page = 1;
74
76
  this.limit = 50;
75
77
  this.pageable = true;
@@ -108,9 +110,6 @@ var SearcherFlow = /** @class */ (function () {
108
110
  }
109
111
  stagesItems.push({ $sort: this.buildOrdenation() });
110
112
  stagesPaging = [];
111
- // if (isNotEmpty(options.pipelines)) {
112
- // stagesPaging.push(...options.pipelines)
113
- // }
114
113
  if ((0, Utils_1.isNotEmpty)(options.unions)) {
115
114
  stagesPaging.push.apply(stagesPaging, options.unions);
116
115
  }
@@ -329,6 +328,9 @@ var SearcherFlow = /** @class */ (function () {
329
328
  'pageable',
330
329
  'orderSense',
331
330
  'orderBy',
331
+ 'groupBy',
332
+ 'sumFields',
333
+ 'countFields',
332
334
  'properties',
333
335
  'populate',
334
336
  'page',
@@ -449,6 +451,21 @@ var SearcherFlow = /** @class */ (function () {
449
451
  .replace(/[u|ü|ú|ù|U|Ú|Ü|Ù]/g, '[u,ü,ú,ù,U,Ú,Ü,Ù]');
450
452
  return new RegExp("".concat(regexExpression), 'i');
451
453
  };
454
+ SearcherFlow.prototype.buildGrouping = function (fieldsToGroup, sumFields, countFields) {
455
+ var groupsClauses = [];
456
+ fieldsToGroup.forEach(function (fieldToGroup) {
457
+ var groupClause = {
458
+ _id: "$".concat(fieldToGroup)
459
+ };
460
+ sumFields.forEach(function (sumField) {
461
+ groupClause[sumField] = { $sum: { $ifNull: ["$".concat(sumField), 0] } };
462
+ });
463
+ countFields.forEach(function (countField) {
464
+ groupClause[countField] = { $sum: 1 };
465
+ });
466
+ groupsClauses.push(groupClause);
467
+ });
468
+ };
452
469
  return SearcherFlow;
453
470
  }());
454
471
  exports.default = SearcherFlow;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-mongoose",
3
- "version": "2.1.305",
3
+ "version": "2.1.307",
4
4
  "description": "Lib to make any search in database mongoose and use as basic crud",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -27,6 +27,8 @@ class SearcherFlow<D> {
27
27
  public searchTextFields: string[] = []
28
28
  public orderSense: string = "desc"
29
29
  public orderBy: string = "_id"
30
+ public groupBy: string[] = []
31
+ public sumFields: string[] = []
30
32
  public page: number = 1
31
33
  public limit: number = 50
32
34
  public pageable: boolean = true
@@ -69,11 +71,7 @@ class SearcherFlow<D> {
69
71
  }
70
72
  stagesItems.push({ $sort: this.buildOrdenation() })
71
73
 
72
- ///
73
74
  let stagesPaging: any[] = []
74
- // if (isNotEmpty(options.pipelines)) {
75
- // stagesPaging.push(...options.pipelines)
76
- // }
77
75
  if (isNotEmpty(options.unions)) {
78
76
  stagesPaging.push(...options.unions)
79
77
  }
@@ -238,7 +236,7 @@ class SearcherFlow<D> {
238
236
  condition[fieldName] = { $exists: value as boolean }
239
237
  } else if (key.endsWith("IsEmpty")) {
240
238
  key = key.replace("IsEmpty", "");
241
- condition[key] = value ? { $size: 0 } : { $not: { $size: 0 } };
239
+ condition[key] = value ? { $size: 0 } : { $not: { $size: 0 } };
242
240
  } else {
243
241
  if (this.model?.schema?.paths[key]?.instance === 'Array') {
244
242
  if (!Array.isArray(value)) {
@@ -259,7 +257,7 @@ class SearcherFlow<D> {
259
257
  condition[key] = { $nin: value };
260
258
  } else if (key.endsWith("IsEmpty")) {
261
259
  key = key.replace("IsEmpty", "");
262
- condition[key] = value ? { $size: 0 } : { $not: { $size: 0 } };
260
+ condition[key] = value ? { $size: 0 } : { $not: { $size: 0 } };
263
261
  } else {
264
262
  condition[key] = { $in: value };
265
263
  }
@@ -305,6 +303,9 @@ class SearcherFlow<D> {
305
303
  'pageable',
306
304
  'orderSense',
307
305
  'orderBy',
306
+ 'groupBy',
307
+ 'sumFields',
308
+ 'countFields',
308
309
  'properties',
309
310
  'populate',
310
311
  'page',
@@ -437,6 +438,27 @@ class SearcherFlow<D> {
437
438
  .replace(/[u|ü|ú|ù|U|Ú|Ü|Ù]/g, '[u,ü,ú,ù,U,Ú,Ü,Ù]')
438
439
  return new RegExp(`${regexExpression}`, 'i');
439
440
  }
441
+
442
+ buildGrouping(fieldsToGroup: string[], sumFields: string[], countFields: string[]) {
443
+ const groupsClauses = []
444
+
445
+ fieldsToGroup.forEach((fieldToGroup: string) => {
446
+
447
+ const groupClause: any = {
448
+ _id: `$${fieldToGroup}`
449
+ }
450
+
451
+ sumFields.forEach((sumField: string) => {
452
+ groupClause[sumField] = { $sum: { $ifNull: [`$${sumField}`, 0] } }
453
+ })
454
+
455
+ countFields.forEach((countField: string) => {
456
+ groupClause[countField] = { $sum: 1 }
457
+ })
458
+
459
+ groupsClauses.push(groupClause)
460
+ })
461
+ }
440
462
  }
441
463
 
442
464
  export default SearcherFlow