mocody 0.4.38 → 0.5.0-alpha

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.
Files changed (83) hide show
  1. package/dist/core/base-schema.d.ts +13 -13
  2. package/dist/core/base-schema.js +11 -11
  3. package/dist/couch/couch-data-operation.d.ts +92 -92
  4. package/dist/couch/couch-data-operation.js +580 -580
  5. package/dist/couch/couch-data-operation.js.map +1 -1
  6. package/dist/couch/couch-filter-query-operation.d.ts +45 -47
  7. package/dist/couch/couch-filter-query-operation.js +371 -438
  8. package/dist/couch/couch-filter-query-operation.js.map +1 -1
  9. package/dist/couch/couch-initializer.d.ts +126 -126
  10. package/dist/couch/couch-initializer.js +189 -189
  11. package/dist/couch/couch-initializer.js.map +1 -1
  12. package/dist/couch/couch-manage-table.d.ts +46 -46
  13. package/dist/couch/couch-manage-table.js +103 -103
  14. package/dist/couch/couch-manage-table.js.map +1 -1
  15. package/dist/dynamo/dynamo-data-operation.d.ts +102 -99
  16. package/dist/dynamo/dynamo-data-operation.js +932 -797
  17. package/dist/dynamo/dynamo-data-operation.js.map +1 -1
  18. package/dist/dynamo/dynamo-filter-query-operation.d.ts +29 -29
  19. package/dist/dynamo/dynamo-filter-query-operation.js +603 -690
  20. package/dist/dynamo/dynamo-filter-query-operation.js.map +1 -1
  21. package/dist/dynamo/dynamo-filter-query-partiql-operation.d.ts +22 -0
  22. package/dist/dynamo/dynamo-filter-query-partiql-operation.js +457 -0
  23. package/dist/dynamo/dynamo-filter-query-partiql-operation.js.map +1 -0
  24. package/dist/dynamo/dynamo-helper.d.ts +1 -1
  25. package/dist/dynamo/dynamo-helper.js +8 -8
  26. package/dist/dynamo/dynamo-initializer.d.ts +15 -14
  27. package/dist/dynamo/dynamo-initializer.js +66 -58
  28. package/dist/dynamo/dynamo-initializer.js.map +1 -1
  29. package/dist/dynamo/dynamo-manage-table.d.ts +33 -33
  30. package/dist/dynamo/dynamo-manage-table.js +419 -419
  31. package/dist/dynamo/dynamo-manage-table.js.map +1 -1
  32. package/dist/dynamo/dynamo-query-partiql-processor.d.ts +31 -0
  33. package/dist/dynamo/dynamo-query-partiql-processor.js +277 -0
  34. package/dist/dynamo/dynamo-query-partiql-processor.js.map +1 -0
  35. package/dist/dynamo/dynamo-query-scan-processor.d.ts +25 -25
  36. package/dist/dynamo/dynamo-query-scan-processor.js +268 -268
  37. package/dist/dynamo/dynamo-query-scan-processor.js.map +1 -1
  38. package/dist/helpers/base-joi-helper.d.ts +2 -2
  39. package/dist/helpers/base-joi-helper.js +12 -12
  40. package/dist/helpers/constants.d.ts +3 -3
  41. package/dist/helpers/constants.js +6 -6
  42. package/dist/helpers/errors.d.ts +13 -13
  43. package/dist/helpers/errors.js +38 -38
  44. package/dist/helpers/logging-service.d.ts +9 -9
  45. package/dist/helpers/logging-service.js +53 -53
  46. package/dist/helpers/logging-service.js.map +1 -1
  47. package/dist/helpers/mocody-utils.d.ts +16 -16
  48. package/dist/helpers/mocody-utils.js +58 -58
  49. package/dist/helpers/query-validator.d.ts +20 -20
  50. package/dist/helpers/query-validator.js +96 -96
  51. package/dist/helpers/util-service.d.ts +29 -29
  52. package/dist/helpers/util-service.js +155 -155
  53. package/dist/index.d.ts +12 -12
  54. package/dist/index.js +23 -23
  55. package/dist/model/index.d.ts +42 -42
  56. package/dist/model/index.js +6 -6
  57. package/dist/mongo/mongo-data-operation.d.ts +90 -90
  58. package/dist/mongo/mongo-data-operation.js +607 -607
  59. package/dist/mongo/mongo-data-operation.js.map +1 -1
  60. package/dist/mongo/mongo-filter-query-operation.d.ts +45 -47
  61. package/dist/mongo/mongo-filter-query-operation.js +368 -433
  62. package/dist/mongo/mongo-filter-query-operation.js.map +1 -1
  63. package/dist/mongo/mongo-initializer.d.ts +16 -16
  64. package/dist/mongo/mongo-initializer.js +47 -47
  65. package/dist/mongo/mongo-initializer.js.map +1 -1
  66. package/dist/mongo/mongo-table-manager.d.ts +31 -31
  67. package/dist/mongo/mongo-table-manager.js +104 -104
  68. package/dist/mongo/mongo-table-manager.js.map +1 -1
  69. package/dist/pouch/pouch-data-operation.d.ts +90 -90
  70. package/dist/pouch/pouch-data-operation.js +583 -583
  71. package/dist/pouch/pouch-data-operation.js.map +1 -1
  72. package/dist/pouch/pouch-filter-query-operation.d.ts +45 -47
  73. package/dist/pouch/pouch-filter-query-operation.js +371 -438
  74. package/dist/pouch/pouch-filter-query-operation.js.map +1 -1
  75. package/dist/pouch/pouch-initializer.d.ts +111 -109
  76. package/dist/pouch/pouch-initializer.js +357 -357
  77. package/dist/pouch/pouch-initializer.js.map +1 -1
  78. package/dist/pouch/pouch-manage-table.d.ts +35 -35
  79. package/dist/pouch/pouch-manage-table.js +99 -99
  80. package/dist/pouch/pouch-manage-table.js.map +1 -1
  81. package/dist/type/index.d.ts +118 -119
  82. package/dist/type/index.js +2 -2
  83. package/package.json +17 -16
