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