c2-mongoose 2.1.278 → 2.1.279

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.
@@ -29,6 +29,7 @@ declare class SearcherFlow<D> {
29
29
  search(options: SearchOptions): Promise<SearchResponse<D>>;
30
30
  transformObject(originalObject: any): any;
31
31
  extractFieldsOfType(schema: Schema, typing?: string, parent?: string): string[];
32
+ buildCondition(key: string, value: any): any;
32
33
  buildDefaultFilters(objectSearch: any): any;
33
34
  isValidObjectId(value: string): boolean;
34
35
  buildOrdenation(): any;
@@ -211,17 +211,105 @@ var SearcherFlow = /** @class */ (function () {
211
211
  });
212
212
  return fieldsTyped;
213
213
  };
214
+ SearcherFlow.prototype.buildCondition = function (key, value) {
215
+ var _this = this;
216
+ var _a, _b, _c;
217
+ var condition = {};
218
+ if (key.endsWith('DateRange')) {
219
+ var fieldName = key.replace('Range', '');
220
+ var values = value;
221
+ if ((0, Utils_1.isNotEmpty)(values[0])) {
222
+ var momentValue = (0, moment_1.default)(values[0]);
223
+ momentValue.hour(0);
224
+ momentValue.minute(0);
225
+ momentValue.second(0);
226
+ condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $gte: momentValue.tz('America/Sao_Paulo', true).toDate() });
227
+ }
228
+ if ((0, Utils_1.isNotEmpty)(values[1])) {
229
+ var momentValue = (0, moment_1.default)(values[1]);
230
+ momentValue.hour(23);
231
+ momentValue.minute(59);
232
+ momentValue.second(59);
233
+ condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $lte: momentValue.tz('America/Sao_Paulo', true).toDate() });
234
+ }
235
+ }
236
+ else if (key.endsWith('DateTimeRange')) {
237
+ var fieldName = key.replace('Range', '');
238
+ var values = value;
239
+ if ((0, Utils_1.isNotEmpty)(values[0])) {
240
+ var momentValue = (0, moment_1.default)(values[0]);
241
+ momentValue.hour(0);
242
+ momentValue.minute(0);
243
+ momentValue.second(0);
244
+ condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $gte: momentValue.tz('America/Sao_Paulo', true).toDate() });
245
+ }
246
+ if ((0, Utils_1.isNotEmpty)(values[1])) {
247
+ var momentValue = (0, moment_1.default)(values[1]);
248
+ momentValue.hour(23);
249
+ momentValue.minute(59);
250
+ momentValue.second(59);
251
+ condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $lte: momentValue.tz('America/Sao_Paulo', true).toDate() });
252
+ }
253
+ }
254
+ else if (key.endsWith('Like')) {
255
+ var fieldName = key.replace('Like', '');
256
+ condition[fieldName] = this.buildRegex(value); //{ $regex: value as any, $options: 'i' }
257
+ }
258
+ else if (key.endsWith('Exists')) {
259
+ var fieldName = key.replace('Exists', '');
260
+ condition[fieldName] = { $exists: value };
261
+ }
262
+ else {
263
+ if (((_c = (_b = (_a = this.model) === null || _a === void 0 ? void 0 : _a.schema) === null || _b === void 0 ? void 0 : _b.paths[key]) === null || _c === void 0 ? void 0 : _c.instance) === 'Array') {
264
+ if (!Array.isArray(value)) {
265
+ value = [value];
266
+ }
267
+ value === null || value === void 0 ? void 0 : value.forEach(function (v) {
268
+ if (typeof v === 'string' && _this.isValidObjectId(v)) {
269
+ v = new mongoose_1.Types.ObjectId(v);
270
+ }
271
+ });
272
+ if (key.startsWith("notIn")) {
273
+ key = key.replace("notIn", "");
274
+ condition[key] = { $nin: value };
275
+ }
276
+ else {
277
+ condition[key] = { $in: value };
278
+ }
279
+ }
280
+ else if (Array.isArray(value)) {
281
+ var arr = [];
282
+ for (var _i = 0, value_1 = value; _i < value_1.length; _i++) {
283
+ var val = value_1[_i];
284
+ if (typeof val === 'string' && this.isValidObjectId(val)) {
285
+ val = new mongoose_1.Types.ObjectId(val);
286
+ }
287
+ arr.push(val);
288
+ }
289
+ if (key.startsWith("notIn")) {
290
+ key = key.replace("notIn", "");
291
+ condition[key] = { $nin: arr };
292
+ }
293
+ else {
294
+ condition[key] = { $in: arr };
295
+ }
296
+ }
297
+ else {
298
+ condition[key] = value;
299
+ }
300
+ }
301
+ return condition;
302
+ };
214
303
  SearcherFlow.prototype.buildDefaultFilters = function (objectSearch) {
215
304
  var _this = this;
216
305
  // Recuperar todos os campos do tipo String
217
306
  var camposString = (0, Utils_1.isEmpty)(this.searchTextFields) ? this.extractFieldsOfType(this.model.schema, 'String') : this.searchTextFields;
218
307
  var filters = { $and: [] };
219
308
  Object.entries(objectSearch).forEach(function (_a) {
220
- var _b, _c, _d;
221
309
  var key = _a[0], value = _a[1];
222
310
  if ((0, Utils_1.isNotEmpty)(value) || value === null) {
223
- var condition = {};
224
311
  if ([
312
+ 'or',
225
313
  'filters',
226
314
  'withTotalizers',
227
315
  'onlyMetadata',
@@ -252,94 +340,8 @@ var SearcherFlow = /** @class */ (function () {
252
340
  if (value === 'false') {
253
341
  value = false;
254
342
  }
255
- if (key.endsWith('DateRange')) {
256
- var fieldName = key.replace('Range', '');
257
- var values = value;
258
- if ((0, Utils_1.isNotEmpty)(values[0])) {
259
- var momentValue = (0, moment_1.default)(values[0]);
260
- momentValue.hour(0);
261
- momentValue.minute(0);
262
- momentValue.second(0);
263
- condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $gte: momentValue.tz('America/Sao_Paulo', true).toDate() });
264
- }
265
- if ((0, Utils_1.isNotEmpty)(values[1])) {
266
- var momentValue = (0, moment_1.default)(values[1]);
267
- momentValue.hour(23);
268
- momentValue.minute(59);
269
- momentValue.second(59);
270
- condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $lte: momentValue.tz('America/Sao_Paulo', true).toDate() });
271
- }
272
- filters.$and.push(condition);
273
- }
274
- else if (key.endsWith('DateTimeRange')) {
275
- var fieldName = key.replace('Range', '');
276
- var values = value;
277
- if ((0, Utils_1.isNotEmpty)(values[0])) {
278
- var momentValue = (0, moment_1.default)(values[0]);
279
- momentValue.hour(0);
280
- momentValue.minute(0);
281
- momentValue.second(0);
282
- condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $gte: momentValue.tz('America/Sao_Paulo', true).toDate() });
283
- }
284
- if ((0, Utils_1.isNotEmpty)(values[1])) {
285
- var momentValue = (0, moment_1.default)(values[1]);
286
- momentValue.hour(23);
287
- momentValue.minute(59);
288
- momentValue.second(59);
289
- condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $lte: momentValue.tz('America/Sao_Paulo', true).toDate() });
290
- }
291
- filters.$and.push(condition);
292
- }
293
- else if (key.endsWith('Like')) {
294
- var fieldName = key.replace('Like', '');
295
- condition[fieldName] = _this.buildRegex(value); //{ $regex: value as any, $options: 'i' }
296
- filters.$and.push(condition);
297
- }
298
- else if (key.endsWith('Exists')) {
299
- var fieldName = key.replace('Exists', '');
300
- condition[fieldName] = { $exists: value };
301
- filters.$and.push(condition);
302
- }
303
- else {
304
- if (((_d = (_c = (_b = _this.model) === null || _b === void 0 ? void 0 : _b.schema) === null || _c === void 0 ? void 0 : _c.paths[key]) === null || _d === void 0 ? void 0 : _d.instance) === 'Array') {
305
- if (!Array.isArray(value)) {
306
- value = [value];
307
- }
308
- value === null || value === void 0 ? void 0 : value.forEach(function (v) {
309
- if (typeof v === 'string' && _this.isValidObjectId(v)) {
310
- v = new mongoose_1.Types.ObjectId(v);
311
- }
312
- });
313
- if (key.startsWith("notIn")) {
314
- key = key.replace("notIn", "");
315
- condition[key] = { $nin: value };
316
- }
317
- else {
318
- condition[key] = { $in: value };
319
- }
320
- }
321
- else if (Array.isArray(value)) {
322
- var arr = [];
323
- for (var _i = 0, value_1 = value; _i < value_1.length; _i++) {
324
- var val = value_1[_i];
325
- if (typeof val === 'string' && _this.isValidObjectId(val)) {
326
- val = new mongoose_1.Types.ObjectId(val);
327
- }
328
- arr.push(val);
329
- }
330
- if (key.startsWith("notIn")) {
331
- key = key.replace("notIn", "");
332
- condition[key] = { $nin: arr };
333
- }
334
- else {
335
- condition[key] = { $in: arr };
336
- }
337
- }
338
- else {
339
- condition[key] = value;
340
- }
341
- filters.$and.push(condition);
342
- }
343
+ var condition = _this.buildCondition(key, value);
344
+ filters.$and.push(condition);
343
345
  }
