c2-mongoose 2.1.31 → 2.1.33

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.
@@ -15,6 +15,6 @@ declare class CrudFlow<D> {
15
15
  getOne(model: D, params?: any): Promise<D>;
16
16
  get(id: string, pop?: string, sel?: string, session?: ClientSession): Promise<D>;
17
17
  getById(id: string, session?: ClientSession): Promise<Partial<D>>;
18
- sumBy(fieldToSum: string, fieldToGroup: string): Promise<any>;
18
+ sumBy(fieldToSum: any, fieldToGroup: any, addToSet: any): Promise<any>;
19
19
  }
20
20
  export default CrudFlow;
@@ -169,11 +169,11 @@ var CrudFlow = /** @class */ (function () {
169
169
  });
170
170
  });
171
171
  };
172
- CrudFlow.prototype.sumBy = function (fieldToSum, fieldToGroup) {
172
+ CrudFlow.prototype.sumBy = function (fieldToSum, fieldToGroup, addToSet) {
173
173
  return __awaiter(this, void 0, void 0, function () {
174
174
  return __generator(this, function (_a) {
175
175
  switch (_a.label) {
176
- case 0: return [4 /*yield*/, this.search.sumBy(this.repository, fieldToSum, fieldToGroup)];
176
+ case 0: return [4 /*yield*/, this.search.sumBy(this.repository, fieldToSum, fieldToGroup, addToSet)];
177
177
  case 1: return [2 /*return*/, _a.sent()];
178
178
  }
179
179
  });
@@ -21,7 +21,7 @@ declare abstract class SearchFlow {
21
21
  private result;
22
22
  count(model: mongoose.Model<any>): Promise<number>;
23
23
  findOne(repository: mongoose.Model<any>, model: any, params?: any): Promise<any>;
24
- sumBy(model: mongoose.Model<any>, _sum: any, _by: any, session?: ClientSession): Promise<any[]>;
24
+ sumBy(model: mongoose.Model<any>, _sum: any, _by: any, _addSet?: any, session?: ClientSession): Promise<any[]>;
25
25
  buildDefaultFilters(objectSearch: any): any;
26
26
  addFilterModel(model: any, filters: any): void;
27
27
  }
@@ -210,23 +210,30 @@ var SearchFlow = /** @class */ (function () {
210
210
  });
211
211
  });
212
212
  };
213
- SearchFlow.prototype.sumBy = function (model, _sum, _by, session) {
213
+ SearchFlow.prototype.sumBy = function (model, _sum, _by, _addSet, session) {
214
+ if (_addSet === void 0) { _addSet = undefined; }
214
215
  return __awaiter(this, void 0, void 0, function () {
215
- var ret;
216
+ var sort, ret;
216
217
  return __generator(this, function (_a) {
217
218
  switch (_a.label) {
218
- case 0: return [4 /*yield*/, model.aggregate([
219
- {
220
- '$match': this.filters
221
- },
222
- {
223
- $group: {
224
- _id: _by,
225
- totalValue: { "$sum": _sum },
226
- count: { "$sum": 1 }
219
+ case 0:
220
+ sort = this.buildOrdenation();
221
+ return [4 /*yield*/, model.aggregate([
222
+ {
223
+ '$match': this.filters
224
+ },
225
+ {
226
+ $group: {
227
+ _id: _by,
228
+ totalValue: { "$sum": _sum },
229
+ count: { "$sum": 1 },
230
+ items: _addSet
231
+ }
232
+ },
233
+ {
234
+ $sort: sort
227
235
  }
228
- }
229
- ]).session(session)];
236
+ ]).session(session)];
230
237
  case 1:
231
238
  ret = _a.sent();
232
239
  return [2 /*return*/, ret];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-mongoose",
3
- "version": "2.1.31",
3
+ "version": "2.1.33",
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",
@@ -65,8 +65,8 @@ class CrudFlow<D> {
65
65
  return data as D
66
66
  }
67
67
 
68
- public async sumBy(fieldToSum: string, fieldToGroup: string): Promise<any> {
69
- return await this.search.sumBy(this.repository, fieldToSum, fieldToGroup)
68
+ public async sumBy(fieldToSum: any, fieldToGroup: any, addToSet: any): Promise<any> {
69
+ return await this.search.sumBy(this.repository, fieldToSum, fieldToGroup, addToSet)
70
70
  }
71
71
  }
72
72
 
@@ -134,7 +134,8 @@ abstract class SearchFlow {
134
134
  .populate(params.populate)
135
135
  }
136
136
 
137
- public async sumBy(model: mongoose.Model<any>, _sum: any, _by: any, session?: ClientSession): Promise<any[]> {
137
+ public async sumBy(model: mongoose.Model<any>, _sum: any, _by: any, _addSet: any = undefined, session?: ClientSession): Promise<any[]> {
138
+ const sort = this.buildOrdenation()
138
139
  const ret = await model.aggregate(
139
140
  [
140
141
  {
@@ -144,8 +145,12 @@ abstract class SearchFlow {
144
145
  $group: {
145
146
  _id: _by,
146
147
  totalValue: { "$sum": _sum },
147
- count: { "$sum": 1 }
148
+ count: { "$sum": 1 },
149
+ items: _addSet
148
150
  }
151
+ },
152
+ {
153
+ $sort: sort
149
154
  }
150
155
  ]
151
156
  ).session(session!)