@@ -1,434 +1,369 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MongoFilterQueryOperation = void 0;
4
- const logging_service_1 = require("./../helpers/logging-service");
5
- const query_validator_1 = require("../helpers/query-validator");
6
- const errors_1 = require("../helpers/errors");
7
- const KEY_CONDITION_MAP = {
8
- $eq: "$eq",
9
- $lt: "$lt",
10
- $lte: "$lte",
11
- $gt: "$gt",
12
- $gte: "$gte",
13
- $between: "",
14
- $beginsWith: "",
15
- };
16
- const QUERY_CONDITION_MAP_PART = {
17
- $ne: "$ne",
18
- $exists: "",
19
- $in: "",
20
- $nin: "",
21
- $not: "",
22
- $contains: "",
23
- $notContains: "",
24
- $elemMatch: "",
25
- $nestedMatch: "",
26
- };
27
- const QUERY_CONDITION_MAP_FULL = { ...KEY_CONDITION_MAP, ...QUERY_CONDITION_MAP_PART };
28
- function hasQueryKeyCondition(key) {
29
- return Object.keys(KEY_CONDITION_MAP).includes(key);
30
- }
31
- function getQueryConditionExpression(key) {
32
- if (key && Object.keys(QUERY_CONDITION_MAP_FULL).includes(key)) {
33
- const conditionExpr = QUERY_CONDITION_MAP_FULL[key];
34
- if (conditionExpr) {
35
- return conditionExpr;
36
- }
37
- }
38
- return null;
39
- }
40
- class MongoFilterQueryOperation {
41
- operation__filterFieldExist({ fieldName }) {
42
- const result = {
43
- [fieldName]: { $exists: true },
44
- };
45
- return result;
46
- }
47
- operation__filterFieldNotExist({ fieldName }) {
48
- const result = {
49
- [fieldName]: { $exists: false },
50
- };
51
- return result;
52
- }
53
- operation__helperFilterBasic({ fieldName, val, conditionExpr, }) {
54
- if (conditionExpr) {
55
- return {
56
- [fieldName]: { [conditionExpr]: val },
57
- };
58
- }
59
- const result = {
60
- [fieldName]: { $eq: val },
61
- };
62
- return result;
63
- }
64
- operation__filterIn({ fieldName, attrValues }) {
65
- const result = {
66
- [fieldName]: { $in: attrValues },
67
- };
68
- return result;
69
- }
70
- operation__filterElementMatch({ fieldName, attrValues, }) {
71
- const result = {
72
- [fieldName]: {
73
- $elemMatch: attrValues,
74
- },
75
- };
76
- return result;
77
- }
78
- operation__filterNotIn({ fieldName, attrValues, }) {
79
- const result = {
80
- [fieldName]: { $nin: attrValues },
81
- };
82
- return result;
83
- }
84
- operation__filterNot({ fieldName, selectorObjValues, }) {
85
- const selector = { ...selectorObjValues };
86
- const mConditions = [];
87
- Object.entries(selector).forEach(([conditionKey, conditionValue]) => {
88
- if (hasQueryKeyCondition(conditionKey)) {
89
- const _conditionKey01 = conditionKey;
90
- if (_conditionKey01 === "$beginsWith") {
91
- query_validator_1.QueryValidatorCheck.beginWith(conditionValue);
92
- const _queryConditions = this.operation__filterBeginsWith({
93
- fieldName: fieldName,
94
- term: conditionValue,
95
- });
96
- mConditions.push(_queryConditions);
97
- }
98
- else if (_conditionKey01 === "$between") {
99
- query_validator_1.QueryValidatorCheck.between(conditionValue);
100
- const _queryConditions = this.operation__filterBetween({
101
- fieldName: fieldName,
102
- from: conditionValue[0],
103
- to: conditionValue[1],
104
- });
105
- mConditions.push(_queryConditions);
106
- }
107
- else {
108
- const conditionExpr = KEY_CONDITION_MAP[conditionKey];
109
- if (conditionExpr) {
110
- const _queryConditions = this.operation__helperFilterBasic({
111
- fieldName: fieldName,
112
- val: conditionValue,
113
- conditionExpr: conditionExpr,
114
- });
115
- mConditions.push(_queryConditions);
116
- }
117
- else {
118
- query_validator_1.QueryValidatorCheck.throwQueryNotFound(conditionKey);
119
- }
120
- }
121
- }
122
- });
123
- if (mConditions.length) {
124
- let selectorValuesAll = {};
125
- mConditions.forEach((condition) => {
126
- selectorValuesAll = { ...selectorValuesAll, ...condition[fieldName] };
127
- });
128
- const result = {
129
- [fieldName]: { $not: selectorValuesAll },
130
- };
131
- return result;
132
- }
133
- return null;
134
- }
135
- operation__filterContains({ fieldName, term }) {
136
- const result = {
137
- [fieldName]: { $regex: new RegExp(`${term}`, "ig") },
138
- };
139
- return result;
140
- }
141
- operation__filterNotContains({ fieldName, term }) {
142
- const result = {
143
- [fieldName]: { $not: { $regex: new RegExp(`${term}`, "ig") } },
144
- };
145
- return result;
146
- }
147
- operation__filterBetween({ fieldName, from, to, }) {
148
- const result = {
149
- [fieldName]: { $gte: from, $lte: to },
150
- };
151
- return result;
152
- }
153
- operation__filterBeginsWith({ fieldName, term }) {
154
- const result = {
155
- [fieldName]: { $regex: new RegExp(`^${term}`, "i") },
156
- };
157
- return result;
158
- }
159
- operation__filterNestedMatchObject({ fieldName, attrValues, }) {
160
- const results = [];
161
- Object.entries(attrValues).forEach(([subFieldName, queryval]) => {
162
- let _queryValue;
163
- if (queryval && typeof queryval === "object") {
164
- _queryValue = { ...queryval };
165
- }
166
- else {
167
- _queryValue = { $eq: queryval };
168
- }
169
- Object.entries(_queryValue).forEach(([condKey, conditionValue]) => {
170
- const conditionKey = condKey;
171
- if (!Object.keys(KEY_CONDITION_MAP).includes(conditionKey)) {
172
- throw errors_1.MocodyErrorUtilsService.mocody_helper_createFriendlyError(`Invalid query key: ${conditionKey} @ NestedMatchObject`);
173
- }
174
- const conditionExpr = KEY_CONDITION_MAP[conditionKey];
175
- if (conditionExpr) {
176
- const result = {
177
- [`${fieldName}.${subFieldName}`]: { [conditionExpr]: conditionValue },
178
- };
179
- results.push(result);
180
- }
181
- else {
182
- if (conditionKey === "$between") {
183
- query_validator_1.QueryValidatorCheck.between(conditionValue);
184
- const [fromVal, toVal] = conditionValue;
185
- const result = {
186
- [`${fieldName}.${subFieldName}`]: { $gte: fromVal, $lte: toVal },
187
- };
188
- results.push(result);
189
- }
190
- else if (conditionKey === "$beginsWith") {
191
- query_validator_1.QueryValidatorCheck.beginWith(conditionValue);
192
- const result = {
193
- [`${fieldName}.${subFieldName}`]: { $regex: new RegExp(`^${conditionValue}`, "i") },
194
- };
195
- results.push(result);
196
- }
197
- else {
198
- throw errors_1.MocodyErrorUtilsService.mocody_helper_createFriendlyError(`Query key: ${conditionKey} not currently supported`);
199
- }
200
- }
201
- });
202
- });
203
- return results;
204
- }
205
- operation__translateAdvancedQueryOperation({ fieldName, queryObject, }) {
206
- const queryConditions = [];
207
- Object.entries(queryObject).forEach(([condKey, conditionValue]) => {
208
- const conditionKey = condKey;
209
- if (conditionValue !== undefined) {
210
- if (conditionKey === "$between") {
211
- query_validator_1.QueryValidatorCheck.between(conditionValue);
212
- const _queryConditions = this.operation__filterBetween({
213
- fieldName: fieldName,
214
- from: conditionValue[0],
215
- to: conditionValue[1],
216
- });
217
- queryConditions.push(_queryConditions);
218
- }
219
- else if (conditionKey === "$beginsWith") {
220
- query_validator_1.QueryValidatorCheck.beginWith(conditionValue);
221
- const _queryConditions = this.operation__filterBeginsWith({
222
- fieldName: fieldName,
223
- term: conditionValue,
224
- });
225
- queryConditions.push(_queryConditions);
226
- }
227
- else if (conditionKey === "$contains") {
228
- query_validator_1.QueryValidatorCheck.contains(conditionValue);
229
- const _queryConditions = this.operation__filterContains({
230
- fieldName: fieldName,
231
- term: conditionValue,
232
- });
233
- queryConditions.push(_queryConditions);
234
- }
235
- else if (conditionKey === "$notContains") {
236
- query_validator_1.QueryValidatorCheck.notContains(conditionValue);
237
- const _queryConditions = this.operation__filterNotContains({
238
- fieldName: fieldName,
239
- term: conditionValue,
240
- });
241
- queryConditions.push(_queryConditions);
242
- }
243
- else if (conditionKey === "$in") {
244
- query_validator_1.QueryValidatorCheck.in_query(conditionValue);
245
- const _queryConditions = this.operation__filterIn({
246
- fieldName: fieldName,
247
- attrValues: conditionValue,
248
- });
249
- queryConditions.push(_queryConditions);
250
- }
251
- else if (conditionKey === "$nin") {
252
- query_validator_1.QueryValidatorCheck.notIn(conditionValue);
253
- const _queryConditions = this.operation__filterNotIn({
254
- fieldName: fieldName,
255
- attrValues: conditionValue,
256
- });
257
- queryConditions.push(_queryConditions);
258
- }
259
- else if (conditionKey === "$elemMatch") {
260
- query_validator_1.QueryValidatorCheck.elemMatch(conditionValue);
261
- const _queryConditions = this.operation__filterElementMatch({
262
- fieldName: fieldName,
263
- attrValues: conditionValue,
264
- });
265
- queryConditions.push(_queryConditions);
266
- }
267
- else if (conditionKey === "$nestedMatch") {
268
- query_validator_1.QueryValidatorCheck.nestedMatch(conditionValue);
269
- const nestedMatchConditions = this.operation__filterNestedMatchObject({
270
- fieldName: fieldName,
271
- attrValues: conditionValue,
272
- });
273
- if (nestedMatchConditions?.length) {
274
- nestedMatchConditions.forEach((cond) => {
275
- queryConditions.push(cond);
276
- });
277
- }
278
- }
279
- else if (conditionKey === "$not") {
280
- query_validator_1.QueryValidatorCheck.not_query(conditionValue);
281
- const _queryConditions = this.operation__filterNot({
282
- fieldName: fieldName,
283
- selectorObjValues: conditionValue,
284
- });
285
- if (_queryConditions) {
286
- queryConditions.push(_queryConditions);
287
- }
288
- }
289
- else if (conditionKey === "$exists") {
290
- query_validator_1.QueryValidatorCheck.exists(conditionValue);
291
- if (String(conditionValue) === "true") {
292
- const _queryConditions = this.operation__filterFieldExist({
293
- fieldName: fieldName,
294
- });
295
- queryConditions.push(_queryConditions);
296
- }
297
- else if (String(conditionValue) === "false") {
298
- const _queryConditions = this.operation__filterFieldNotExist({
299
- fieldName: fieldName,
300
- });
301
- queryConditions.push(_queryConditions);
302
- }
303
- }
304
- else {
305
- const conditionExpr = getQueryConditionExpression(conditionKey);
306
- if (conditionExpr) {
307
- const _queryConditions = this.operation__helperFilterBasic({
308
- fieldName: fieldName,
309
- val: conditionValue,
310
- conditionExpr: conditionExpr,
311
- });
312
- queryConditions.push(_queryConditions);
313
- }
314
- else {
315
- query_validator_1.QueryValidatorCheck.throwQueryNotFound(conditionKey);
316
- }
317
- }
318
- }
319
- });
320
- return queryConditions;
321
- }
322
- operation_translateBasicQueryOperation({ fieldName, queryObject }) {
323
- const _queryConditions = this.operation__helperFilterBasic({
324
- fieldName: fieldName,
325
- val: queryObject,
326
- });
327
- return _queryConditions;
328
- }
329
- processQueryFilter({ queryDefs }) {
330
- let queryMainConditions = [];
331
- let queryAndConditions = [];
332
- const queryOrConditions = [];
333
- Object.entries(queryDefs).forEach(([conditionKey, conditionValue]) => {
334
- if (conditionKey === "$or") {
335
- const orArray = conditionValue;
336
- query_validator_1.QueryValidatorCheck.or_query(orArray);
337
- orArray.forEach((orQuery) => {
338
- const perQueryCondition02 = {};
339
- Object.entries(orQuery).forEach(([fieldName, orQueryObjectOrValue]) => {
340
- if (orQueryObjectOrValue !== undefined) {
341
- if (orQueryObjectOrValue && typeof orQueryObjectOrValue === "object") {
342
- const orQueryCond01 = this.operation__translateAdvancedQueryOperation({
343
- fieldName,
344
- queryObject: orQueryObjectOrValue,
345
- });
346
- let nquery = {};
347
- for (const xcond of orQueryCond01) {
348
- Object.entries(xcond).forEach(([_, value]) => {
349
- nquery = { ...nquery, ...value };
350
- });
351
- }
352
- perQueryCondition02[fieldName] = nquery;
353
- }
354
- else {
355
- const orQueryCondition02 = this.operation_translateBasicQueryOperation({
356
- fieldName,
357
- queryObject: orQueryObjectOrValue,
358
- });
359
- let nquery02 = {};
360
- Object.entries(orQueryCondition02).forEach(([_, value]) => {
361
- nquery02 = { ...nquery02, ...value };
362
- });
363
- perQueryCondition02[fieldName] = nquery02;
364
- }
365
- }
366
- });
367
- if (Object.keys(perQueryCondition02).length) {
368
- queryOrConditions.push(perQueryCondition02);
369
- }
370
- });
371
- }
372
- else if (conditionKey === "$and") {
373
- const andArray = conditionValue;
374
- query_validator_1.QueryValidatorCheck.and_query(conditionValue);
375
- andArray.forEach((andQuery) => {
376
- Object.entries(andQuery).forEach(([fieldName, andQueryObjectOrValue]) => {
377
- if (andQueryObjectOrValue !== undefined) {
378
- if (andQueryObjectOrValue && typeof andQueryObjectOrValue === "object") {
379
- const _andQueryCond = this.operation__translateAdvancedQueryOperation({
380
- fieldName,
381
- queryObject: andQueryObjectOrValue,
382
- });
383
- queryAndConditions = [...queryAndConditions, ..._andQueryCond];
384
- }
385
- else {
386
- const _andQueryConditions = this.operation_translateBasicQueryOperation({
387
- fieldName,
388
- queryObject: andQueryObjectOrValue,
389
- });
390
- queryAndConditions = [...queryAndConditions, _andQueryConditions];
391
- }
392
- }
393
- });
394
- });
395
- }
396
- else {
397
- if (conditionKey && conditionValue !== undefined) {
398
- if (conditionValue && typeof conditionValue === "object") {
399
- const _queryCond = this.operation__translateAdvancedQueryOperation({
400
- fieldName: conditionKey,
401
- queryObject: conditionValue,
402
- });
403
- queryMainConditions = [...queryMainConditions, ..._queryCond];
404
- }
405
- else {
406
- const _queryConditions = this.operation_translateBasicQueryOperation({
407
- fieldName: conditionKey,
408
- queryObject: conditionValue,
409
- });
410
- queryMainConditions = [...queryMainConditions, _queryConditions];
411
- }
412
- }
413
- }
414
- });
415
- let queryAllConditions = {};
416
- if (queryMainConditions?.length) {
417
- queryMainConditions.forEach((item1) => {
418
- if (item1) {
419
- queryAllConditions = { ...queryAllConditions, ...item1 };
420
- }
421
- });
422
- }
423
- if (queryAndConditions?.length) {
424
- queryAllConditions.$and = queryAndConditions;
425
- }
426
- if (queryOrConditions?.length) {
427
- queryAllConditions.$or = queryOrConditions;
428
- }
429
- logging_service_1.LoggingService.logAsString({ queryAllConditions });
430
- return queryAllConditions;
431
- }
432
- }
433
- exports.MongoFilterQueryOperation = MongoFilterQueryOperation;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MongoFilterQueryOperation = void 0;
4
+ const logging_service_1 = require("./../helpers/logging-service");
5
+ const query_validator_1 = require("../helpers/query-validator");
6
+ const errors_1 = require("../helpers/errors");
7
+ const KEY_CONDITION_MAP = {
8
+ $eq: "$eq",
9
+ $lt: "$lt",
10
+ $lte: "$lte",
11
+ $gt: "$gt",
12
+ $gte: "$gte",
13
+ $between: "",
14
+ $beginsWith: "",
15
+ };
16
+ const QUERY_CONDITION_MAP_PART = {
17
+ $ne: "$ne",
18
+ $exists: "",
19
+ $in: "",
20
+ $nin: "",
21
+ $contains: "",
22
+ $notContains: "",
23
+ $elemMatch: "",
24
+ $nestedMatch: "",
25
+ };
26
+ const QUERY_CONDITION_MAP_FULL = { ...KEY_CONDITION_MAP, ...QUERY_CONDITION_MAP_PART };
27
+ function getQueryConditionExpression(key) {
28
+ if (key && Object.keys(QUERY_CONDITION_MAP_FULL).includes(key)) {
29
+ const conditionExpr = QUERY_CONDITION_MAP_FULL[key];
30
+ if (conditionExpr) {
31
+ return conditionExpr;
32
+ }
33
+ }
34
+ return null;
35
+ }
36
+ class MongoFilterQueryOperation {
37
+ operation__filterFieldExist({ fieldName }) {
38
+ const result = {
39
+ [fieldName]: { $exists: true },
40
+ };
41
+ return result;
42
+ }
43
+ operation__filterFieldNotExist({ fieldName }) {
44
+ const result = {
45
+ [fieldName]: { $exists: false },
46
+ };
47
+ return result;
48
+ }
49
+ operation__helperFilterBasic({ fieldName, val, conditionExpr, }) {
50
+ if (conditionExpr) {
51
+ return {
52
+ [fieldName]: { [conditionExpr]: val },
53
+ };
54
+ }
55
+ const result = {
56
+ [fieldName]: { $eq: val },
57
+ };
58
+ return result;
59
+ }
60
+ operation__filterIn({ fieldName, attrValues }) {
61
+ const result = {
62
+ [fieldName]: { $in: attrValues },
63
+ };
64
+ return result;
65
+ }
66
+ operation__filterElementMatch({ fieldName, attrValues, }) {
67
+ const result = {
68
+ [fieldName]: {
69
+ $elemMatch: attrValues,
70
+ },
71
+ };
72
+ return result;
73
+ }
74
+ operation__filterNotIn({ fieldName, attrValues, }) {
75
+ const result = {
76
+ [fieldName]: { $nin: attrValues },
77
+ };
78
+ return result;
79
+ }
80
+ operation__filterContains({ fieldName, term }) {
81
+ const result = {
82
+ [fieldName]: { $regex: new RegExp(`${term}`, "ig") },
83
+ };
84
+ return result;
85
+ }
86
+ operation__filterNotContains({ fieldName, term }) {
87
+ const result = {
88
+ [fieldName]: { $not: { $regex: new RegExp(`${term}`, "ig") } },
89
+ };
90
+ return result;
91
+ }
92
+ operation__filterBetween({ fieldName, from, to, }) {
93
+ const result = {
94
+ [fieldName]: { $gte: from, $lte: to },
95
+ };
96
+ return result;
97
+ }
98
+ operation__filterBeginsWith({ fieldName, term }) {
99
+ const result = {
100
+ [fieldName]: { $regex: new RegExp(`^${term}`, "i") },
101
+ };
102
+ return result;
103
+ }
104
+ operation__filterNestedMatchObject({ fieldName, attrValues, }) {
105
+ const results = [];
106
+ Object.entries(attrValues).forEach(([subFieldName, queryval]) => {
107
+ let _queryValue;
108
+ if (queryval && typeof queryval === "object") {
109
+ _queryValue = { ...queryval };
110
+ }
111
+ else {
112
+ _queryValue = { $eq: queryval };
113
+ }
114
+ Object.entries(_queryValue).forEach(([condKey, conditionValue]) => {
115
+ const conditionKey = condKey;
116
+ if (!Object.keys(KEY_CONDITION_MAP).includes(conditionKey)) {
117
+ throw errors_1.MocodyErrorUtilsService.mocody_helper_createFriendlyError(`Invalid query key: ${conditionKey} @ NestedMatchObject`);
118
+ }
119
+ const conditionExpr = KEY_CONDITION_MAP[conditionKey];
120
+ if (conditionExpr) {
121
+ const result = {
122
+ [`${fieldName}.${subFieldName}`]: { [conditionExpr]: conditionValue },
123
+ };
124
+ results.push(result);
125
+ }
126
+ else {
127
+ if (conditionKey === "$between") {
128
+ query_validator_1.QueryValidatorCheck.between(conditionValue);
129
+ const [fromVal, toVal] = conditionValue;
130
+ const result = {
131
+ [`${fieldName}.${subFieldName}`]: { $gte: fromVal, $lte: toVal },
132
+ };
133
+ results.push(result);
134
+ }
135
+ else if (conditionKey === "$beginsWith") {
136
+ query_validator_1.QueryValidatorCheck.beginWith(conditionValue);
137
+ const result = {
138
+ [`${fieldName}.${subFieldName}`]: { $regex: new RegExp(`^${conditionValue}`, "i") },
139
+ };
140
+ results.push(result);
141
+ }
142
+ else {
143
+ throw errors_1.MocodyErrorUtilsService.mocody_helper_createFriendlyError(`Query key: ${conditionKey} not currently supported`);
144
+ }
145
+ }
146
+ });
147
+ });
148
+ return results;
149
+ }
150
+ operation__translateAdvancedQueryOperation({ fieldName, queryObject, }) {
151
+ const queryConditions = [];
152
+ Object.entries(queryObject).forEach(([condKey, conditionValue]) => {
153
+ const conditionKey = condKey;
154
+ if (conditionValue !== undefined) {
155
+ if (conditionKey === "$between") {
156
+ query_validator_1.QueryValidatorCheck.between(conditionValue);
157
+ const _queryConditions = this.operation__filterBetween({
158
+ fieldName: fieldName,
159
+ from: conditionValue[0],
160
+ to: conditionValue[1],
161
+ });
162
+ queryConditions.push(_queryConditions);
163
+ }
164
+ else if (conditionKey === "$beginsWith") {
165
+ query_validator_1.QueryValidatorCheck.beginWith(conditionValue);
166
+ const _queryConditions = this.operation__filterBeginsWith({
167
+ fieldName: fieldName,
168
+ term: conditionValue,
169
+ });
170
+ queryConditions.push(_queryConditions);
171
+ }
172
+ else if (conditionKey === "$contains") {
173
+ query_validator_1.QueryValidatorCheck.contains(conditionValue);
174
+ const _queryConditions = this.operation__filterContains({
175
+ fieldName: fieldName,
176
+ term: conditionValue,
177
+ });
178
+ queryConditions.push(_queryConditions);
179
+ }
180
+ else if (conditionKey === "$notContains") {
181
+ query_validator_1.QueryValidatorCheck.notContains(conditionValue);
182
+ const _queryConditions = this.operation__filterNotContains({
183
+ fieldName: fieldName,
184
+ term: conditionValue,
185
+ });
186
+ queryConditions.push(_queryConditions);
187
+ }
188
+ else if (conditionKey === "$in") {
189
+ query_validator_1.QueryValidatorCheck.in_query(conditionValue);
190
+ const _queryConditions = this.operation__filterIn({
191
+ fieldName: fieldName,
192
+ attrValues: conditionValue,
193
+ });
194
+ queryConditions.push(_queryConditions);
195
+ }
196
+ else if (conditionKey === "$nin") {
197
+ query_validator_1.QueryValidatorCheck.notIn(conditionValue);
198
+ const _queryConditions = this.operation__filterNotIn({
199
+ fieldName: fieldName,
200
+ attrValues: conditionValue,
201
+ });
202
+ queryConditions.push(_queryConditions);
203
+ }
204
+ else if (conditionKey === "$elemMatch") {
205
+ query_validator_1.QueryValidatorCheck.elemMatch(conditionValue);
206
+ const _queryConditions = this.operation__filterElementMatch({
207
+ fieldName: fieldName,
208
+ attrValues: conditionValue,
209
+ });
210
+ queryConditions.push(_queryConditions);
211
+ }
212
+ else if (conditionKey === "$nestedMatch") {
213
+ query_validator_1.QueryValidatorCheck.nestedMatch(conditionValue);
214
+ const nestedMatchConditions = this.operation__filterNestedMatchObject({
215
+ fieldName: fieldName,
216
+ attrValues: conditionValue,
217
+ });
218
+ if (nestedMatchConditions?.length) {
219
+ nestedMatchConditions.forEach((cond) => {
220
+ queryConditions.push(cond);
221
+ });
222
+ }
223
+ }
224
+ else if (conditionKey === "$exists") {
225
+ query_validator_1.QueryValidatorCheck.exists(conditionValue);
226
+ if (String(conditionValue) === "true") {
227
+ const _queryConditions = this.operation__filterFieldExist({
228
+ fieldName: fieldName,
229
+ });
230
+ queryConditions.push(_queryConditions);
231
+ }
232
+ else if (String(conditionValue) === "false") {
233
+ const _queryConditions = this.operation__filterFieldNotExist({
234
+ fieldName: fieldName,
235
+ });
236
+ queryConditions.push(_queryConditions);
237
+ }
238
+ }
239
+ else {
240
+ const conditionExpr = getQueryConditionExpression(conditionKey);
241
+ if (conditionExpr) {
242
+ const _queryConditions = this.operation__helperFilterBasic({
243
+ fieldName: fieldName,
244
+ val: conditionValue,
245
+ conditionExpr: conditionExpr,
246
+ });
247
+ queryConditions.push(_queryConditions);
248
+ }
249
+ else {
250
+ query_validator_1.QueryValidatorCheck.throwQueryNotFound(conditionKey);
251
+ }
252
+ }
253
+ }
254
+ });
255
+ return queryConditions;
256
+ }
257
+ operation_translateBasicQueryOperation({ fieldName, queryObject }) {
258
+ const _queryConditions = this.operation__helperFilterBasic({
259
+ fieldName: fieldName,
260
+ val: queryObject,
261
+ });
262
+ return _queryConditions;
263
+ }
264
+ processQueryFilter({ queryDefs }) {
265
+ let queryMainConditions = [];
266
+ let queryAndConditions = [];
267
+ const queryOrConditions = [];
268
+ Object.entries(queryDefs).forEach(([conditionKey, conditionValue]) => {
269
+ if (conditionKey === "$or") {
270
+ const orArray = conditionValue;
271
+ query_validator_1.QueryValidatorCheck.or_query(orArray);
272
+ orArray.forEach((orQuery) => {
273
+ const perQueryCondition02 = {};
274
+ Object.entries(orQuery).forEach(([fieldName, orQueryObjectOrValue]) => {
275
+ if (orQueryObjectOrValue !== undefined) {
276
+ if (orQueryObjectOrValue && typeof orQueryObjectOrValue === "object") {
277
+ const orQueryCond01 = this.operation__translateAdvancedQueryOperation({
278
+ fieldName,
279
+ queryObject: orQueryObjectOrValue,
280
+ });
281
+ let nquery = {};
282
+ for (const xcond of orQueryCond01) {
283
+ Object.entries(xcond).forEach(([_, value]) => {
284
+ nquery = { ...nquery, ...value };
285
+ });
286
+ }
287
+ perQueryCondition02[fieldName] = nquery;
288
+ }
289
+ else {
290
+ const orQueryCondition02 = this.operation_translateBasicQueryOperation({
291
+ fieldName,
292
+ queryObject: orQueryObjectOrValue,
293
+ });
294
+ let nquery02 = {};
295
+ Object.entries(orQueryCondition02).forEach(([_, value]) => {
296
+ nquery02 = { ...nquery02, ...value };
297
+ });
298
+ perQueryCondition02[fieldName] = nquery02;
299
+ }
300
+ }
301
+ });
302
+ if (Object.keys(perQueryCondition02).length) {
303
+ queryOrConditions.push(perQueryCondition02);
304
+ }
305
+ });
306
+ }
307
+ else if (conditionKey === "$and") {
308
+ const andArray = conditionValue;
309
+ query_validator_1.QueryValidatorCheck.and_query(conditionValue);
310
+ andArray.forEach((andQuery) => {
311
+ Object.entries(andQuery).forEach(([fieldName, andQueryObjectOrValue]) => {
312
+ if (andQueryObjectOrValue !== undefined) {
313
+ if (andQueryObjectOrValue && typeof andQueryObjectOrValue === "object") {
314
+ const _andQueryCond = this.operation__translateAdvancedQueryOperation({
315
+ fieldName,
316
+ queryObject: andQueryObjectOrValue,
317
+ });
318
+ queryAndConditions = [...queryAndConditions, ..._andQueryCond];
319
+ }
320
+ else {
321
+ const _andQueryConditions = this.operation_translateBasicQueryOperation({
322
+ fieldName,
323
+ queryObject: andQueryObjectOrValue,
324
+ });
325
+ queryAndConditions = [...queryAndConditions, _andQueryConditions];
326
+ }
327
+ }
328
+ });
329
+ });
330
+ }
331
+ else {
332
+ if (conditionKey && conditionValue !== undefined) {
333
+ if (conditionValue && typeof conditionValue === "object") {
334
+ const _queryCond = this.operation__translateAdvancedQueryOperation({
335
+ fieldName: conditionKey,
336
+ queryObject: conditionValue,
337
+ });
338
+ queryMainConditions = [...queryMainConditions, ..._queryCond];
339
+ }
340
+ else {
341
+ const _queryConditions = this.operation_translateBasicQueryOperation({
342
+ fieldName: conditionKey,
343
+ queryObject: conditionValue,
344
+ });
345
+ queryMainConditions = [...queryMainConditions, _queryConditions];
346
+ }
347
+ }
348
+ }
349
+ });
350
+ let queryAllConditions = {};
351
+ if (queryMainConditions?.length) {
352
+ queryMainConditions.forEach((item1) => {
353
+ if (item1) {
354
+ queryAllConditions = { ...queryAllConditions, ...item1 };
355
+ }
356
+ });
357
+ }
358
+ if (queryAndConditions?.length) {
359
+ queryAllConditions.$and = queryAndConditions;
360
+ }
361
+ if (queryOrConditions?.length) {
362
+ queryAllConditions.$or = queryOrConditions;
363
+ }
364
+ logging_service_1.LoggingService.logAsString({ queryAllConditions });
365
+ return queryAllConditions;
366
+ }
367
+ }
368
+ exports.MongoFilterQueryOperation = MongoFilterQueryOperation;
434
369
  //# sourceMappingURL=mongo-filter-query-operation.js.map