c2-mongoose 2.1.283 → 2.1.285

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.
@@ -31,6 +31,7 @@ declare class SearcherFlow<D> {
31
31
  extractFieldsOfType(schema: Schema, typing?: string, parent?: string): string[];
32
32
  buildCondition(key: string, value: any): any;
33
33
  buildDefaultFilters(objectSearch: any): any;
34
+ normalizeValue(key: string, value: any): any;
34
35
  isValidObjectId(value: string): boolean;
35
36
  buildOrdenation(): any;
36
37
  buildRegex(searchText: string): RegExp;
@@ -304,7 +304,7 @@ var SearcherFlow = /** @class */ (function () {
304
304
  var _this = this;
305
305
  // Recuperar todos os campos do tipo String
306
306
  var camposString = (0, Utils_1.isEmpty)(this.searchTextFields) ? this.extractFieldsOfType(this.model.schema, 'String') : this.searchTextFields;
307
- var filters = { $and: [] };
307
+ var andQueriesParam = [];
308
308
  Object.entries(objectSearch).forEach(function (_a) {
309
309
  var key = _a[0], value = _a[1];
310
310
  if ((0, Utils_1.isNotEmpty)(value) || value === null) {
@@ -341,7 +341,7 @@ var SearcherFlow = /** @class */ (function () {
341
341
  value = false;
342
342
  }
343
343
  var condition = _this.buildCondition(key, value);
344
- filters.$and.push(condition);
344
+ andQueriesParam.push(condition);
345
345
  }
346
346
  });
347
347
  if ((0, Utils_1.isNotEmpty)(this.searchText) && (0, Utils_1.isNotEmpty)(camposString)) {
@@ -353,34 +353,58 @@ var SearcherFlow = /** @class */ (function () {
353
353
  conditionAux[fieldString] = { $regex: regex };
354
354
  conditions.$or.push(conditionAux);
355
355
  }
356
- filters.$and.push(conditions);
356
+ andQueriesParam.push(conditions);
357
357
  }
358
- if (filters.$and.length === 0)
359
- delete filters['$and'];
358
+ // if (filters.$and.length === 0)
359
+ // delete filters['$and']
360
+ var orPayload = [];
360
361
  if (objectSearch === null || objectSearch === void 0 ? void 0 : objectSearch.or) {
361
- for (var _b = 0, _c = objectSearch === null || objectSearch === void 0 ? void 0 : objectSearch.or; _b < _c.length; _b++) {
362
- var clause = _c[_b];
363
- if (!filters.$or)
364
- filters.$or = [];
365
- Object.entries(clause).forEach(function (_a) {
366
- var key = _a[0], value = _a[1];
367
- if ((0, Utils_1.isEmpty)(value))
368
- return;
369
- if (typeof value === 'string' && _this.isValidObjectId(value) && key !== 'owner') {
370
- value = new mongoose_1.Types.ObjectId(value);
371
- }
372
- if (value === 'true') {
373
- value = true;
374
- }
375
- if (value === 'false') {
376
- value = false;
362
+ var _loop_1 = function (clauseOr) {
363
+ if (clauseOr.and && Array.isArray(clauseOr.and)) {
364
+ var and_1 = [];
365
+ for (var _d = 0, _e = clauseOr.and; _d < _e.length; _d++) {
366
+ var clauseAnd = _e[_d];
367
+ Object.entries(clauseAnd).forEach(function (_a) {
368
+ var key = _a[0], value = _a[1];
369
+ if ((0, Utils_1.isEmpty)(value))
370
+ return;
371
+ value = _this.normalizeValue(key, value);
372
+ var condition = _this.buildCondition(key, value);
373
+ and_1.push(condition);
374
+ });
377
375
  }
378
- var condition = _this.buildCondition(key, value);
379
- filters.$or.push(condition);
380
- });
376
+ orPayload.push(and_1);
377
+ }
378
+ };
379
+ for (var _b = 0, _c = objectSearch === null || objectSearch === void 0 ? void 0 : objectSearch.or; _b < _c.length; _b++) {
380
+ var clauseOr = _c[_b];
381
+ _loop_1(clauseOr);
381
382
  }
382
383
  }
383
- return filters;
384
+ var filtersFinal = {};
385
+ if (andQueriesParam.length > 0) {
386
+ if (!filtersFinal.$or)
387
+ filtersFinal.$or = [];
388
+ filtersFinal.$or = __spreadArray(__spreadArray([], filtersFinal.$or, true), [{ $and: __spreadArray([], andQueriesParam, true) }], false);
389
+ }
390
+ if (orPayload.length > 0) {
391
+ if (!filtersFinal.$or)
392
+ filtersFinal.$or = [];
393
+ filtersFinal.$or = __spreadArray(__spreadArray([], filtersFinal.$or, true), [{ $and: __spreadArray([], orPayload, true) }], false);
394
+ }
395
+ return filtersFinal;
396
+ };
397
+ SearcherFlow.prototype.normalizeValue = function (key, value) {
398
+ if (typeof value === 'string' && this.isValidObjectId(value) && key !== 'owner') {
399
+ value = new mongoose_1.Types.ObjectId(value);
400
+ }
401
+ if (value === 'true') {
402
+ value = true;
403
+ }
404
+ if (value === 'false') {
405
+ value = false;
406
+ }
407
+ return value;
384
408
  };
385
409
  SearcherFlow.prototype.isValidObjectId = function (value) {
386
410
  var objectIdPattern = /^[0-9a-fA-F]{24}$/;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-mongoose",
3
- "version": "2.1.283",
3
+ "version": "2.1.285",
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",
@@ -285,7 +285,7 @@ class SearcherFlow<D> {
285
285
  const camposString = isEmpty(this.searchTextFields) ? this.extractFieldsOfType(this.model.schema, 'String') : this.searchTextFields
286
286
 
287
287
 
288
- let filters = { $and: [] } as any
288
+ const andQueriesParam = []
289
289
  Object.entries(objectSearch).forEach(([key, value]) => {
290
290
  if (isNotEmpty(value) || value === null) {
291
291
  if ([
@@ -324,7 +324,7 @@ class SearcherFlow<D> {
324
324
  }
325
325
 
326
326
  const condition = this.buildCondition(key, value)
327
- filters.$and.push(condition)
327
+ andQueriesParam.push(condition)
328
328
  }
329
329
  })
330
330
 
@@ -337,40 +337,60 @@ class SearcherFlow<D> {
337
337
 
338
338
  conditions.$or.push(conditionAux)
339
339
  }
340
- filters.$and.push(conditions)
340
+ andQueriesParam.push(conditions)
341
341
  }
342
342
 
343
- if (filters.$and.length === 0)
344
- delete filters['$and']
343
+ // if (filters.$and.length === 0)
344
+ // delete filters['$and']
345
345
 
346
+ const orPayload: any[] = []
346
347
  if (objectSearch?.or) {
347
- for (const clause of objectSearch?.or) {
348
- if (!filters.$or) filters.$or = []
349
-
350
- Object.entries(clause).forEach(([key, value]) => {
351
-
352
- if (isEmpty(value)) return
353
-
354
- if (typeof value === 'string' && this.isValidObjectId(value as string) && key !== 'owner') {
355
- value = new Types.ObjectId(value as string)
348
+ for (const clauseOr of objectSearch?.or) {
349
+ if (clauseOr.and && Array.isArray(clauseOr.and)) {
350
+ const and: any[] = []
351
+ for (const clauseAnd of clauseOr.and) {
352
+ Object.entries(clauseAnd).forEach(([key, value]) => {
353
+ if (isEmpty(value)) return
354
+ value = this.normalizeValue(key, value)
355
+ const condition = this.buildCondition(key, value)
356
+ and.push(condition)
357
+ })
356
358
  }
357
-
358
- if (value === 'true') {
359
- value = true
360
- }
361
-
362
- if (value === 'false') {
363
- value = false
364
- }
365
-
366
- const condition = this.buildCondition(key, value)
367
- filters.$or.push(condition)
368
- })
359
+ orPayload.push(and)
360
+ }
369
361
  }
370
- }
362
+ }
363
+
364
+ const filtersFinal = {} as any
365
+
366
+ if (andQueriesParam.length > 0) {
367
+ if (!filtersFinal.$or) filtersFinal.$or = []
368
+ filtersFinal.$or = [...filtersFinal.$or, { $and: [...andQueriesParam] }]
369
+ }
370
+
371
+ if (orPayload.length > 0) {
372
+ if (!filtersFinal.$or) filtersFinal.$or = []
373
+ filtersFinal.$or = [...filtersFinal.$or, { $and: [...orPayload] }]
374
+ }
375
+
371
376
 
377
+ return filtersFinal
378
+ }
379
+
380
+ public normalizeValue(key: string, value: any): any {
381
+ if (typeof value === 'string' && this.isValidObjectId(value as string) && key !== 'owner') {
382
+ value = new Types.ObjectId(value as string)
383
+ }
384
+
385
+ if (value === 'true') {
386
+ value = true
387
+ }
388
+
389
+ if (value === 'false') {
390
+ value = false
391
+ }
372
392
 
373
- return filters
393
+ return value
374
394
  }
375
395
 
376
396
  public isValidObjectId(value: string) {