c2-mongoose 2.1.102 → 2.1.104

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.
@@ -26,6 +26,7 @@ declare abstract class SearchFlow {
26
26
  searchPageable(model: mongoose.Model<any>, options: SearchOptions): Promise<{
27
27
  items: any;
28
28
  paging: any;
29
+ metadata: any;
29
30
  }>;
30
31
  search(model: mongoose.Model<any>, options?: SearchOptions): Promise<SearchResponse<any>>;
31
32
  /**
@@ -185,11 +185,11 @@ var SearchFlow = /** @class */ (function () {
185
185
  return new RegExp("".concat(regexExpression), 'i');
186
186
  };
187
187
  SearchFlow.prototype.searchPageable = function (model, options) {
188
- var _a;
188
+ var _a, _b;
189
189
  return __awaiter(this, void 0, void 0, function () {
190
- var stagesItems, stagesPaging, result, items, _i, _b, populate;
191
- return __generator(this, function (_c) {
192
- switch (_c.label) {
190
+ var stagesItems, stagesPaging, stagesMetadata, result, items, _i, _c, populate;
191
+ return __generator(this, function (_d) {
192
+ switch (_d.label) {
193
193
  case 0:
194
194
  stagesItems = [{ $match: this.filters }];
195
195
  if ((0, Utils_1.isNotEmpty)(options.unions)) {
@@ -210,33 +210,39 @@ var SearchFlow = /** @class */ (function () {
210
210
  }
211
211
  stagesPaging.push({ $count: "total" });
212
212
  stagesPaging.push({ $addFields: { page: this.page || 1, limit: this.limit } });
213
+ stagesMetadata = [];
214
+ if ((0, Utils_1.isNotEmpty)(options.metadata)) {
215
+ stagesMetadata.push.apply(stagesMetadata, options.metadata);
216
+ }
213
217
  return [4 /*yield*/, model.aggregate([
214
218
  {
215
219
  $facet: {
216
220
  items: stagesItems,
217
- paging: stagesPaging
221
+ paging: stagesPaging,
222
+ metadata: stagesMetadata
218
223
  }
219
224
  }
220
225
  ]).session(options === null || options === void 0 ? void 0 : options.session)];
221
226
  case 1:
222
- result = _c.sent();
227
+ result = _d.sent();
223
228
  items = result[0].items;
224
229
  if (!((0, Utils_1.isNotEmpty)(this.populate) && Array.isArray(this.populate))) return [3 /*break*/, 5];
225
- _i = 0, _b = this.populate;
226
- _c.label = 2;
230
+ _i = 0, _c = this.populate;
231
+ _d.label = 2;
227
232
  case 2:
228
- if (!(_i < _b.length)) return [3 /*break*/, 5];
229
- populate = _b[_i];
233
+ if (!(_i < _c.length)) return [3 /*break*/, 5];
234
+ populate = _c[_i];
230
235
  return [4 /*yield*/, model.populate(result[0].items, populate)];
231
236
  case 3:
232
- items = _c.sent();
233
- _c.label = 4;
237
+ items = _d.sent();
238
+ _d.label = 4;
234
239
  case 4:
235
240
  _i++;
236
241
  return [3 /*break*/, 2];
237
242
  case 5: return [2 /*return*/, {
238
243
  items: items,
239
- paging: ((_a = result[0]) === null || _a === void 0 ? void 0 : _a.paging[0]) || { total: 0, page: 1, limit: this.limit }
244
+ paging: ((_a = result[0]) === null || _a === void 0 ? void 0 : _a.paging[0]) || { total: 0, page: 1, limit: this.limit },
245
+ metadata: ((_b = result[0]) === null || _b === void 0 ? void 0 : _b.metadata[0]) || undefined
240
246
  }];
241
247
  }
242
248
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-mongoose",
3
- "version": "2.1.102",
3
+ "version": "2.1.104",
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",
@@ -123,15 +123,6 @@ abstract class SearchFlow {
123
123
  }
124
124
 
125
125
  async searchPageable(model: mongoose.Model<any>, options: SearchOptions) {
126
- // let stagesItems = [
127
- // { $match: this.filters },
128
- // isNotEmpty(options.unions) ? options.unions : undefined,
129
- // this.projection ? { $project: this.projection } : { $addFields: {} },
130
- // ...options.pipelines,
131
- // { $sort: this.sort },
132
- // { $skip: ((this.page - 1) * this.limit) || 0 },
133
- // { $limit: this.limit },
134
- // ]
135
126
 
136
127
  let stagesItems: any[] = [{ $match: this.filters }]
137
128
 
@@ -159,13 +150,18 @@ abstract class SearchFlow {
159
150
  stagesPaging.push({ $count: "total" })
160
151
  stagesPaging.push({ $addFields: { page: this.page || 1, limit: this.limit } })
161
152
 
153
+ let stagesMetadata: any[] = []
154
+ if (isNotEmpty(options.metadata)) {
155
+ stagesMetadata.push(...options.metadata)
156
+ }
162
157
 
163
158
  const result = await model.aggregate(
164
159
  [
165
160
  {
166
161
  $facet: {
167
162
  items: stagesItems,
168
- paging: stagesPaging
163
+ paging: stagesPaging,
164
+ metadata: stagesMetadata
169
165
  }
170
166
  }
171
167
  ]
@@ -181,7 +177,8 @@ abstract class SearchFlow {
181
177
 
182
178
  return {
183
179
  items,
184
- paging: result[0]?.paging[0] || { total: 0, page: 1, limit: this.limit }
180
+ paging: result[0]?.paging[0] || { total: 0, page: 1, limit: this.limit },
181
+ metadata: result[0]?.metadata[0] || undefined
185
182
  }
186
183
  }
187
184
 
@@ -18,6 +18,7 @@ declare interface PipelineStage {
18
18
  declare interface SearchOptions {
19
19
  unions: PipelineStage[],
20
20
  pipelines: PipelineStage[],
21
+ metadata: PipelineStage[],
21
22
  session?: ClientSession
22
23
  }
23
24