c2-mongoose 2.1.36 → 2.1.38

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.
@@ -16,5 +16,6 @@ declare class CrudFlow<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
18
  sumBy(fieldToSum: any, fieldToGroup: any, addToSet?: any): Promise<any>;
19
+ sumByAndPaging(fieldToSum: any, fieldToGroup: any, addToSet?: any): Promise<SearchResponse<any>>;
19
20
  }
20
21
  export default CrudFlow;
@@ -179,6 +179,16 @@ var CrudFlow = /** @class */ (function () {
179
179
  });
180
180
  });
181
181
  };
182
+ CrudFlow.prototype.sumByAndPaging = function (fieldToSum, fieldToGroup, addToSet) {
183
+ return __awaiter(this, void 0, void 0, function () {
184
+ return __generator(this, function (_a) {
185
+ switch (_a.label) {
186
+ case 0: return [4 /*yield*/, this.search.sumByPaging(this.repository, fieldToSum, fieldToGroup, addToSet)];
187
+ case 1: return [2 /*return*/, _a.sent()];
188
+ }
189
+ });
190
+ });
191
+ };
182
192
  return CrudFlow;
183
193
  }());
184
194
  exports.default = CrudFlow;
@@ -22,6 +22,7 @@ declare abstract class SearchFlow {
22
22
  count(model: mongoose.Model<any>): Promise<number>;
23
23
  findOne(repository: mongoose.Model<any>, model: any, params?: any): Promise<any>;
24
24
  sumBy(model: mongoose.Model<any>, _sum: any, _by: any, items?: any, session?: ClientSession): Promise<any[]>;
25
+ sumByPaging(model: mongoose.Model<any>, fieldToSum: any, fieldToGroup: any, items?: any): Promise<SearchResponse<any>>;
25
26
  buildDefaultFilters(objectSearch: any): any;
26
27
  addFilterModel(model: any, filters: any): void;
27
28
  }
@@ -241,6 +241,55 @@ var SearchFlow = /** @class */ (function () {
241
241
  });
242
242
  });
243
243
  };
244
+ SearchFlow.prototype.sumByPaging = function (model, fieldToSum, fieldToGroup, items) {
245
+ if (items === void 0) { items = undefined; }
246
+ return __awaiter(this, void 0, void 0, function () {
247
+ var sort, result;
248
+ return __generator(this, function (_a) {
249
+ switch (_a.label) {
250
+ case 0:
251
+ sort = this.buildOrdenation();
252
+ return [4 /*yield*/, model.aggregate([
253
+ {
254
+ $facet: {
255
+ items: [
256
+ { $match: this.filters },
257
+ {
258
+ $group: {
259
+ _id: fieldToGroup,
260
+ totalValue: { "$sum": fieldToSum },
261
+ count: { "$sum": 1 },
262
+ items: items
263
+ }
264
+ },
265
+ { $sort: sort },
266
+ { $skip: (this.page - 1) * this.limit },
267
+ { $limit: this.limit }
268
+ ],
269
+ paging: [
270
+ { $match: this.filters },
271
+ { $group: { _id: fieldToGroup, count: { $sum: 1 } } },
272
+ { $count: "total" },
273
+ {
274
+ $addFields: {
275
+ page: this.page,
276
+ limit: this.limit
277
+ }
278
+ }
279
+ ]
280
+ }
281
+ }
282
+ ])];
283
+ case 1:
284
+ result = _a.sent();
285
+ return [2 /*return*/, {
286
+ items: result[0].items,
287
+ paging: result[0].paging[0]
288
+ }];
289
+ }
290
+ });
291
+ });
292
+ };
244
293
  SearchFlow.prototype.buildDefaultFilters = function (objectSearch) {
245
294
  var filters = { $and: [] };
246
295
  Object.entries(objectSearch.model).forEach(function (_a) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-mongoose",
3
- "version": "2.1.36",
3
+ "version": "2.1.38",
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",
@@ -68,6 +68,10 @@ class CrudFlow<D> {
68
68
  public async sumBy(fieldToSum: any, fieldToGroup: any, addToSet?: any): Promise<any> {
69
69
  return await this.search.sumBy(this.repository, fieldToSum, fieldToGroup, addToSet)
70
70
  }
71
+
72
+ public async sumByAndPaging(fieldToSum: any, fieldToGroup: any, addToSet?: any): Promise<SearchResponse<any>> {
73
+ return await this.search.sumByPaging(this.repository, fieldToSum, fieldToGroup, addToSet)
74
+ }
71
75
  }
72
76
 
73
77
  export default CrudFlow
@@ -157,6 +157,49 @@ abstract class SearchFlow {
157
157
  return ret
158
158
  }
159
159
 
160
+ async sumByPaging(model: mongoose.Model<any>, fieldToSum: any, fieldToGroup: any, items: any = undefined): Promise<SearchResponse<any>> {
161
+ const sort = this.buildOrdenation()
162
+ const result = await model.aggregate(
163
+ [
164
+ {
165
+ $facet: {
166
+ items: [
167
+ { $match: this.filters },
168
+ {
169
+ $group: {
170
+ _id: fieldToGroup,
171
+ totalValue: { "$sum": fieldToSum },
172
+ count: { "$sum": 1 },
173
+ items: items
174
+ }
175
+ },
176
+ { $sort: sort },
177
+ { $skip: (this.page - 1) * this.limit },
178
+ { $limit: this.limit }
179
+ ],
180
+ paging: [
181
+ { $match: this.filters },
182
+ { $group: { _id: fieldToGroup, count: { $sum: 1 } } },
183
+ { $count: "total" },
184
+ {
185
+ $addFields:
186
+ {
187
+ page: this.page,
188
+ limit: this.limit
189
+ }
190
+ }
191
+ ]
192
+ }
193
+ }
194
+ ]
195
+ )
196
+
197
+ return {
198
+ items: result[0].items,
199
+ paging: result[0].paging[0]
200
+ }
201
+ }
202
+
160
203
  public buildDefaultFilters(objectSearch: any) {
161
204
  let filters = { $and: [] } as any
162
205
  Object.entries(objectSearch.model).forEach(([key, value]) => {
@@ -1,10 +1,12 @@
1
- export declare interface SearchResponse<T> {
1
+ declare interface SearchResponse<T> {
2
2
  paging?: Pagination,
3
3
  items?: T[]
4
4
  }
5
5
 
6
- export declare interface Pagination {
6
+ declare interface Pagination {
7
7
  total?: number,
8
8
  page?: number,
9
9
  limit?: number
10
- }
10
+ }
11
+
12
+ export { SearchResponse, Pagination }