c2-mongoose 2.1.122 → 2.1.124

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.
@@ -23,18 +23,10 @@ declare abstract class SearchFlow {
23
23
  buildPath(target: string, nested?: string): any;
24
24
  buildOrdenation(): any;
25
25
  buildRegex(searchText: string): RegExp;
26
+ searchNoPageable(model: mongoose.Model<any>, options: SearchOptions): Promise<any>;
26
27
  searchPageable(model: mongoose.Model<any>, options: SearchOptions): Promise<any>;
27
28
  transformObject(originalObject: any): any;
28
- search(model: mongoose.Model<any>, options?: SearchOptions): Promise<SearchResponse<any>>;
29
- /**
30
- *
31
- * @param model
32
- * @deprecated
33
- * @returns
34
- */
35
- private searchPageableOld;
36
- private searchNoPageable;
37
- private result;
29
+ search(model: mongoose.Model<any>, options: SearchOptions): Promise<SearchResponse<any>>;
38
30
  count(model: mongoose.Model<any>): Promise<number>;
39
31
  findOne(repository: mongoose.Model<any>, model: any, session?: ClientSession): Promise<any>;
40
32
  sumBy(model: mongoose.Model<any>, _sum: any, _by: any, items?: any, session?: ClientSession): Promise<any[]>;
@@ -52,39 +52,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
52
52
  Object.defineProperty(exports, "__esModule", { value: true });
53
53
  var moment_1 = __importDefault(require("moment"));
54
54
  var mongoose_1 = require("mongoose");
55
- var Environment_1 = require("../configuration/Environment");
56
55
  var Utils_1 = require("../utils/Utils");
57
56
  var SearchFlow = /** @class */ (function () {
58
57
  function SearchFlow(params) {
59
- var _this = this;
60
- /**
61
- *
62
- * @param model
63
- * @deprecated
64
- * @returns
65
- */
66
- this.searchPageableOld = function (model, session) { return __awaiter(_this, void 0, void 0, function () {
67
- var sort, items;
68
- return __generator(this, function (_a) {
69
- switch (_a.label) {
70
- case 0:
71
- sort = this.buildOrdenation();
72
- return [4 /*yield*/, model
73
- .find(this.filters, this.select)
74
- .populate(this.populate)
75
- .skip((this.page - 1) * this.limit)
76
- .limit(this.limit)
77
- .sort(sort)
78
- .session(session)
79
- .collation({
80
- locale: Environment_1.dbCollation || "pt"
81
- })];
82
- case 1:
83
- items = _a.sent();
84
- return [2 /*return*/, this.result(model, items)];
85
- }
86
- });
87
- }); };
88
58
  this.searchText = params.searchText || "";
89
59
  this.order = params.order || "asc";
90
60
  this.orderBy = params.orderBy || "_id";
@@ -184,9 +154,67 @@ var SearchFlow = /** @class */ (function () {
184
154
  .replace(/[u|ü|ú|ù|U|Ú|Ü|Ù]/g, '[u,ü,ú,ù,U,Ú,Ü,Ù]');
185
155
  return new RegExp("".concat(regexExpression), 'i');
186
156
  };
157
+ SearchFlow.prototype.searchNoPageable = function (model, options) {
158
+ return __awaiter(this, void 0, void 0, function () {
159
+ var stagesItems, stagesMetadata, facet, _i, stagesMetadata_1, metadata, result, items, _a, _b, populate, resultAux;
160
+ return __generator(this, function (_c) {
161
+ switch (_c.label) {
162
+ case 0:
163
+ stagesItems = [{ $match: this.filters }];
164
+ if ((0, Utils_1.isNotEmpty)(options.unions)) {
165
+ stagesItems.push.apply(stagesItems, options.unions);
166
+ }
167
+ if ((0, Utils_1.isNotEmpty)(this.projection)) {
168
+ stagesItems.push({ $project: this.projection });
169
+ }
170
+ if ((0, Utils_1.isNotEmpty)(options.pipelines)) {
171
+ stagesItems.push.apply(stagesItems, options.pipelines);
172
+ }
173
+ stagesItems.push({ $sort: this.sort });
174
+ stagesItems.push({ $skip: ((this.page - 1) * this.limit) || 0 });
175
+ stagesItems.push({ $limit: this.limit });
176
+ stagesMetadata = [];
177
+ if ((0, Utils_1.isNotEmpty)(options.metadata)) {
178
+ stagesMetadata.push.apply(stagesMetadata, options.metadata);
179
+ }
180
+ facet = {
181
+ items: stagesItems
182
+ };
183
+ for (_i = 0, stagesMetadata_1 = stagesMetadata; _i < stagesMetadata_1.length; _i++) {
184
+ metadata = stagesMetadata_1[_i];
185
+ facet[metadata.id] = metadata.conditions;
186
+ }
187
+ return [4 /*yield*/, model.aggregate([
188
+ {
189
+ $facet: facet
190
+ },
191
+ ]).session(options === null || options === void 0 ? void 0 : options.session)];
192
+ case 1:
193
+ result = _c.sent();
194
+ items = result[0].items;
195
+ if (!((0, Utils_1.isNotEmpty)(this.populate) && Array.isArray(this.populate))) return [3 /*break*/, 5];
196
+ _a = 0, _b = this.populate;
197
+ _c.label = 2;
198
+ case 2:
199
+ if (!(_a < _b.length)) return [3 /*break*/, 5];
200
+ populate = _b[_a];
201
+ return [4 /*yield*/, model.populate(result[0].items, populate)];
202
+ case 3:
203
+ items = _c.sent();
204
+ _c.label = 4;
205
+ case 4:
206
+ _a++;
207
+ return [3 /*break*/, 2];
208
+ case 5:
209
+ resultAux = this.transformObject(result[0]);
210
+ return [2 /*return*/, resultAux];
211
+ }
212
+ });
213
+ });
214
+ };
187
215
  SearchFlow.prototype.searchPageable = function (model, options) {
188
216
  return __awaiter(this, void 0, void 0, function () {
189
- var stagesItems, stagesPaging, stagesMetadata, facet, _i, stagesMetadata_1, metadata, result, items, _a, _b, populate, resultAux;
217
+ var stagesItems, stagesPaging, stagesMetadata, facet, _i, stagesMetadata_2, metadata, result, items, _a, _b, populate, resultAux;
190
218
  return __generator(this, function (_c) {
191
219
  switch (_c.label) {
192
220
  case 0:
@@ -217,8 +245,8 @@ var SearchFlow = /** @class */ (function () {
217
245
  items: stagesItems,
218
246
  paging: stagesPaging,
219
247
  };
220
- for (_i = 0, stagesMetadata_1 = stagesMetadata; _i < stagesMetadata_1.length; _i++) {
221
- metadata = stagesMetadata_1[_i];
248
+ for (_i = 0, stagesMetadata_2 = stagesMetadata; _i < stagesMetadata_2.length; _i++) {
249
+ metadata = stagesMetadata_2[_i];
222
250
  facet[metadata.id] = metadata.conditions;
223
251
  }
224
252
  return [4 /*yield*/, model.aggregate([
@@ -244,13 +272,7 @@ var SearchFlow = /** @class */ (function () {
244
272
  return [3 /*break*/, 2];
245
273
  case 5:
246
274
  resultAux = this.transformObject(result[0]);
247
- return [2 /*return*/, resultAux
248
- // return {
249
- // ...result[0],
250
- // items,
251
- // paging: result[0]?.paging[0] || { total: 0, page: 1, limit: this.limit },
252
- // }
253
- ];
275
+ return [2 /*return*/, resultAux];
254
276
  }
255
277
  });
256
278
  });
@@ -260,7 +282,7 @@ var SearchFlow = /** @class */ (function () {
260
282
  var transformedObject = {};
261
283
  for (var key in originalObject) {
262
284
  if (key.startsWith('metadata-')) {
263
- metadata[key.replace("metadata-", "")] = originalObject[key];
285
+ metadata[key.replace("metadata-", "")] = originalObject[key][0];
264
286
  }
265
287
  else {
266
288
  transformedObject[key] = originalObject[key];
@@ -273,59 +295,11 @@ var SearchFlow = /** @class */ (function () {
273
295
  return __generator(this, function (_a) {
274
296
  switch (_a.label) {
275
297
  case 0:
276
- if (!(this.pageable == true)) return [3 /*break*/, 4];
277
- if (!(options === null || options === void 0 ? void 0 : options.pipelines)) return [3 /*break*/, 2];
298
+ if (!(this.pageable == true)) return [3 /*break*/, 2];
278
299
  return [4 /*yield*/, this.searchPageable(model, options)];
279
300
  case 1: return [2 /*return*/, _a.sent()];
280
- case 2: return [4 /*yield*/, this.searchPageableOld(model, options === null || options === void 0 ? void 0 : options.session)];
301
+ case 2: return [4 /*yield*/, this.searchNoPageable(model, options)];
281
302
  case 3: return [2 /*return*/, _a.sent()];
282
- case 4: return [4 /*yield*/, this.searchNoPageable(model, options === null || options === void 0 ? void 0 : options.session)];
283
- case 5: return [2 /*return*/, _a.sent()];
284
- }
285
- });
286
- });
287
- };
288
- SearchFlow.prototype.searchNoPageable = function (model, session) {
289
- return __awaiter(this, void 0, void 0, function () {
290
- var sort, items;
291
- return __generator(this, function (_a) {
292
- switch (_a.label) {
293
- case 0:
294
- sort = this.buildOrdenation();
295
- return [4 /*yield*/, model
296
- .find(this.filters, this.select)
297
- .populate(this.populate)
298
- .sort(sort)
299
- .session(session)
300
- .collation({
301
- locale: Environment_1.dbCollation || "pt"
302
- })];
303
- case 1:
304
- items = _a.sent();
305
- return [2 /*return*/, this.result(model, items)];
306
- }
307
- });
308
- });
309
- };
310
- SearchFlow.prototype.result = function (model, items) {
311
- return __awaiter(this, void 0, void 0, function () {
312
- var searchResponse, total, paging;
313
- return __generator(this, function (_a) {
314
- switch (_a.label) {
315
- case 0:
316
- searchResponse = {};
317
- searchResponse.items = items;
318
- if (!(this.pageable == true)) return [3 /*break*/, 2];
319
- return [4 /*yield*/, this.count(model)];
320
- case 1:
321
- total = _a.sent();
322
- paging = {};
323
- paging.total = total;
324
- paging.page = Number(this.page);
325
- paging.limit = Number(this.limit);
326
- searchResponse.paging = paging;
327
- _a.label = 2;
328
- case 2: return [2 /*return*/, searchResponse];
329
303
  }
330
304
  });
331
305
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-mongoose",
3
- "version": "2.1.122",
3
+ "version": "2.1.124",
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",
@@ -122,6 +122,60 @@ abstract class SearchFlow {
122
122
  return new RegExp(`${regexExpression}`, 'i');
123
123
  }
124
124
 
125
+ async searchNoPageable(model: mongoose.Model<any>, options: SearchOptions) {
126
+
127
+ let stagesItems: any[] = [{ $match: this.filters }]
128
+
129
+ if (isNotEmpty(options.unions)) {
130
+ stagesItems.push(...options.unions)
131
+ }
132
+
133
+ if (isNotEmpty(this.projection)) {
134
+ stagesItems.push({ $project: this.projection })
135
+ }
136
+
137
+ if (isNotEmpty(options.pipelines)) {
138
+ stagesItems.push(...options.pipelines)
139
+ }
140
+
141
+ stagesItems.push({ $sort: this.sort })
142
+ stagesItems.push({ $skip: ((this.page - 1) * this.limit) || 0 })
143
+ stagesItems.push({ $limit: this.limit })
144
+
145
+ let stagesMetadata: any[] = []
146
+ if (isNotEmpty(options.metadata)) {
147
+ stagesMetadata.push(...options.metadata)
148
+ }
149
+
150
+
151
+ const facet: { [key: string]: any } = {
152
+ items: stagesItems
153
+ }
154
+
155
+ for (let metadata of stagesMetadata) {
156
+ facet[metadata.id] = metadata.conditions
157
+ }
158
+
159
+ const result = await model.aggregate(
160
+ [
161
+ {
162
+ $facet: facet
163
+ },
164
+ ]
165
+ ).session(options?.session as ClientSession)
166
+
167
+ var items = result[0].items
168
+
169
+ if (isNotEmpty(this.populate) && Array.isArray(this.populate)) {
170
+ for (var populate of this.populate) {
171
+ items = await model.populate(result[0].items, populate)
172
+ }
173
+ }
174
+
175
+ let resultAux = this.transformObject(result[0])
176
+ return resultAux
177
+ }
178
+
125
179
  async searchPageable(model: mongoose.Model<any>, options: SearchOptions) {
126
180
 
127
181
  let stagesItems: any[] = [{ $match: this.filters }]
@@ -185,25 +239,20 @@ abstract class SearchFlow {
185
239
 
186
240
  let resultAux = this.transformObject(result[0])
187
241
  return resultAux
188
- // return {
189
- // ...result[0],
190
- // items,
191
- // paging: result[0]?.paging[0] || { total: 0, page: 1, limit: this.limit },
192
- // }
193
242
  }
194
243
 
195
244
  transformObject(originalObject: any): any {
196
245
  const metadata: any = {};
197
246
  const transformedObject: any = {};
198
-
247
+
199
248
  for (const key in originalObject) {
200
249
  if (key.startsWith('metadata-')) {
201
- metadata[key.replace("metadata-", "")] = originalObject[key];
250
+ metadata[key.replace("metadata-", "")] = originalObject[key][0];
202
251
  } else {
203
252
  transformedObject[key] = originalObject[key];
204
253
  }
205
254
  }
206
-
255
+
207
256
  return {
208
257
  ...transformedObject,
209
258
  metadata,
@@ -211,67 +260,12 @@ abstract class SearchFlow {
211
260
  };
212
261
  }
213
262
 
214
- public async search(model: mongoose.Model<any>, options?: SearchOptions): Promise<SearchResponse<any>> {
215
- if (this.pageable == true) {
216
- if (options?.pipelines) {
217
- return await this.searchPageable(model, options)
218
- }
219
- return await this.searchPageableOld(model, options?.session)
220
- }
221
-
222
- return await this.searchNoPageable(model, options?.session)
223
- }
224
-
225
- /**
226
- *
227
- * @param model
228
- * @deprecated
229
- * @returns
230
- */
231
- private searchPageableOld = async (model: mongoose.Model<any>, session?: ClientSession): Promise<SearchResponse<any>> => {
232
- const sort = this.buildOrdenation()
233
- var items = await model
234
- .find(this.filters, this.select)
235
- .populate(this.populate)
236
- .skip((this.page - 1) * this.limit)
237
- .limit(this.limit)
238
- .sort(sort)
239
- .session(session!)
240
- .collation({
241
- locale: dbCollation || "pt"
242
- }) as []
243
-
244
- return this.result(model, items)
245
- }
246
-
247
- private async searchNoPageable(model: mongoose.Model<any>, session?: ClientSession): Promise<SearchResponse<any>> {
248
- const sort = this.buildOrdenation()
249
- var items = await model
250
- .find(this.filters, this.select)
251
- .populate(this.populate)
252
- .sort(sort)
253
- .session(session!)
254
- .collation({
255
- locale: dbCollation || "pt"
256
- }) as []
257
-
258
- return this.result(model, items)
259
- }
260
-
261
- private async result(model: mongoose.Model<any>, items: []): Promise<SearchResponse<any>> {
262
- var searchResponse: SearchResponse<any> = {}
263
- searchResponse.items = items
264
-
263
+ public async search(model: mongoose.Model<any>, options: SearchOptions): Promise<SearchResponse<any>> {
265
264
  if (this.pageable == true) {
266
- var total = await this.count(model)
267
- var paging: Pagination = {}
268
- paging.total = total
269
- paging.page = Number(this.page)
270
- paging.limit = Number(this.limit)
271
- searchResponse.paging = paging
265
+ return await this.searchPageable(model, options)
272
266
  }
273
267
 
274
- return searchResponse
268
+ return await this.searchNoPageable(model, options)
275
269
  }
276
270
 
277
271
  public async count(model: mongoose.Model<any>): Promise<number> {