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