344
346
  });
345
347
  if ((0, Utils_1.isNotEmpty)(this.searchText) && (0, Utils_1.isNotEmpty)(camposString)) {
@@ -355,6 +357,23 @@ var SearcherFlow = /** @class */ (function () {
355
357
  }
356
358
  if (filters.$and.length === 0)
357
359
  delete filters['$and'];
360
+ Object.entries(objectSearch === null || objectSearch === void 0 ? void 0 : objectSearch.or).forEach(function (_a) {
361
+ var key = _a[0], value = _a[1];
362
+ console.log(key, value);
363
+ if ((0, Utils_1.isEmpty)(value))
364
+ return;
365
+ if (typeof value === 'string' && _this.isValidObjectId(value) && key !== 'owner') {
366
+ value = new mongoose_1.Types.ObjectId(value);
367
+ }
368
+ if (value === 'true') {
369
+ value = true;
370
+ }
371
+ if (value === 'false') {
372
+ value = false;
373
+ }
374
+ var condition = _this.buildCondition(key, value);
375
+ filters.$or.push(condition);
376
+ });
358
377
  return filters;
359
378
  };
360
379
  SearcherFlow.prototype.isValidObjectId = function (value) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-mongoose",
3
- "version": "2.1.278",
3
+ "version": "2.1.279",
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",
@@ -180,6 +180,105 @@ class SearcherFlow<D> {
180
180
  return fieldsTyped
181
181
  }
182
182
 
183
+ public buildCondition(key: string, value: any) {
184
+ let condition = {} as any
185
+ if (key.endsWith('DateRange')) {
186
+ var fieldName = key.replace('Range', '')
187
+ var values = value as any
188
+ if (isNotEmpty(values[0])) {
189
+ var momentValue = moment(values[0])
190
+ momentValue.hour(0)
191
+ momentValue.minute(0)
192
+ momentValue.second(0)
193
+ condition[fieldName] = {
194
+ ...condition[fieldName],
195
+ $gte: momentValue.tz('America/Sao_Paulo', true).toDate()
196
+ }
197
+ }
198
+
199
+ if (isNotEmpty(values[1])) {
200
+ var momentValue = moment(values[1])
201
+ momentValue.hour(23)
202
+ momentValue.minute(59)
203
+ momentValue.second(59)
204
+ condition[fieldName] = {
205
+ ...condition[fieldName],
206
+ $lte: momentValue.tz('America/Sao_Paulo', true).toDate()
207
+ }
208
+ }
209
+ } else if (key.endsWith('DateTimeRange')) {
210
+ var fieldName = key.replace('Range', '')
211
+ var values = value as any
212
+ if (isNotEmpty(values[0])) {
213
+ var momentValue = moment(values[0])
214
+ momentValue.hour(0)
215
+ momentValue.minute(0)
216
+ momentValue.second(0)
217
+ condition[fieldName] = {
218
+ ...condition[fieldName],
219
+ $gte: momentValue.tz('America/Sao_Paulo', true).toDate()
220
+ }
221
+ }
222
+
223
+ if (isNotEmpty(values[1])) {
224
+ var momentValue = moment(values[1])
225
+ momentValue.hour(23)
226
+ momentValue.minute(59)
227
+ momentValue.second(59)
228
+ condition[fieldName] = {
229
+ ...condition[fieldName],
230
+ $lte: momentValue.tz('America/Sao_Paulo', true).toDate()
231
+ }
232
+ }
233
+ } else if (key.endsWith('Like')) {
234
+ var fieldName = key.replace('Like', '')
235
+ condition[fieldName] = this.buildRegex(value as string) //{ $regex: value as any, $options: 'i' }
236
+ } else if (key.endsWith('Exists')) {
237
+ var fieldName = key.replace('Exists', '')
238
+ condition[fieldName] = { $exists: value as boolean }
239
+ } else {
240
+ if (this.model?.schema?.paths[key]?.instance === 'Array') {
241
+ if (!Array.isArray(value)) {
242
+ value = [value]
243
+ }
244
+
245
+ (value as [])?.forEach((v: any) => {
246
+ if (typeof v === 'string' && this.isValidObjectId(v)) {
247
+ v = new Types.ObjectId(v);
248
+ }
249
+ })
250
+
251
+ if (key.startsWith("notIn")) {
252
+ key = key.replace("notIn", "");
253
+ condition[key] = { $nin: value };
254
+ }
255
+ else {
256
+ condition[key] = { $in: value };
257
+ }
258
+ } else if (Array.isArray(value)) {
259
+ var arr = [];
260
+ for (var _i = 0, value_1 = value; _i < value_1.length; _i++) {
261
+ var val = value_1[_i];
262
+ if (typeof val === 'string' && this.isValidObjectId(val)) {
263
+ val = new Types.ObjectId(val);
264
+ }
265
+ arr.push(val);
266
+ }
267
+ if (key.startsWith("notIn")) {
268
+ key = key.replace("notIn", "");
269
+ condition[key] = { $nin: arr };
270
+ }
271
+ else {
272
+ condition[key] = { $in: arr };
273
+ }
274
+ } else {
275
+ condition[key] = value;
276
+ }
277
+ }
278
+
279
+ return condition
280
+ }
281
+
183
282
  public buildDefaultFilters(objectSearch: any) {
184
283
 
185
284
  // Recuperar todos os campos do tipo String
@@ -189,8 +288,8 @@ class SearcherFlow<D> {
189
288
  let filters = { $and: [] } as any
190
289
  Object.entries(objectSearch).forEach(([key, value]) => {
191
290
  if (isNotEmpty(value) || value === null) {
192
- let condition = {} as any
193
291
  if ([
292
+ 'or',
194
293
  'filters',
195
294
  'withTotalizers',
196
295
  'onlyMetadata',
@@ -224,106 +323,8 @@ class SearcherFlow<D> {
224
323
  value = false
225
324
  }
226
325
 
227
- if (key.endsWith('DateRange')) {
228
- var fieldName = key.replace('Range', '')
229
- var values = value as any
230
- if (isNotEmpty(values[0])) {
231
- var momentValue = moment(values[0])
232
- momentValue.hour(0)
233
- momentValue.minute(0)
234
- momentValue.second(0)
235
- condition[fieldName] = {
236
- ...condition[fieldName],
237
- $gte: momentValue.tz('America/Sao_Paulo', true).toDate()
238
- }
239
- }
240
-
241
- if (isNotEmpty(values[1])) {
242
- var momentValue = moment(values[1])
243
- momentValue.hour(23)
244
- momentValue.minute(59)
245
- momentValue.second(59)
246
- condition[fieldName] = {
247
- ...condition[fieldName],
248
- $lte: momentValue.tz('America/Sao_Paulo', true).toDate()
249
- }
250
- }
251
- filters.$and.push(condition)
252
- }else if (key.endsWith('DateTimeRange')) {
253
- var fieldName = key.replace('Range', '')
254
- var values = value as any
255
- if (isNotEmpty(values[0])) {
256
- var momentValue = moment(values[0])
257
- momentValue.hour(0)
258
- momentValue.minute(0)
259
- momentValue.second(0)
260
- condition[fieldName] = {
261
- ...condition[fieldName],
262
- $gte: momentValue.tz('America/Sao_Paulo', true).toDate()
263
- }
264
- }
265
-
266
- if (isNotEmpty(values[1])) {
267
- var momentValue = moment(values[1])
268
- momentValue.hour(23)
269
- momentValue.minute(59)
270
- momentValue.second(59)
271
- condition[fieldName] = {
272
- ...condition[fieldName],
273
- $lte: momentValue.tz('America/Sao_Paulo', true).toDate()
274
- }
275
- }
276
- filters.$and.push(condition)
277
- } else if (key.endsWith('Like')) {
278
- var fieldName = key.replace('Like', '')
279
- condition[fieldName] = this.buildRegex(value as string) //{ $regex: value as any, $options: 'i' }
280
- filters.$and.push(condition)
281
- } else if (key.endsWith('Exists')) {
282
- var fieldName = key.replace('Exists', '')
283
- condition[fieldName] = { $exists: value as boolean }
284
- filters.$and.push(condition)
285
- }
286
-
287
- else {
288
- if (this.model?.schema?.paths[key]?.instance === 'Array') {
289
- if (!Array.isArray(value)) {
290
- value = [value]
291
- }
292
-
293
- (value as [])?.forEach((v: any) => {
294
- if (typeof v === 'string' && this.isValidObjectId(v)) {
295
- v = new Types.ObjectId(v);
296
- }
297
- })
298
-
299
- if (key.startsWith("notIn")) {
300
- key = key.replace("notIn", "");
301
- condition[key] = { $nin: value };
302
- }
303
- else {
304
- condition[key] = { $in: value };
305
- }
306
- } else if (Array.isArray(value)) {
307
- var arr = [];
308
- for (var _i = 0, value_1 = value; _i < value_1.length; _i++) {
309
- var val = value_1[_i];
310
- if (typeof val === 'string' && this.isValidObjectId(val)) {
311
- val = new Types.ObjectId(val);
312
- }
313
- arr.push(val);
314
- }
315
- if (key.startsWith("notIn")) {
316
- key = key.replace("notIn", "");
317
- condition[key] = { $nin: arr };
318
- }
319
- else {
320
- condition[key] = { $in: arr };
321
- }
322
- } else {
323
- condition[key] = value;
324
- }
325
- filters.$and.push(condition)
326
- }
326
+ const condition = this.buildCondition(key, value)
327
+ filters.$and.push(condition)
327
328
  }
328
329
  })
329
330
 
@@ -342,6 +343,27 @@ class SearcherFlow<D> {
342
343
  if (filters.$and.length === 0)
343
344
  delete filters['$and']
344
345
 
346
+
347
+ Object.entries(objectSearch?.or).forEach(([key, value]) => {
348
+ console.log(key, value)
349
+ if (isEmpty(value)) return
350
+
351
+ if (typeof value === 'string' && this.isValidObjectId(value as string) && key !== 'owner') {
352
+ value = new Types.ObjectId(value as string)
353
+ }
354
+
355
+ if (value === 'true') {
356
+ value = true
357
+ }
358
+
359
+ if (value === 'false') {
360
+ value = false
361
+ }
362
+
363
+ const condition = this.buildCondition(key, value)
364
+ filters.$or.push(condition)
365
+ })
366
+
345
367
  return filters
346
368
  }
347
369