mocody 0.2.14 → 0.2.17

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 (66) 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 -15
  24. package/dist/dynamo/dynamo-helper.js.map +1 -1
  25. package/dist/dynamo/dynamo-initializer.d.ts +14 -8
  26. package/dist/dynamo/dynamo-initializer.js +58 -34
  27. package/dist/dynamo/dynamo-initializer.js.map +1 -1
  28. package/dist/dynamo/dynamo-manage-table.d.ts +33 -32
  29. package/dist/dynamo/dynamo-manage-table.js +419 -418
  30. package/dist/dynamo/dynamo-manage-table.js.map +1 -1
  31. package/dist/dynamo/dynamo-query-scan-processor.d.ts +25 -24
  32. package/dist/dynamo/dynamo-query-scan-processor.js +268 -266
  33. package/dist/dynamo/dynamo-query-scan-processor.js.map +1 -1
  34. package/dist/helpers/base-joi-helper.d.ts +2 -2
  35. package/dist/helpers/base-joi-helper.js +12 -12
  36. package/dist/helpers/constants.d.ts +3 -3
  37. package/dist/helpers/constants.js +6 -6
  38. package/dist/helpers/errors.d.ts +13 -13
  39. package/dist/helpers/errors.js +38 -38
  40. package/dist/helpers/logging-service.d.ts +8 -8
  41. package/dist/helpers/logging-service.js +44 -44
  42. package/dist/helpers/mocody-utils.d.ts +13 -15
  43. package/dist/helpers/mocody-utils.js +48 -48
  44. package/dist/helpers/query-validator.d.ts +20 -20
  45. package/dist/helpers/query-validator.js +96 -84
  46. package/dist/helpers/query-validator.js.map +1 -1
  47. package/dist/helpers/util-service.d.ts +29 -30
  48. package/dist/helpers/util-service.js +155 -167
  49. package/dist/helpers/util-service.js.map +1 -1
  50. package/dist/index.d.ts +10 -10
  51. package/dist/index.js +19 -19
  52. package/dist/model/index.d.ts +40 -40
  53. package/dist/model/index.js +6 -6
  54. package/dist/mongo/mongo-data-operation.d.ts +88 -88
  55. package/dist/mongo/mongo-data-operation.js +597 -597
  56. package/dist/mongo/mongo-data-operation.js.map +1 -1
  57. package/dist/mongo/mongo-filter-query-operation.d.ts +47 -47
  58. package/dist/mongo/mongo-filter-query-operation.js +433 -433
  59. package/dist/mongo/mongo-initializer.d.ts +16 -16
  60. package/dist/mongo/mongo-initializer.js +47 -47
  61. package/dist/mongo/mongo-initializer.js.map +1 -1
  62. package/dist/mongo/mongo-table-manager.d.ts +31 -31
  63. package/dist/mongo/mongo-table-manager.js +104 -104
  64. package/dist/type/index.d.ts +108 -108
  65. package/dist/type/index.js +2 -2
  66. package/package.json +70 -71
