mocody 0.2.15 → 0.2.18

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 (64) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +4 -4
  3. package/dist/core/base-schema.d.ts +13 -13
  4. package/dist/core/base-schema.js +11 -11
  5. package/dist/core/base-schema.js.map +1 -1
  6. package/dist/couch/couch-data-operation.d.ts +90 -90
  7. package/dist/couch/couch-data-operation.js +581 -581
  8. package/dist/couch/couch-data-operation.js.map +1 -1
  9. package/dist/couch/couch-filter-query-operation.d.ts +47 -47
  10. package/dist/couch/couch-filter-query-operation.js +438 -438
  11. package/dist/couch/couch-initializer.d.ts +45 -45
  12. package/dist/couch/couch-initializer.js +98 -98
  13. package/dist/couch/couch-initializer.js.map +1 -1
  14. package/dist/couch/couch-manage-table.d.ts +46 -46
  15. package/dist/couch/couch-manage-table.js +113 -113
  16. package/dist/dynamo/dynamo-data-operation.d.ts +97 -97
  17. package/dist/dynamo/dynamo-data-operation.js +788 -793
  18. package/dist/dynamo/dynamo-data-operation.js.map +1 -1
  19. package/dist/dynamo/dynamo-filter-query-operation.d.ts +29 -28
  20. package/dist/dynamo/dynamo-filter-query-operation.js +655 -627
  21. package/dist/dynamo/dynamo-filter-query-operation.js.map +1 -1
  22. package/dist/dynamo/dynamo-helper.d.ts +1 -1
  23. package/dist/dynamo/dynamo-helper.js +8 -8
  24. package/dist/dynamo/dynamo-initializer.d.ts +14 -8
  25. package/dist/dynamo/dynamo-initializer.js +58 -34
  26. package/dist/dynamo/dynamo-initializer.js.map +1 -1
  27. package/dist/dynamo/dynamo-manage-table.d.ts +33 -32
  28. package/dist/dynamo/dynamo-manage-table.js +419 -418
  29. package/dist/dynamo/dynamo-manage-table.js.map +1 -1
  30. package/dist/dynamo/dynamo-query-scan-processor.d.ts +25 -24
  31. package/dist/dynamo/dynamo-query-scan-processor.js +268 -266
  32. package/dist/dynamo/dynamo-query-scan-processor.js.map +1 -1
  33. package/dist/helpers/base-joi-helper.d.ts +2 -2
  34. package/dist/helpers/base-joi-helper.js +12 -12
  35. package/dist/helpers/constants.d.ts +3 -3
  36. package/dist/helpers/constants.js +6 -6
  37. package/dist/helpers/errors.d.ts +13 -13
  38. package/dist/helpers/errors.js +38 -38
  39. package/dist/helpers/logging-service.d.ts +8 -8
  40. package/dist/helpers/logging-service.js +44 -44
  41. package/dist/helpers/mocody-utils.d.ts +13 -15
  42. package/dist/helpers/mocody-utils.js +48 -48
  43. package/dist/helpers/query-validator.d.ts +20 -20
  44. package/dist/helpers/query-validator.js +96 -84
  45. package/dist/helpers/query-validator.js.map +1 -1
  46. package/dist/helpers/util-service.d.ts +29 -29
  47. package/dist/helpers/util-service.js +155 -155
  48. package/dist/index.d.ts +10 -10
  49. package/dist/index.js +19 -19
  50. package/dist/model/index.d.ts +40 -40
  51. package/dist/model/index.js +6 -6
  52. package/dist/mongo/mongo-data-operation.d.ts +88 -88
  53. package/dist/mongo/mongo-data-operation.js +597 -597
  54. package/dist/mongo/mongo-data-operation.js.map +1 -1
  55. package/dist/mongo/mongo-filter-query-operation.d.ts +47 -47
  56. package/dist/mongo/mongo-filter-query-operation.js +433 -433
  57. package/dist/mongo/mongo-initializer.d.ts +16 -16
  58. package/dist/mongo/mongo-initializer.js +47 -47
  59. package/dist/mongo/mongo-initializer.js.map +1 -1
  60. package/dist/mongo/mongo-table-manager.d.ts +31 -31
  61. package/dist/mongo/mongo-table-manager.js +104 -104
  62. package/dist/type/index.d.ts +109 -108
  63. package/dist/type/index.js +2 -2
  64. package/package.json +70 -72
@@ -1,439 +1,439 @@
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 keyConditionMap = {
7
- $eq: "$eq",
8
- $lt: "$lt",
9
- $lte: "$lte",
10
- $gt: "$gt",
11
- $gte: "$gte",
12
- $between: "",
13
- $beginsWith: "",
14
- };
15
- const conditionMapPre = {
16
- $ne: "$ne",
17
- $exists: "",
18
- $in: "",
19
- $nin: "",
20
- $not: "",
21
- $contains: "",
22
- $notContains: "",
23
- $elemMatch: "",
24
- $nestedMatch: "",
25
- };
26
- const conditionMap = { ...keyConditionMap, ...conditionMapPre };
27
- function hasQueryKeyCondition(key) {
28
- return Object.keys(keyConditionMap).includes(key);
29
- }
30
- function getQueryConditionExpression(key) {
31
- if (key && Object.keys(conditionMap).includes(key)) {
32
- const conditionExpr = conditionMap[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 = keyConditionMap[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(keyConditionMap).includes(conditionKey)) {
177
- throw errors_1.MocodyErrorUtilsService.mocody_helper_createFriendlyError(`Invalid query key: ${conditionKey} @ NestedMatchObject`);
178
- }
179
- const conditionExpr = keyConditionMap[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 keyConditionMap = {
7
+ $eq: "$eq",
8
+ $lt: "$lt",
9
+ $lte: "$lte",
10
+ $gt: "$gt",
11
+ $gte: "$gte",
12
+ $between: "",
13
+ $beginsWith: "",
14
+ };
15
+ const conditionMapPre = {
16
+ $ne: "$ne",
17
+ $exists: "",
18
+ $in: "",
19
+ $nin: "",
20
+ $not: "",
21
+ $contains: "",
22
+ $notContains: "",
23
+ $elemMatch: "",
24
+ $nestedMatch: "",
25
+ };
26
+ const conditionMap = { ...keyConditionMap, ...conditionMapPre };
27
+ function hasQueryKeyCondition(key) {
28
+ return Object.keys(keyConditionMap).includes(key);
29
+ }
30
+ function getQueryConditionExpression(key) {
31
+ if (key && Object.keys(conditionMap).includes(key)) {
32
+ const conditionExpr = conditionMap[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 = keyConditionMap[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(keyConditionMap).includes(conditionKey)) {
177
+ throw errors_1.MocodyErrorUtilsService.mocody_helper_createFriendlyError(`Invalid query key: ${conditionKey} @ NestedMatchObject`);
178
+ }
179
+ const conditionExpr = keyConditionMap[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;
439
439
  //# sourceMappingURL=couch-filter-query-operation.js.map