@@ -1,628 +1,656 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DynamoFilterQueryOperation = void 0;
4
- const query_validator_1 = require("./../helpers/query-validator");
5
- const logging_service_1 = require("../helpers/logging-service");
6
- const util_service_1 = require("../helpers/util-service");
7
- const errors_1 = require("../helpers/errors");
8
- const dynamo_helper_1 = require("./dynamo-helper");
9
- const keyConditionMap = {
10
- $eq: "=",
11
- $lt: "<",
12
- $lte: "<=",
13
- $gt: ">",
14
- $gte: ">=",
15
- $beginsWith: "",
16
- $between: "",
17
- };
18
- const conditionMapPre = {
19
- $ne: "<>",
20
- $exists: "",
21
- $in: "",
22
- $nin: "",
23
- $not: "",
24
- $contains: "",
25
- $notContains: "",
26
- $elemMatch: "",
27
- $nestedMatch: "",
28
- };
29
- const conditionMap = { ...keyConditionMap, ...conditionMapPre };
30
- function hasQueryConditionValue(key) {
31
- if (key && Object.keys(conditionMap).includes(key) && conditionMap[key]) {
32
- return true;
33
- }
34
- return false;
35
- }
36
- class DynamoFilterQueryOperation {
37
- operation__filterFieldExist({ fieldName }) {
38
- const attrKeyHash = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
39
- const result = {
40
- xExpressionAttributeNames: {
41
- [attrKeyHash]: fieldName,
42
- },
43
- xFilterExpression: `attribute_exists (${attrKeyHash})`,
44
- };
45
- return result;
46
- }
47
- operation__filterFieldNotExist({ fieldName }) {
48
- const attrKeyHash = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
49
- const result = {
50
- xExpressionAttributeNames: {
51
- [attrKeyHash]: fieldName,
52
- },
53
- xFilterExpression: `attribute_not_exists (${attrKeyHash})`,
54
- };
55
- return result;
56
- }
57
- operation__helperFilterBasic({ fieldName, val, conditionExpr, }) {
58
- const keyAttr = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)(":");
59
- const attrKeyHash = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
60
- const result = {
61
- xExpressionAttributeValues: {
62
- [keyAttr]: val,
63
- },
64
- xExpressionAttributeNames: {
65
- [attrKeyHash]: fieldName,
66
- },
67
- xFilterExpression: [attrKeyHash, conditionExpr, keyAttr].join(" "),
68
- };
69
- return result;
70
- }
71
- operation__filterIn({ fieldName, attrValues }) {
72
- const expressAttrVal = {};
73
- const expressAttrName = {};
74
- const filterExpress = [];
75
- const _attrKeyHash = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
76
- expressAttrName[_attrKeyHash] = fieldName;
77
- attrValues.forEach((item) => {
78
- const keyAttr = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)(":");
79
- expressAttrVal[keyAttr] = item;
80
- filterExpress.push(`${_attrKeyHash} = ${keyAttr}`);
81
- });
82
- const _filterExpression = filterExpress.join(" OR ").trim();
83
- const _filterExpressionValue = filterExpress.length > 1 ? `(${_filterExpression})` : _filterExpression;
84
- const result = {
85
- xExpressionAttributeValues: {
86
- ...expressAttrVal,
87
- },
88
- xExpressionAttributeNames: {
89
- ...expressAttrName,
90
- },
91
- xFilterExpression: _filterExpressionValue,
92
- };
93
- return result;
94
- }
95
- operation__filterElemMatch({ fieldName, attrValues, }) {
96
- const result = [];
97
- attrValues.$in.forEach((term) => {
98
- const query01 = this.operation__filterContains({ term, fieldName });
99
- result.push(query01);
100
- });
101
- return result;
102
- }
103
- operation__filterNestedMatchObject({ fieldName, attrValues, }) {
104
- const results = [];
105
- Object.entries(attrValues).forEach(([subFieldName, queryval]) => {
106
- let _queryValue;
107
- if (queryval && typeof queryval === "object") {
108
- _queryValue = { ...queryval };
109
- }
110
- else {
111
- _queryValue = { $eq: queryval };
112
- }
113
- Object.entries(_queryValue).forEach(([condKey, conditionValue]) => {
114
- const conditionKey = condKey;
115
- if (!Object.keys(keyConditionMap).includes(conditionKey)) {
116
- throw errors_1.MocodyErrorUtilsService.mocody_helper_createFriendlyError(`Invalid query key: ${conditionKey} @ NestedMatchObject`);
117
- }
118
- const conditionExpr = keyConditionMap[conditionKey];
119
- const attrValue = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)(":");
120
- const attrKeyHash = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
121
- const parentFieldName = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
122
- if (conditionExpr) {
123
- const result = {
124
- xExpressionAttributeValues: {
125
- [attrValue]: conditionValue,
126
- },
127
- xExpressionAttributeNames: {
128
- [attrKeyHash]: subFieldName,
129
- [parentFieldName]: fieldName,
130
- },
131
- xFilterExpression: [`${parentFieldName}.${attrKeyHash}`, conditionExpr, attrValue].join(" "),
132
- };
133
- results.push(result);
134
- }
135
- else {
136
- if (conditionKey === "$between") {
137
- query_validator_1.QueryValidatorCheck.between(conditionValue);
138
- const fromKey = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)(":");
139
- const toKey = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)(":");
140
- const [fromVal, toVal] = conditionValue;
141
- const result = {
142
- xExpressionAttributeValues: {
143
- [fromKey]: fromVal,
144
- [toKey]: toVal,
145
- },
146
- xExpressionAttributeNames: {
147
- [attrKeyHash]: subFieldName,
148
- [parentFieldName]: fieldName,
149
- },
150
- xFilterExpression: [`${parentFieldName}.${attrKeyHash}`, "between", fromKey, "and", toKey].join(" "),
151
- };
152
- results.push(result);
153
- }
154
- else if (conditionKey === "$beginsWith") {
155
- const result = {
156
- xExpressionAttributeValues: {
157
- [attrValue]: conditionValue,
158
- },
159
- xExpressionAttributeNames: {
160
- [attrKeyHash]: subFieldName,
161
- [parentFieldName]: fieldName,
162
- },
163
- xFilterExpression: `begins_with (${parentFieldName}.${attrKeyHash}, ${attrValue})`,
164
- };
165
- results.push(result);
166
- }
167
- else {
168
- throw errors_1.MocodyErrorUtilsService.mocody_helper_createFriendlyError(`Query key: ${conditionKey} not currently supported`);
169
- }
170
- }
171
- });
172
- });
173
- return results;
174
- }
175
- operation__filterContains({ fieldName, term }) {
176
- const attrKeyHash = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
177
- const keyAttr = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)(":");
178
- const result = {
179
- xExpressionAttributeValues: {
180
- [keyAttr]: term,
181
- },
182
- xExpressionAttributeNames: {
183
- [attrKeyHash]: fieldName,
184
- },
185
- xFilterExpression: `contains (${attrKeyHash}, ${keyAttr})`,
186
- };
187
- return result;
188
- }
189
- operation__filterNot({ fieldName, selectorValues, }) {
190
- const selector = { ...selectorValues };
191
- const mConditions = [];
192
- Object.entries(selector).forEach(([conditionKey, conditionValue]) => {
193
- if (hasQueryConditionValue(conditionKey)) {
194
- const _conditionKey01 = conditionKey;
195
- if (_conditionKey01 === "$beginsWith") {
196
- query_validator_1.QueryValidatorCheck.beginWith(conditionValue);
197
- const _queryConditions = this.operation__filterBeginsWith({
198
- fieldName: fieldName,
199
- term: conditionValue,
200
- });
201
- mConditions.push(_queryConditions);
202
- }
203
- else if (_conditionKey01 === "$between") {
204
- query_validator_1.QueryValidatorCheck.between(conditionValue);
205
- const [from, to] = conditionValue;
206
- const _queryConditions = this.operation__filterBetween({
207
- fieldName: fieldName,
208
- from,
209
- to,
210
- });
211
- mConditions.push(_queryConditions);
212
- }
213
- else {
214
- const conditionExpr = conditionMap[conditionKey];
215
- if (conditionExpr) {
216
- const _queryConditions = this.operation__helperFilterBasic({
217
- fieldName: fieldName,
218
- val: conditionValue,
219
- conditionExpr: conditionExpr,
220
- });
221
- mConditions.push(_queryConditions);
222
- }
223
- else {
224
- query_validator_1.QueryValidatorCheck.throwQueryNotFound(conditionKey);
225
- }
226
- }
227
- }
228
- });
229
- return mConditions;
230
- }
231
- operation__filterBetween({ fieldName, from, to, }) {
232
- const _attrKeyHash = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
233
- const _fromKey = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)(":");
234
- const _toKey = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)(":");
235
- const result = {
236
- xExpressionAttributeValues: {
237
- [_fromKey]: from,
238
- [_toKey]: to,
239
- },
240
- xExpressionAttributeNames: {
241
- [_attrKeyHash]: fieldName,
242
- },
243
- xFilterExpression: [_attrKeyHash, "between", _fromKey, "and", _toKey].join(" "),
244
- };
245
- return result;
246
- }
247
- operation__filterBeginsWith({ fieldName, term }) {
248
- const _attrKeyHash = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
249
- const keyAttr = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)(":");
250
- const result = {
251
- xExpressionAttributeValues: {
252
- [keyAttr]: term,
253
- },
254
- xExpressionAttributeNames: {
255
- [_attrKeyHash]: fieldName,
256
- },
257
- xFilterExpression: `begins_with (${_attrKeyHash}, ${keyAttr})`,
258
- };
259
- return result;
260
- }
261
- operation__translateAdvancedQueryOperation({ fieldName, queryObject, }) {
262
- const queryConditions = [];
263
- const notConditions = [];
264
- const orConditions = [];
265
- Object.entries(queryObject).forEach(([condKey, conditionValue]) => {
266
- const conditionKey = condKey;
267
- if (conditionValue !== undefined) {
268
- if (conditionKey === "$between") {
269
- query_validator_1.QueryValidatorCheck.between(conditionValue);
270
- const [from, to] = conditionValue;
271
- const _queryConditions = this.operation__filterBetween({
272
- fieldName: fieldName,
273
- from,
274
- to,
275
- });
276
- queryConditions.push(_queryConditions);
277
- }
278
- else if (conditionKey === "$beginsWith") {
279
- query_validator_1.QueryValidatorCheck.beginWith(conditionValue);
280
- const _queryConditions = this.operation__filterBeginsWith({
281
- fieldName: fieldName,
282
- term: conditionValue,
283
- });
284
- queryConditions.push(_queryConditions);
285
- }
286
- else if (conditionKey === "$contains") {
287
- query_validator_1.QueryValidatorCheck.contains(conditionValue);
288
- const _queryConditions = this.operation__filterContains({
289
- fieldName: fieldName,
290
- term: conditionValue,
291
- });
292
- queryConditions.push(_queryConditions);
293
- }
294
- else if (conditionKey === "$in") {
295
- query_validator_1.QueryValidatorCheck.in_query(conditionValue);
296
- const _queryConditions = this.operation__filterIn({
297
- fieldName: fieldName,
298
- attrValues: conditionValue,
299
- });
300
- queryConditions.push(_queryConditions);
301
- }
302
- else if (conditionKey === "$nin") {
303
- query_validator_1.QueryValidatorCheck.notIn(conditionValue);
304
- const _queryConditions = this.operation__filterIn({
305
- fieldName: fieldName,
306
- attrValues: conditionValue,
307
- });
308
- _queryConditions.xFilterExpression = `NOT ${_queryConditions.xFilterExpression}`;
309
- queryConditions.push(_queryConditions);
310
- }
311
- else if (conditionKey === "$elemMatch") {
312
- query_validator_1.QueryValidatorCheck.elemMatch(conditionValue);
313
- const elemMatchConditions = this.operation__filterElemMatch({
314
- fieldName: fieldName,
315
- attrValues: conditionValue,
316
- });
317
- if (elemMatchConditions?.length) {
318
- elemMatchConditions.forEach((cond) => {
319
- orConditions.push(cond);
320
- });
321
- }
322
- }
323
- else if (conditionKey === "$nestedMatch") {
324
- query_validator_1.QueryValidatorCheck.nestedMatch(conditionValue);
325
- const nestedMatchConditions = this.operation__filterNestedMatchObject({
326
- fieldName: fieldName,
327
- attrValues: conditionValue,
328
- });
329
- if (nestedMatchConditions?.length) {
330
- nestedMatchConditions.forEach((cond) => {
331
- queryConditions.push(cond);
332
- });
333
- }
334
- }
335
- else if (conditionKey === "$not") {
336
- query_validator_1.QueryValidatorCheck.not_query(conditionValue);
337
- const _queryConditions = this.operation__filterNot({
338
- fieldName: fieldName,
339
- selectorValues: conditionValue,
340
- });
341
- if (_queryConditions?.length) {
342
- _queryConditions.forEach((cond) => {
343
- notConditions.push(cond);
344
- });
345
- }
346
- }
347
- else if (conditionKey === "$notContains") {
348
- query_validator_1.QueryValidatorCheck.notContains(conditionValue);
349
- const _queryConditions = this.operation__filterContains({
350
- fieldName: fieldName,
351
- term: conditionValue,
352
- });
353
- _queryConditions.xFilterExpression = `NOT (${_queryConditions.xFilterExpression})`;
354
- queryConditions.push(_queryConditions);
355
- }
356
- else if (conditionKey === "$exists") {
357
- query_validator_1.QueryValidatorCheck.exists(conditionValue);
358
- if (String(conditionValue) === "true") {
359
- const _queryConditions = this.operation__filterFieldExist({
360
- fieldName: fieldName,
361
- });
362
- queryConditions.push(_queryConditions);
363
- }
364
- else if (String(conditionValue) === "false") {
365
- const _queryConditions = this.operation__filterFieldNotExist({
366
- fieldName: fieldName,
367
- });
368
- queryConditions.push(_queryConditions);
369
- }
370
- }
371
- else {
372
- const conditionExpr = conditionMap[conditionKey];
373
- if (hasQueryConditionValue(conditionKey) && conditionExpr) {
374
- const _queryConditions = this.operation__helperFilterBasic({
375
- fieldName: fieldName,
376
- val: conditionValue,
377
- conditionExpr: conditionExpr,
378
- });
379
- queryConditions.push(_queryConditions);
380
- }
381
- else {
382
- query_validator_1.QueryValidatorCheck.throwQueryNotFound(conditionKey);
383
- }
384
- }
385
- }
386
- });
387
- logging_service_1.LoggingService.log(queryConditions);
388
- return { queryConditions, notConditions, orConditions };
389
- }
390
- operation_translateBasicQueryOperation({ fieldName, queryObject }) {
391
- const _queryConditions = this.operation__helperFilterBasic({
392
- fieldName: fieldName,
393
- val: queryObject,
394
- conditionExpr: "=",
395
- });
396
- return _queryConditions;
397
- }
398
- processQueryFilter({ queryDefs, projectionFields, }) {
399
- let AND_queryConditions = [];
400
- let OR_queryConditions = [];
401
- const OR_queryConditions_multiFields = [];
402
- let NOT_queryConditions = [];
403
- let NOT_inside_OR_queryConditions = [];
404
- const AND_FilterExpressionArray = [];
405
- const OR_FilterExpressionArray = [];
406
- const OR_FilterExpressionMultiFieldsArray = [];
407
- const NOT_FilterExpressionArray = [];
408
- const NOT_inside_OR_FilterExpressionArray = [];
409
- Object.entries(queryDefs).forEach(([conditionKey, conditionValue]) => {
410
- if (conditionKey === "$or") {
411
- query_validator_1.QueryValidatorCheck.or_query(conditionValue);
412
- const orArray = conditionValue;
413
- orArray.forEach((orQuery) => {
414
- const OR_queryMultiConditionsPrivate = [];
415
- const hasMultiField = Object.keys(orQuery || {}).length > 1;
416
- Object.entries(orQuery).forEach(([fieldName, orQueryObjectOrValue]) => {
417
- if (orQueryObjectOrValue !== undefined) {
418
- if (orQueryObjectOrValue && typeof orQueryObjectOrValue === "object") {
419
- const orQueryCond01 = this.operation__translateAdvancedQueryOperation({
420
- fieldName,
421
- queryObject: orQueryObjectOrValue,
422
- });
423
- if (orQueryCond01?.queryConditions?.length) {
424
- if (hasMultiField) {
425
- OR_queryMultiConditionsPrivate.push(...orQueryCond01.queryConditions);
426
- }
427
- else {
428
- OR_queryConditions = [...OR_queryConditions, ...orQueryCond01.queryConditions];
429
- NOT_inside_OR_queryConditions = [
430
- ...NOT_inside_OR_queryConditions,
431
- ...orQueryCond01.notConditions,
432
- ];
433
- }
434
- }
435
- }
436
- else {
437
- const orQueryCondition01 = this.operation_translateBasicQueryOperation({
438
- fieldName,
439
- queryObject: orQueryObjectOrValue,
440
- });
441
- OR_queryConditions.push(orQueryCondition01);
442
- }
443
- }
444
- });
445
- if (OR_queryMultiConditionsPrivate.length) {
446
- OR_queryConditions_multiFields.push(OR_queryMultiConditionsPrivate);
447
- }
448
- });
449
- }
450
- else if (conditionKey === "$and") {
451
- const andArray = conditionValue;
452
- query_validator_1.QueryValidatorCheck.and_query(andArray);
453
- andArray.forEach((andQuery) => {
454
- Object.entries(andQuery).forEach(([fieldName, andQueryObjectOrValue]) => {
455
- if (andQueryObjectOrValue !== undefined) {
456
- if (andQueryObjectOrValue && typeof andQueryObjectOrValue === "object") {
457
- const andQueryCond01 = this.operation__translateAdvancedQueryOperation({
458
- fieldName,
459
- queryObject: andQueryObjectOrValue,
460
- });
461
- AND_queryConditions = [...AND_queryConditions, ...andQueryCond01.queryConditions];
462
- NOT_queryConditions = [...NOT_queryConditions, ...andQueryCond01.notConditions];
463
- OR_queryConditions = [...OR_queryConditions, ...andQueryCond01.orConditions];
464
- }
465
- else {
466
- const andQueryCondition01 = this.operation_translateBasicQueryOperation({
467
- fieldName,
468
- queryObject: andQueryObjectOrValue,
469
- });
470
- AND_queryConditions = [...AND_queryConditions, andQueryCondition01];
471
- }
472
- }
473
- });
474
- });
475
- }
476
- else {
477
- if (conditionKey) {
478
- if (conditionValue !== undefined) {
479
- if (conditionValue && typeof conditionValue === "object") {
480
- const queryCond01 = this.operation__translateAdvancedQueryOperation({
481
- fieldName: conditionKey,
482
- queryObject: conditionValue,
483
- });
484
- AND_queryConditions = [...AND_queryConditions, ...queryCond01.queryConditions];
485
- NOT_queryConditions = [...NOT_queryConditions, ...queryCond01.notConditions];
486
- OR_queryConditions = [...OR_queryConditions, ...queryCond01.orConditions];
487
- }
488
- else {
489
- const queryCondition01 = this.operation_translateBasicQueryOperation({
490
- fieldName: conditionKey,
491
- queryObject: conditionValue,
492
- });
493
- AND_queryConditions = [...AND_queryConditions, queryCondition01];
494
- }
495
- }
496
- }
497
- }
498
- });
499
- let _expressionAttributeValues = {};
500
- let _expressionAttributeNames = {};
501
- let _projectionExpression;
502
- AND_queryConditions.forEach((item) => {
503
- _expressionAttributeNames = {
504
- ..._expressionAttributeNames,
505
- ...item.xExpressionAttributeNames,
506
- };
507
- _expressionAttributeValues = {
508
- ..._expressionAttributeValues,
509
- ...item.xExpressionAttributeValues,
510
- };
511
- AND_FilterExpressionArray.push(item.xFilterExpression);
512
- });
513
- OR_queryConditions.forEach((item2) => {
514
- _expressionAttributeNames = {
515
- ..._expressionAttributeNames,
516
- ...item2.xExpressionAttributeNames,
517
- };
518
- _expressionAttributeValues = {
519
- ..._expressionAttributeValues,
520
- ...item2.xExpressionAttributeValues,
521
- };
522
- OR_FilterExpressionArray.push(item2.xFilterExpression);
523
- });
524
- OR_queryConditions_multiFields.forEach((item2a) => {
525
- const xFilterExpression = [];
526
- item2a.forEach((item01) => {
527
- _expressionAttributeNames = {
528
- ..._expressionAttributeNames,
529
- ...item01.xExpressionAttributeNames,
530
- };
531
- _expressionAttributeValues = {
532
- ..._expressionAttributeValues,
533
- ...item01.xExpressionAttributeValues,
534
- };
535
- xFilterExpression.push(item01.xFilterExpression);
536
- });
537
- OR_FilterExpressionMultiFieldsArray.push(xFilterExpression);
538
- });
539
- NOT_queryConditions.forEach((item03) => {
540
- _expressionAttributeNames = {
541
- ..._expressionAttributeNames,
542
- ...item03.xExpressionAttributeNames,
543
- };
544
- _expressionAttributeValues = {
545
- ..._expressionAttributeValues,
546
- ...item03.xExpressionAttributeValues,
547
- };
548
- NOT_FilterExpressionArray.push(item03.xFilterExpression);
549
- });
550
- NOT_inside_OR_queryConditions.forEach((item4) => {
551
- _expressionAttributeNames = {
552
- ..._expressionAttributeNames,
553
- ...item4.xExpressionAttributeNames,
554
- };
555
- _expressionAttributeValues = {
556
- ..._expressionAttributeValues,
557
- ...item4.xExpressionAttributeValues,
558
- };
559
- NOT_inside_OR_FilterExpressionArray.push(item4.xFilterExpression);
560
- });
561
- let _andfilterExpression = "";
562
- let _orfilterExpression = "";
563
- let _notfilterExpression = "";
564
- let _notInsideOrFilterExpression = "";
565
- if (AND_FilterExpressionArray?.length) {
566
- _andfilterExpression = AND_FilterExpressionArray.join(" AND ").trim();
567
- }
568
- if (OR_FilterExpressionMultiFieldsArray?.length) {
569
- const subQuery = [];
570
- OR_FilterExpressionMultiFieldsArray.forEach((item01) => {
571
- const val = item01.join(" AND ").trim();
572
- subQuery.push(`(${val})`);
573
- });
574
- _orfilterExpression = [...OR_FilterExpressionArray, ...subQuery].join(" OR ").trim();
575
- }
576
- else if (OR_FilterExpressionArray?.length) {
577
- _orfilterExpression = OR_FilterExpressionArray.join(" OR ").trim();
578
- }
579
- if (NOT_FilterExpressionArray?.length) {
580
- _notfilterExpression = NOT_FilterExpressionArray.join(" AND ").trim();
581
- _notfilterExpression = `NOT (${_notfilterExpression})`;
582
- }
583
- if (NOT_inside_OR_FilterExpressionArray?.length) {
584
- _notInsideOrFilterExpression = NOT_inside_OR_FilterExpressionArray.join(" OR ").trim();
585
- _notInsideOrFilterExpression = `NOT (${_notInsideOrFilterExpression})`;
586
- }
587
- let allFilters = [
588
- _andfilterExpression,
589
- _notfilterExpression,
590
- _orfilterExpression,
591
- _notInsideOrFilterExpression,
592
- ].filter((f) => f);
593
- if (allFilters?.length && allFilters.length > 1) {
594
- allFilters = allFilters.map((f) => `(${f})`);
595
- }
596
- const _filterExpression = allFilters.join(" AND ");
597
- if (projectionFields?.length && Array.isArray(projectionFields)) {
598
- const _projection_expressionAttributeNames = {};
599
- projectionFields.forEach((field) => {
600
- if (field && typeof field === "string") {
601
- const attrKeyHash = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
602
- _projection_expressionAttributeNames[attrKeyHash] = field;
603
- }
604
- });
605
- _projectionExpression = Object.keys(_projection_expressionAttributeNames).join(", ");
606
- _expressionAttributeNames = {
607
- ..._projection_expressionAttributeNames,
608
- ..._expressionAttributeNames,
609
- };
610
- }
611
- const _expressionAttributeValuesFinal = util_service_1.UtilService.objectHasAnyProperty(_expressionAttributeValues)
612
- ? _expressionAttributeValues
613
- : undefined;
614
- const _expressionAttributeNamesFinal = util_service_1.UtilService.objectHasAnyProperty(_expressionAttributeNames)
615
- ? _expressionAttributeNames
616
- : undefined;
617
- const queryExpressions = {
618
- expressionAttributeValues: _expressionAttributeValuesFinal,
619
- filterExpression: _filterExpression,
620
- projectionExpressionAttr: _projectionExpression,
621
- expressionAttributeNames: _expressionAttributeNamesFinal,
622
- };
623
- logging_service_1.LoggingService.log(JSON.stringify({ queryExpressions }, null, 2));
624
- return queryExpressions;
625
- }
626
- }
627
- exports.DynamoFilterQueryOperation = DynamoFilterQueryOperation;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DynamoFilterQueryOperation = void 0;
4
+ const query_validator_1 = require("./../helpers/query-validator");
5
+ const logging_service_1 = require("../helpers/logging-service");
6
+ const util_service_1 = require("../helpers/util-service");
7
+ const errors_1 = require("../helpers/errors");
8
+ const dynamo_helper_1 = require("./dynamo-helper");
9
+ const keyConditionMap = {
10
+ $eq: "=",
11
+ $lt: "<",
12
+ $lte: "<=",
13
+ $gt: ">",
14
+ $gte: ">=",
15
+ $beginsWith: "",
16
+ $between: "",
17
+ };
18
+ const conditionMapPre = {
19
+ $ne: "<>",
20
+ $exists: "",
21
+ $in: "",
22
+ $nin: "",
23
+ $not: "",
24
+ $contains: "",
25
+ $notContains: "",
26
+ $elemMatch: "",
27
+ $nestedMatch: "",
28
+ };
29
+ const conditionMap = { ...keyConditionMap, ...conditionMapPre };
30
+ function hasQueryConditionValue(key) {
31
+ if (key && Object.keys(conditionMap).includes(key) && conditionMap[key]) {
32
+ return true;
33
+ }
34
+ return false;
35
+ }
36
+ class DynamoFilterQueryOperation {
37
+ operation__filterFieldExist({ fieldName }) {
38
+ const attrKeyHash = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
39
+ const result = {
40
+ xExpressionAttributeNames: {
41
+ [attrKeyHash]: fieldName,
42
+ },
43
+ xFilterExpression: `attribute_exists (${attrKeyHash})`,
44
+ };
45
+ return result;
46
+ }
47
+ operation__filterFieldNotExist({ fieldName }) {
48
+ const attrKeyHash = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
49
+ const result = {
50
+ xExpressionAttributeNames: {
51
+ [attrKeyHash]: fieldName,
52
+ },
53
+ xFilterExpression: `attribute_not_exists (${attrKeyHash})`,
54
+ };
55
+ return result;
56
+ }
57
+ operation__helperFilterBasic({ fieldName, val, conditionExpr, }) {
58
+ const keyAttr = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)(":");
59
+ const attrKeyHash = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
60
+ const result = {
61
+ xExpressionAttributeValues: {
62
+ [keyAttr]: val,
63
+ },
64
+ xExpressionAttributeNames: {
65
+ [attrKeyHash]: fieldName,
66
+ },
67
+ xFilterExpression: [attrKeyHash, conditionExpr, keyAttr].join(" "),
68
+ };
69
+ return result;
70
+ }
71
+ operation__filterIn({ fieldName, attrValues, }) {
72
+ const expressAttrVal = {};
73
+ const expressAttrName = {};
74
+ const valuesVariable = [];
75
+ const attrKeyVariale = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
76
+ expressAttrName[attrKeyVariale] = fieldName;
77
+ attrValues.forEach((item, i) => {
78
+ const keyAttr = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)(":");
79
+ if (typeof item === "number") {
80
+ expressAttrVal[keyAttr] = { N: item };
81
+ }
82
+ else {
83
+ expressAttrVal[keyAttr] = { S: item };
84
+ }
85
+ valuesVariable.push(keyAttr);
86
+ });
87
+ const filterExpressionValue01 = `${attrKeyVariale} IN (${valuesVariable.join(", ")})`;
88
+ const result = {
89
+ xExpressionAttributeValues: {
90
+ ...expressAttrVal,
91
+ },
92
+ xExpressionAttributeNames: {
93
+ ...expressAttrName,
94
+ },
95
+ xFilterExpression: filterExpressionValue01,
96
+ };
97
+ return result;
98
+ }
99
+ operation__filterIn_00({ fieldName, attrValues, }) {
100
+ const expressAttrVal = {};
101
+ const expressAttrName = {};
102
+ const filterExpress = [];
103
+ const _attrKeyHash = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
104
+ expressAttrName[_attrKeyHash] = fieldName;
105
+ attrValues.forEach((item) => {
106
+ const keyAttr = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)(":");
107
+ expressAttrVal[keyAttr] = item;
108
+ filterExpress.push(`${_attrKeyHash} = ${keyAttr}`);
109
+ });
110
+ const _filterExpression = filterExpress.join(" OR ").trim();
111
+ const _filterExpressionValue = filterExpress.length > 1 ? `(${_filterExpression})` : _filterExpression;
112
+ const result = {
113
+ xExpressionAttributeValues: {
114
+ ...expressAttrVal,
115
+ },
116
+ xExpressionAttributeNames: {
117
+ ...expressAttrName,
118
+ },
119
+ xFilterExpression: _filterExpressionValue,
120
+ };
121
+ return result;
122
+ }
123
+ operation__filterElemMatch({ fieldName, attrValues, }) {
124
+ const result = [];
125
+ attrValues.$in.forEach((term) => {
126
+ const query01 = this.operation__filterContains({ term, fieldName });
127
+ result.push(query01);
128
+ });
129
+ return result;
130
+ }
131
+ operation__filterNestedMatchObject({ fieldName, attrValues, }) {
132
+ const results = [];
133
+ Object.entries(attrValues).forEach(([subFieldName, queryval]) => {
134
+ let _queryValue;
135
+ if (queryval && typeof queryval === "object") {
136
+ _queryValue = { ...queryval };
137
+ }
138
+ else {
139
+ _queryValue = { $eq: queryval };
140
+ }
141
+ Object.entries(_queryValue).forEach(([condKey, conditionValue]) => {
142
+ const conditionKey = condKey;
143
+ if (!Object.keys(keyConditionMap).includes(conditionKey)) {
144
+ throw errors_1.MocodyErrorUtilsService.mocody_helper_createFriendlyError(`Invalid query key: ${conditionKey} @ NestedMatchObject`);
145
+ }
146
+ const conditionExpr = keyConditionMap[conditionKey];
147
+ const attrValue = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)(":");
148
+ const attrKeyHash = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
149
+ const parentFieldName = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
150
+ if (conditionExpr) {
151
+ const result = {
152
+ xExpressionAttributeValues: {
153
+ [attrValue]: conditionValue,
154
+ },
155
+ xExpressionAttributeNames: {
156
+ [attrKeyHash]: subFieldName,
157
+ [parentFieldName]: fieldName,
158
+ },
159
+ xFilterExpression: [`${parentFieldName}.${attrKeyHash}`, conditionExpr, attrValue].join(" "),
160
+ };
161
+ results.push(result);
162
+ }
163
+ else {
164
+ if (conditionKey === "$between") {
165
+ query_validator_1.QueryValidatorCheck.between(conditionValue);
166
+ const fromKey = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)(":");
167
+ const toKey = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)(":");
168
+ const [fromVal, toVal] = conditionValue;
169
+ const result = {
170
+ xExpressionAttributeValues: {
171
+ [fromKey]: fromVal,
172
+ [toKey]: toVal,
173
+ },
174
+ xExpressionAttributeNames: {
175
+ [attrKeyHash]: subFieldName,
176
+ [parentFieldName]: fieldName,
177
+ },
178
+ xFilterExpression: [`${parentFieldName}.${attrKeyHash}`, "between", fromKey, "and", toKey].join(" "),
179
+ };
180
+ results.push(result);
181
+ }
182
+ else if (conditionKey === "$beginsWith") {
183
+ const result = {
184
+ xExpressionAttributeValues: {
185
+ [attrValue]: conditionValue,
186
+ },
187
+ xExpressionAttributeNames: {
188
+ [attrKeyHash]: subFieldName,
189
+ [parentFieldName]: fieldName,
190
+ },
191
+ xFilterExpression: `begins_with (${parentFieldName}.${attrKeyHash}, ${attrValue})`,
192
+ };
193
+ results.push(result);
194
+ }
195
+ else {
196
+ throw errors_1.MocodyErrorUtilsService.mocody_helper_createFriendlyError(`Query key: ${conditionKey} not currently supported`);
197
+ }
198
+ }
199
+ });
200
+ });
201
+ return results;
202
+ }
203
+ operation__filterContains({ fieldName, term }) {
204
+ const attrKeyHash = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
205
+ const keyAttr = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)(":");
206
+ const result = {
207
+ xExpressionAttributeValues: {
208
+ [keyAttr]: term,
209
+ },
210
+ xExpressionAttributeNames: {
211
+ [attrKeyHash]: fieldName,
212
+ },
213
+ xFilterExpression: `contains (${attrKeyHash}, ${keyAttr})`,
214
+ };
215
+ return result;
216
+ }
217
+ operation__filterNot({ fieldName, selectorValues, }) {
218
+ const selector = { ...selectorValues };
219
+ const mConditions = [];
220
+ Object.entries(selector).forEach(([conditionKey, conditionValue]) => {
221
+ if (hasQueryConditionValue(conditionKey)) {
222
+ const _conditionKey01 = conditionKey;
223
+ if (_conditionKey01 === "$beginsWith") {
224
+ query_validator_1.QueryValidatorCheck.beginWith(conditionValue);
225
+ const _queryConditions = this.operation__filterBeginsWith({
226
+ fieldName: fieldName,
227
+ term: conditionValue,
228
+ });
229
+ mConditions.push(_queryConditions);
230
+ }
231
+ else if (_conditionKey01 === "$between") {
232
+ query_validator_1.QueryValidatorCheck.between(conditionValue);
233
+ const [from, to] = conditionValue;
234
+ const _queryConditions = this.operation__filterBetween({
235
+ fieldName: fieldName,
236
+ from,
237
+ to,
238
+ });
239
+ mConditions.push(_queryConditions);
240
+ }
241
+ else {
242
+ const conditionExpr = conditionMap[conditionKey];
243
+ if (conditionExpr) {
244
+ const _queryConditions = this.operation__helperFilterBasic({
245
+ fieldName: fieldName,
246
+ val: conditionValue,
247
+ conditionExpr: conditionExpr,
248
+ });
249
+ mConditions.push(_queryConditions);
250
+ }
251
+ else {
252
+ query_validator_1.QueryValidatorCheck.throwQueryNotFound(conditionKey);
253
+ }
254
+ }
255
+ }
256
+ });
257
+ return mConditions;
258
+ }
259
+ operation__filterBetween({ fieldName, from, to, }) {
260
+ const _attrKeyHash = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
261
+ const _fromKey = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)(":");
262
+ const _toKey = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)(":");
263
+ const result = {
264
+ xExpressionAttributeValues: {
265
+ [_fromKey]: from,
266
+ [_toKey]: to,
267
+ },
268
+ xExpressionAttributeNames: {
269
+ [_attrKeyHash]: fieldName,
270
+ },
271
+ xFilterExpression: [_attrKeyHash, "between", _fromKey, "and", _toKey].join(" "),
272
+ };
273
+ return result;
274
+ }
275
+ operation__filterBeginsWith({ fieldName, term }) {
276
+ const _attrKeyHash = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
277
+ const keyAttr = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)(":");
278
+ const result = {
279
+ xExpressionAttributeValues: {
280
+ [keyAttr]: term,
281
+ },
282
+ xExpressionAttributeNames: {
283
+ [_attrKeyHash]: fieldName,
284
+ },
285
+ xFilterExpression: `begins_with (${_attrKeyHash}, ${keyAttr})`,
286
+ };
287
+ return result;
288
+ }
289
+ operation__translateAdvancedQueryOperation({ fieldName, queryObject, }) {
290
+ const queryConditions = [];
291
+ const notConditions = [];
292
+ const orConditions = [];
293
+ Object.entries(queryObject).forEach(([condKey, conditionValue]) => {
294
+ const conditionKey = condKey;
295
+ if (conditionValue !== undefined) {
296
+ if (conditionKey === "$between") {
297
+ query_validator_1.QueryValidatorCheck.between(conditionValue);
298
+ const [from, to] = conditionValue;
299
+ const _queryConditions = this.operation__filterBetween({
300
+ fieldName: fieldName,
301
+ from,
302
+ to,
303
+ });
304
+ queryConditions.push(_queryConditions);
305
+ }
306
+ else if (conditionKey === "$beginsWith") {
307
+ query_validator_1.QueryValidatorCheck.beginWith(conditionValue);
308
+ const _queryConditions = this.operation__filterBeginsWith({
309
+ fieldName: fieldName,
310
+ term: conditionValue,
311
+ });
312
+ queryConditions.push(_queryConditions);
313
+ }
314
+ else if (conditionKey === "$contains") {
315
+ query_validator_1.QueryValidatorCheck.contains(conditionValue);
316
+ const _queryConditions = this.operation__filterContains({
317
+ fieldName: fieldName,
318
+ term: conditionValue,
319
+ });
320
+ queryConditions.push(_queryConditions);
321
+ }
322
+ else if (conditionKey === "$in") {
323
+ query_validator_1.QueryValidatorCheck.in_query(conditionValue);
324
+ const _queryConditions = this.operation__filterIn({
325
+ fieldName: fieldName,
326
+ attrValues: conditionValue,
327
+ });
328
+ queryConditions.push(_queryConditions);
329
+ }
330
+ else if (conditionKey === "$nin") {
331
+ query_validator_1.QueryValidatorCheck.notIn(conditionValue);
332
+ const _queryConditions = this.operation__filterIn({
333
+ fieldName: fieldName,
334
+ attrValues: conditionValue,
335
+ });
336
+ _queryConditions.xFilterExpression = `NOT ${_queryConditions.xFilterExpression}`;
337
+ queryConditions.push(_queryConditions);
338
+ }
339
+ else if (conditionKey === "$elemMatch") {
340
+ query_validator_1.QueryValidatorCheck.elemMatch(conditionValue);
341
+ const elemMatchConditions = this.operation__filterElemMatch({
342
+ fieldName: fieldName,
343
+ attrValues: conditionValue,
344
+ });
345
+ if (elemMatchConditions?.length) {
346
+ elemMatchConditions.forEach((cond) => {
347
+ orConditions.push(cond);
348
+ });
349
+ }
350
+ }
351
+ else if (conditionKey === "$nestedMatch") {
352
+ query_validator_1.QueryValidatorCheck.nestedMatch(conditionValue);
353
+ const nestedMatchConditions = this.operation__filterNestedMatchObject({
354
+ fieldName: fieldName,
355
+ attrValues: conditionValue,
356
+ });
357
+ if (nestedMatchConditions?.length) {
358
+ nestedMatchConditions.forEach((cond) => {
359
+ queryConditions.push(cond);
360
+ });
361
+ }
362
+ }
363
+ else if (conditionKey === "$not") {
364
+ query_validator_1.QueryValidatorCheck.not_query(conditionValue);
365
+ const _queryConditions = this.operation__filterNot({
366
+ fieldName: fieldName,
367
+ selectorValues: conditionValue,
368
+ });
369
+ if (_queryConditions?.length) {
370
+ _queryConditions.forEach((cond) => {
371
+ notConditions.push(cond);
372
+ });
373
+ }
374
+ }
375
+ else if (conditionKey === "$notContains") {
376
+ query_validator_1.QueryValidatorCheck.notContains(conditionValue);
377
+ const _queryConditions = this.operation__filterContains({
378
+ fieldName: fieldName,
379
+ term: conditionValue,
380
+ });
381
+ _queryConditions.xFilterExpression = `NOT (${_queryConditions.xFilterExpression})`;
382
+ queryConditions.push(_queryConditions);
383
+ }
384
+ else if (conditionKey === "$exists") {
385
+ query_validator_1.QueryValidatorCheck.exists(conditionValue);
386
+ if (String(conditionValue) === "true") {
387
+ const _queryConditions = this.operation__filterFieldExist({
388
+ fieldName: fieldName,
389
+ });
390
+ queryConditions.push(_queryConditions);
391
+ }
392
+ else if (String(conditionValue) === "false") {
393
+ const _queryConditions = this.operation__filterFieldNotExist({
394
+ fieldName: fieldName,
395
+ });
396
+ queryConditions.push(_queryConditions);
397
+ }
398
+ }
399
+ else {
400
+ const conditionExpr = conditionMap[conditionKey];
401
+ if (hasQueryConditionValue(conditionKey) && conditionExpr) {
402
+ const _queryConditions = this.operation__helperFilterBasic({
403
+ fieldName: fieldName,
404
+ val: conditionValue,
405
+ conditionExpr: conditionExpr,
406
+ });
407
+ queryConditions.push(_queryConditions);
408
+ }
409
+ else {
410
+ query_validator_1.QueryValidatorCheck.throwQueryNotFound(conditionKey);
411
+ }
412
+ }
413
+ }
414
+ });
415
+ logging_service_1.LoggingService.log(queryConditions);
416
+ return { queryConditions, notConditions, orConditions };
417
+ }
418
+ operation_translateBasicQueryOperation({ fieldName, queryObject }) {
419
+ const _queryConditions = this.operation__helperFilterBasic({
420
+ fieldName: fieldName,
421
+ val: queryObject,
422
+ conditionExpr: "=",
423
+ });
424
+ return _queryConditions;
425
+ }
426
+ processQueryFilter({ queryDefs, projectionFields, }) {
427
+ let AND_queryConditions = [];
428
+ let OR_queryConditions = [];
429
+ const OR_queryConditions_multiFields = [];
430
+ let NOT_queryConditions = [];
431
+ let NOT_inside_OR_queryConditions = [];
432
+ const AND_FilterExpressionArray = [];
433
+ const OR_FilterExpressionArray = [];
434
+ const OR_FilterExpressionMultiFieldsArray = [];
435
+ const NOT_FilterExpressionArray = [];
436
+ const NOT_inside_OR_FilterExpressionArray = [];
437
+ Object.entries(queryDefs).forEach(([conditionKey, conditionValue]) => {
438
+ if (conditionKey === "$or") {
439
+ query_validator_1.QueryValidatorCheck.or_query(conditionValue);
440
+ const orArray = conditionValue;
441
+ orArray.forEach((orQuery) => {
442
+ const OR_queryMultiConditionsPrivate = [];
443
+ const hasMultiField = Object.keys(orQuery || {}).length > 1;
444
+ Object.entries(orQuery).forEach(([fieldName, orQueryObjectOrValue]) => {
445
+ if (orQueryObjectOrValue !== undefined) {
446
+ if (orQueryObjectOrValue && typeof orQueryObjectOrValue === "object") {
447
+ const orQueryCond01 = this.operation__translateAdvancedQueryOperation({
448
+ fieldName,
449
+ queryObject: orQueryObjectOrValue,
450
+ });
451
+ if (orQueryCond01?.queryConditions?.length) {
452
+ if (hasMultiField) {
453
+ OR_queryMultiConditionsPrivate.push(...orQueryCond01.queryConditions);
454
+ }
455
+ else {
456
+ OR_queryConditions = [...OR_queryConditions, ...orQueryCond01.queryConditions];
457
+ NOT_inside_OR_queryConditions = [
458
+ ...NOT_inside_OR_queryConditions,
459
+ ...orQueryCond01.notConditions,
460
+ ];
461
+ }
462
+ }
463
+ }
464
+ else {
465
+ const orQueryCondition01 = this.operation_translateBasicQueryOperation({
466
+ fieldName,
467
+ queryObject: orQueryObjectOrValue,
468
+ });
469
+ OR_queryConditions.push(orQueryCondition01);
470
+ }
471
+ }
472
+ });
473
+ if (OR_queryMultiConditionsPrivate.length) {
474
+ OR_queryConditions_multiFields.push(OR_queryMultiConditionsPrivate);
475
+ }
476
+ });
477
+ }
478
+ else if (conditionKey === "$and") {
479
+ const andArray = conditionValue;
480
+ query_validator_1.QueryValidatorCheck.and_query(andArray);
481
+ andArray.forEach((andQuery) => {
482
+ Object.entries(andQuery).forEach(([fieldName, andQueryObjectOrValue]) => {
483
+ if (andQueryObjectOrValue !== undefined) {
484
+ if (andQueryObjectOrValue && typeof andQueryObjectOrValue === "object") {
485
+ const andQueryCond01 = this.operation__translateAdvancedQueryOperation({
486
+ fieldName,
487
+ queryObject: andQueryObjectOrValue,
488
+ });
489
+ AND_queryConditions = [...AND_queryConditions, ...andQueryCond01.queryConditions];
490
+ NOT_queryConditions = [...NOT_queryConditions, ...andQueryCond01.notConditions];
491
+ OR_queryConditions = [...OR_queryConditions, ...andQueryCond01.orConditions];
492
+ }
493
+ else {
494
+ const andQueryCondition01 = this.operation_translateBasicQueryOperation({
495
+ fieldName,
496
+ queryObject: andQueryObjectOrValue,
497
+ });
498
+ AND_queryConditions = [...AND_queryConditions, andQueryCondition01];
499
+ }
500
+ }
501
+ });
502
+ });
503
+ }
504
+ else {
505
+ if (conditionKey) {
506
+ if (conditionValue !== undefined) {
507
+ if (conditionValue && typeof conditionValue === "object") {
508
+ const queryCond01 = this.operation__translateAdvancedQueryOperation({
509
+ fieldName: conditionKey,
510
+ queryObject: conditionValue,
511
+ });
512
+ AND_queryConditions = [...AND_queryConditions, ...queryCond01.queryConditions];
513
+ NOT_queryConditions = [...NOT_queryConditions, ...queryCond01.notConditions];
514
+ OR_queryConditions = [...OR_queryConditions, ...queryCond01.orConditions];
515
+ }
516
+ else {
517
+ const queryCondition01 = this.operation_translateBasicQueryOperation({
518
+ fieldName: conditionKey,
519
+ queryObject: conditionValue,
520
+ });
521
+ AND_queryConditions = [...AND_queryConditions, queryCondition01];
522
+ }
523
+ }
524
+ }
525
+ }
526
+ });
527
+ let _expressionAttributeValues = {};
528
+ let _expressionAttributeNames = {};
529
+ let _projectionExpression;
530
+ AND_queryConditions.forEach((item) => {
531
+ _expressionAttributeNames = {
532
+ ..._expressionAttributeNames,
533
+ ...item.xExpressionAttributeNames,
534
+ };
535
+ _expressionAttributeValues = {
536
+ ..._expressionAttributeValues,
537
+ ...item.xExpressionAttributeValues,
538
+ };
539
+ AND_FilterExpressionArray.push(item.xFilterExpression);
540
+ });
541
+ OR_queryConditions.forEach((item2) => {
542
+ _expressionAttributeNames = {
543
+ ..._expressionAttributeNames,
544
+ ...item2.xExpressionAttributeNames,
545
+ };
546
+ _expressionAttributeValues = {
547
+ ..._expressionAttributeValues,
548
+ ...item2.xExpressionAttributeValues,
549
+ };
550
+ OR_FilterExpressionArray.push(item2.xFilterExpression);
551
+ });
552
+ OR_queryConditions_multiFields.forEach((item2a) => {
553
+ const xFilterExpression = [];
554
+ item2a.forEach((item01) => {
555
+ _expressionAttributeNames = {
556
+ ..._expressionAttributeNames,
557
+ ...item01.xExpressionAttributeNames,
558
+ };
559
+ _expressionAttributeValues = {
560
+ ..._expressionAttributeValues,
561
+ ...item01.xExpressionAttributeValues,
562
+ };
563
+ xFilterExpression.push(item01.xFilterExpression);
564
+ });
565
+ OR_FilterExpressionMultiFieldsArray.push(xFilterExpression);
566
+ });
567
+ NOT_queryConditions.forEach((item03) => {
568
+ _expressionAttributeNames = {
569
+ ..._expressionAttributeNames,
570
+ ...item03.xExpressionAttributeNames,
571
+ };
572
+ _expressionAttributeValues = {
573
+ ..._expressionAttributeValues,
574
+ ...item03.xExpressionAttributeValues,
575
+ };
576
+ NOT_FilterExpressionArray.push(item03.xFilterExpression);
577
+ });
578
+ NOT_inside_OR_queryConditions.forEach((item4) => {
579
+ _expressionAttributeNames = {
580
+ ..._expressionAttributeNames,
581
+ ...item4.xExpressionAttributeNames,
582
+ };
583
+ _expressionAttributeValues = {
584
+ ..._expressionAttributeValues,
585
+ ...item4.xExpressionAttributeValues,
586
+ };
587
+ NOT_inside_OR_FilterExpressionArray.push(item4.xFilterExpression);
588
+ });
589
+ let _andfilterExpression = "";
590
+ let _orfilterExpression = "";
591
+ let _notfilterExpression = "";
592
+ let _notInsideOrFilterExpression = "";
593
+ if (AND_FilterExpressionArray?.length) {
594
+ _andfilterExpression = AND_FilterExpressionArray.join(" AND ").trim();
595
+ }
596
+ if (OR_FilterExpressionMultiFieldsArray?.length) {
597
+ const subQuery = [];
598
+ OR_FilterExpressionMultiFieldsArray.forEach((item01) => {
599
+ const val = item01.join(" AND ").trim();
600
+ subQuery.push(`(${val})`);
601
+ });
602
+ _orfilterExpression = [...OR_FilterExpressionArray, ...subQuery].join(" OR ").trim();
603
+ }
604
+ else if (OR_FilterExpressionArray?.length) {
605
+ _orfilterExpression = OR_FilterExpressionArray.join(" OR ").trim();
606
+ }
607
+ if (NOT_FilterExpressionArray?.length) {
608
+ _notfilterExpression = NOT_FilterExpressionArray.join(" AND ").trim();
609
+ _notfilterExpression = `NOT (${_notfilterExpression})`;
610
+ }
611
+ if (NOT_inside_OR_FilterExpressionArray?.length) {
612
+ _notInsideOrFilterExpression = NOT_inside_OR_FilterExpressionArray.join(" OR ").trim();
613
+ _notInsideOrFilterExpression = `NOT (${_notInsideOrFilterExpression})`;
614
+ }
615
+ let allFilters = [
616
+ _andfilterExpression,
617
+ _notfilterExpression,
618
+ _orfilterExpression,
619
+ _notInsideOrFilterExpression,
620
+ ].filter((f) => f);
621
+ if (allFilters?.length && allFilters.length > 1) {
622
+ allFilters = allFilters.map((f) => `(${f})`);
623
+ }
624
+ const _filterExpression = allFilters.join(" AND ");
625
+ if (projectionFields?.length && Array.isArray(projectionFields)) {
626
+ const _projection_expressionAttributeNames = {};
627
+ projectionFields.forEach((field) => {
628
+ if (field && typeof field === "string") {
629
+ const attrKeyHash = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
630
+ _projection_expressionAttributeNames[attrKeyHash] = field;
631
+ }
632
+ });
633
+ _projectionExpression = Object.keys(_projection_expressionAttributeNames).join(", ");
634
+ _expressionAttributeNames = {
635
+ ..._projection_expressionAttributeNames,
636
+ ..._expressionAttributeNames,
637
+ };
638
+ }
639
+ const _expressionAttributeValuesFinal = util_service_1.UtilService.objectHasAnyProperty(_expressionAttributeValues)
640
+ ? _expressionAttributeValues
641
+ : undefined;
642
+ const _expressionAttributeNamesFinal = util_service_1.UtilService.objectHasAnyProperty(_expressionAttributeNames)
643
+ ? _expressionAttributeNames
644
+ : undefined;
645
+ const queryExpressions = {
646
+ expressionAttributeValues: _expressionAttributeValuesFinal,
647
+ filterExpression: _filterExpression,
648
+ projectionExpressionAttr: _projectionExpression,
649
+ expressionAttributeNames: _expressionAttributeNamesFinal,
650
+ };
651
+ logging_service_1.LoggingService.log(JSON.stringify({ queryExpressions }, null, 2));
652
+ return queryExpressions;
653
+ }
654
+ }
655
+ exports.DynamoFilterQueryOperation = DynamoFilterQueryOperation;
628
656
  //# sourceMappingURL=dynamo-filter-query-operation.js.map