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,798 +1,933 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DynamoDataOperation = void 0;
4
- const tslib_1 = require("tslib");
5
- const util_service_1 = require("./../helpers/util-service");
6
- const mocody_utils_1 = require("../helpers/mocody-utils");
7
- const model_1 = require("../model");
8
- const errors_1 = require("./../helpers/errors");
9
- const joi_1 = tslib_1.__importDefault(require("joi"));
10
- const base_joi_helper_1 = require("../helpers/base-joi-helper");
11
- const base_schema_1 = require("../core/base-schema");
12
- const dynamo_manage_table_1 = require("./dynamo-manage-table");
13
- const logging_service_1 = require("../helpers/logging-service");
14
- const dynamo_filter_query_operation_1 = require("./dynamo-filter-query-operation");
15
- const dynamo_query_scan_processor_1 = require("./dynamo-query-scan-processor");
16
- const lodash_1 = tslib_1.__importDefault(require("lodash"));
17
- const dynamo_helper_1 = require("./dynamo-helper");
18
- class DynamoDataOperation extends model_1.RepoModel {
19
- constructor({ schemaDef, dynamoDbInitializer, secondaryIndexOptions, featureEntityValue, baseTableName, strictRequiredFields, dataKeyGenerator, }) {
20
- super();
21
- Object.defineProperty(this, "_mocody_partitionKeyFieldName", {
22
- enumerable: true,
23
- configurable: true,
24
- writable: true,
25
- value: "id"
26
- });
27
- Object.defineProperty(this, "_mocody_sortKeyFieldName", {
28
- enumerable: true,
29
- configurable: true,
30
- writable: true,
31
- value: "featureEntity"
32
- });
33
- Object.defineProperty(this, "_mocody_featureEntity_Key_Value", {
34
- enumerable: true,
35
- configurable: true,
36
- writable: true,
37
- value: void 0
38
- });
39
- Object.defineProperty(this, "_mocody_dynamoDb", {
40
- enumerable: true,
41
- configurable: true,
42
- writable: true,
43
- value: void 0
44
- });
45
- Object.defineProperty(this, "_mocody_dataKeyGenerator", {
46
- enumerable: true,
47
- configurable: true,
48
- writable: true,
49
- value: void 0
50
- });
51
- Object.defineProperty(this, "_mocody_schema", {
52
- enumerable: true,
53
- configurable: true,
54
- writable: true,
55
- value: void 0
56
- });
57
- Object.defineProperty(this, "_mocody_tableFullName", {
58
- enumerable: true,
59
- configurable: true,
60
- writable: true,
61
- value: void 0
62
- });
63
- Object.defineProperty(this, "_mocody_strictRequiredFields", {
64
- enumerable: true,
65
- configurable: true,
66
- writable: true,
67
- value: void 0
68
- });
69
- Object.defineProperty(this, "_mocody_featureEntityValue", {
70
- enumerable: true,
71
- configurable: true,
72
- writable: true,
73
- value: void 0
74
- });
75
- Object.defineProperty(this, "_mocody_secondaryIndexOptions", {
76
- enumerable: true,
77
- configurable: true,
78
- writable: true,
79
- value: void 0
80
- });
81
- Object.defineProperty(this, "_mocody_queryFilter", {
82
- enumerable: true,
83
- configurable: true,
84
- writable: true,
85
- value: void 0
86
- });
87
- Object.defineProperty(this, "_mocody_queryScanProcessor", {
88
- enumerable: true,
89
- configurable: true,
90
- writable: true,
91
- value: void 0
92
- });
93
- Object.defineProperty(this, "_mocody_errorHelper", {
94
- enumerable: true,
95
- configurable: true,
96
- writable: true,
97
- value: void 0
98
- });
99
- Object.defineProperty(this, "_mocody_entityFieldsKeySet", {
100
- enumerable: true,
101
- configurable: true,
102
- writable: true,
103
- value: void 0
104
- });
105
- Object.defineProperty(this, "_mocody_tableManager", {
106
- enumerable: true,
107
- configurable: true,
108
- writable: true,
109
- value: void 0
110
- });
111
- this._mocody_dynamoDb = dynamoDbInitializer;
112
- this._mocody_dataKeyGenerator = dataKeyGenerator;
113
- this._mocody_tableFullName = baseTableName;
114
- this._mocody_featureEntityValue = featureEntityValue;
115
- this._mocody_secondaryIndexOptions = secondaryIndexOptions;
116
- this._mocody_strictRequiredFields = strictRequiredFields;
117
- this._mocody_queryFilter = new dynamo_filter_query_operation_1.DynamoFilterQueryOperation();
118
- this._mocody_queryScanProcessor = new dynamo_query_scan_processor_1.DynamoQueryScanProcessor();
119
- this._mocody_errorHelper = new errors_1.MocodyErrorUtils();
120
- this._mocody_featureEntity_Key_Value = { featureEntity: featureEntityValue };
121
- this._mocody_entityFieldsKeySet = new Set();
122
- const fullSchemaMapDef = {
123
- ...schemaDef,
124
- ...base_schema_1.coreSchemaDefinition,
125
- };
126
- Object.keys(fullSchemaMapDef).forEach((key) => {
127
- this._mocody_entityFieldsKeySet.add(key);
128
- });
129
- this._mocody_schema = joi_1.default.object().keys(fullSchemaMapDef);
130
- }
131
- mocody_tableManager() {
132
- if (!this._mocody_tableManager) {
133
- this._mocody_tableManager = new dynamo_manage_table_1.DynamoManageTable({
134
- dynamoDb: () => this._mocody_dynamoDb(),
135
- secondaryIndexOptions: this._mocody_secondaryIndexOptions,
136
- tableFullName: this._mocody_tableFullName,
137
- partitionKeyFieldName: this._mocody_partitionKeyFieldName,
138
- sortKeyFieldName: this._mocody_sortKeyFieldName,
139
- });
140
- }
141
- return this._mocody_tableManager;
142
- }
143
- _mocody_dynamoInit() {
144
- return this._mocody_dynamoDb();
145
- }
146
- _mocody_generateDynamoTableKey() {
147
- return this._mocody_dataKeyGenerator();
148
- }
149
- _mocody_getLocalVariables() {
150
- return {
151
- partitionKeyFieldName: this._mocody_partitionKeyFieldName,
152
- sortKeyFieldName: this._mocody_sortKeyFieldName,
153
- featureEntityValue: this._mocody_featureEntityValue,
154
- tableFullName: this._mocody_tableFullName,
155
- secondaryIndexOptions: this._mocody_secondaryIndexOptions,
156
- strictRequiredFields: this._mocody_strictRequiredFields,
157
- };
158
- }
159
- _mocody_getBaseObject({ dataId }) {
160
- const { partitionKeyFieldName, sortKeyFieldName, featureEntityValue } = this._mocody_getLocalVariables();
161
- const dataMust = {
162
- [partitionKeyFieldName]: dataId,
163
- [sortKeyFieldName]: featureEntityValue,
164
- };
165
- return dataMust;
166
- }
167
- _mocody_checkValidateMustBeAnObjectDataType(data) {
168
- if (!data || typeof data !== "object") {
169
- throw this._mocody_createGenericError(`Data MUST be valid object`);
170
- }
171
- }
172
- _mocody_checkValidateStrictRequiredFields(onDataObj) {
173
- this._mocody_checkValidateMustBeAnObjectDataType(onDataObj);
174
- const { strictRequiredFields } = this._mocody_getLocalVariables();
175
- if (strictRequiredFields?.length) {
176
- strictRequiredFields.forEach((field) => {
177
- if (onDataObj[field] === null || onDataObj[field] === undefined) {
178
- throw this._mocody_createGenericError(`Strict required field: '${field}', NOT defined`);
179
- }
180
- });
181
- }
182
- }
183
- _mocody_createGenericError(error) {
184
- return new errors_1.MocodyGenericError(error);
185
- }
186
- _mocody_withConditionPassed({ item, withCondition, }) {
187
- if (item && typeof item === "object" && withCondition?.length) {
188
- const isPassed = withCondition.every(({ field, equals }) => {
189
- return item[field] !== undefined && item[field] === equals;
190
- });
191
- return isPassed;
192
- }
193
- return true;
194
- }
195
- _mocody_removeDuplicateString(strArray) {
196
- return Array.from(new Set([...strArray]));
197
- }
198
- async _mocody_allHelpValidateGetValue(data) {
199
- const { error, value } = this._mocody_schema.validate(data, {
200
- stripUnknown: true,
201
- });
202
- if (error) {
203
- const msg = (0, base_joi_helper_1.getJoiValidationErrors)(error) ?? "Validation error occured";
204
- throw this._mocody_errorHelper.mocody_helper_createFriendlyError(msg);
205
- }
206
- return await Promise.resolve({ validatedData: value });
207
- }
208
- _mocody_formatTTL(fullData) {
209
- if (fullData?.dangerouslyExpireAt) {
210
- fullData.dangerouslyExpireAtTTL = util_service_1.UtilService.getEpochTime(fullData.dangerouslyExpireAt);
211
- }
212
- else {
213
- delete fullData.dangerouslyExpireAtTTL;
214
- }
215
- return fullData;
216
- }
217
- async mocody_createOne({ data, fieldAliases }) {
218
- const { tableFullName, partitionKeyFieldName, featureEntityValue } = this._mocody_getLocalVariables();
219
- try {
220
- const { marshalled, validatedData } = await this._mocody_validateReady({ data });
221
- const query01 = {
222
- [partitionKeyFieldName]: { $exists: false },
223
- };
224
- mocody_utils_1.MocodyUtil.validateFieldAlias({
225
- fieldAliases,
226
- data: validatedData,
227
- featureEntity: featureEntityValue,
228
- });
229
- const { expressionAttributeNames, expressionAttributeValues, filterExpression, } = this._mocody_queryFilter.processQueryFilter({
230
- queryDefs: query01,
231
- projectionFields: undefined,
232
- });
233
- const params = {
234
- TableName: tableFullName,
235
- Item: marshalled,
236
- ConditionExpression: filterExpression,
237
- ExpressionAttributeNames: expressionAttributeNames,
238
- ExpressionAttributeValues: expressionAttributeValues,
239
- };
240
- await this._mocody_dynamoInit().putItem(params);
241
- const result = { ...validatedData };
242
- return result;
243
- }
244
- catch (error) {
245
- if (error?.name === "ConditionalCheckFailedException") {
246
- throw this._mocody_errorHelper.mocody_helper_createFriendlyError(`Field, ${partitionKeyFieldName} already exists`);
247
- }
248
- throw error;
249
- }
250
- }
251
- async mocody_validateFormatData({ data }) {
252
- const { marshalled } = await this._mocody_validateReady({ data });
253
- return JSON.stringify(marshalled);
254
- }
255
- async mocody_formatForDump({ dataList }) {
256
- const bulkData = [];
257
- for (const data of dataList) {
258
- const { marshalled } = await this._mocody_validateReady({ data });
259
- bulkData.push(JSON.stringify({ Item: marshalled }));
260
- }
261
- return bulkData;
262
- }
263
- async _mocody_validateReady({ data }) {
264
- const { partitionKeyFieldName, featureEntityValue } = this._mocody_getLocalVariables();
265
- let dataId = data[partitionKeyFieldName];
266
- if (!dataId) {
267
- dataId = this._mocody_generateDynamoTableKey();
268
- }
269
- if (!(dataId && typeof dataId === "string")) {
270
- throw this._mocody_createGenericError("Invalid dataId generation");
271
- }
272
- const dataMust = this._mocody_getBaseObject({ dataId });
273
- const fullData = { ...data, ...dataMust };
274
- if (fullData.featureEntity !== featureEntityValue) {
275
- throw this._mocody_createGenericError("FeatureEntity mismatched");
276
- }
277
- const { validatedData } = await this._mocody_allHelpValidateGetValue(fullData);
278
- this._mocody_checkValidateStrictRequiredFields(validatedData);
279
- const validatedDataTTL = this._mocody_formatTTL(validatedData);
280
- const ready = {
281
- validatedData,
282
- marshalled: mocody_utils_1.MocodyUtil.marshallFromJson(validatedDataTTL),
283
- };
284
- return ready;
285
- }
286
- async mocody_getOneById({ dataId, withCondition, }) {
287
- const { partitionKeyFieldName, sortKeyFieldName, featureEntityValue, tableFullName, } = this._mocody_getLocalVariables();
288
- this._mocody_errorHelper.mocody_helper_validateRequiredString({
289
- QueryGetOnePartitionKey: dataId,
290
- QueryGetOneSortKey: featureEntityValue,
291
- });
292
- const params = {
293
- TableName: tableFullName,
294
- Key: {
295
- [partitionKeyFieldName]: { S: dataId },
296
- [sortKeyFieldName]: { S: featureEntityValue },
297
- },
298
- };
299
- const result = await this._mocody_dynamoInit().getItem(params);
300
- const item01 = result.Item;
301
- if (!item01) {
302
- return null;
303
- }
304
- const item = mocody_utils_1.MocodyUtil.unmarshallToJson(item01);
305
- const isPassed = this._mocody_withConditionPassed({ withCondition, item });
306
- if (!isPassed) {
307
- return null;
308
- }
309
- return item;
310
- }
311
- async mocody_updateOne({ dataId, updateData, withCondition, fieldAliases, }) {
312
- const { tableFullName, partitionKeyFieldName, sortKeyFieldName, featureEntityValue, } = this._mocody_getLocalVariables();
313
- this._mocody_errorHelper.mocody_helper_validateRequiredString({ Update1DataId: dataId });
314
- const dataInDb = await this.mocody_getOneById({ dataId });
315
- if (!dataInDb?.[partitionKeyFieldName]) {
316
- throw this._mocody_errorHelper.mocody_helper_createFriendlyError("Data does NOT exists");
317
- }
318
- if (dataInDb?.[sortKeyFieldName] !== featureEntityValue) {
319
- throw this._mocody_createGenericError("Record does not exists");
320
- }
321
- const isPassed = this._mocody_withConditionPassed({
322
- withCondition,
323
- item: dataInDb,
324
- });
325
- if (!isPassed) {
326
- throw this._mocody_createGenericError("Update condition failed");
327
- }
328
- const dataMust = this._mocody_getBaseObject({ dataId });
329
- const fullData = {
330
- ...dataInDb,
331
- ...updateData,
332
- ...dataMust,
333
- };
334
- const { validatedData } = await this._mocody_allHelpValidateGetValue(fullData);
335
- mocody_utils_1.MocodyUtil.validateFieldAlias({
336
- data: validatedData,
337
- fieldAliases,
338
- featureEntity: featureEntityValue,
339
- });
340
- this._mocody_checkValidateStrictRequiredFields(validatedData);
341
- const validatedData01 = this._mocody_formatTTL(validatedData);
342
- const query01 = {
343
- [partitionKeyFieldName]: { $exists: true },
344
- };
345
- const { expressionAttributeNames, expressionAttributeValues, filterExpression, } = this._mocody_queryFilter.processQueryFilter({
346
- queryDefs: query01,
347
- projectionFields: undefined,
348
- });
349
- const params = {
350
- TableName: tableFullName,
351
- Item: mocody_utils_1.MocodyUtil.marshallFromJson(validatedData01),
352
- ConditionExpression: filterExpression,
353
- ExpressionAttributeNames: expressionAttributeNames,
354
- ExpressionAttributeValues: expressionAttributeValues,
355
- };
356
- await this._mocody_dynamoInit().putItem(params);
357
- const result = validatedData;
358
- return result;
359
- }
360
- async mocody_prepareTransaction({ transactPrepareInfo, }) {
361
- const { tableFullName, partitionKeyFieldName, sortKeyFieldName, featureEntityValue, } = this._mocody_getLocalVariables();
362
- const prepareTransactData = [];
363
- for (const transactInfoItem of transactPrepareInfo) {
364
- if (transactInfoItem.kind === "create") {
365
- const { marshalled } = await this._mocody_validateReady({ data: transactInfoItem.data });
366
- prepareTransactData.push({
367
- kind: transactInfoItem.kind,
368
- tableName: tableFullName,
369
- data: marshalled,
370
- partitionKeyFieldName,
371
- });
372
- }
373
- else if (transactInfoItem.kind === "update") {
374
- const dataMust = this._mocody_getBaseObject({ dataId: transactInfoItem.dataId });
375
- const fullData = {
376
- ...transactInfoItem.data,
377
- ...dataMust,
378
- };
379
- const { validatedData } = await this._mocody_allHelpValidateGetValue(fullData);
380
- this._mocody_checkValidateStrictRequiredFields(validatedData);
381
- const validatedData01 = this._mocody_formatTTL(validatedData);
382
- prepareTransactData.push({
383
- partitionKeyFieldName,
384
- kind: transactInfoItem.kind,
385
- tableName: tableFullName,
386
- data: mocody_utils_1.MocodyUtil.marshallFromJson(validatedData01),
387
- keyQuery: {
388
- [partitionKeyFieldName]: { S: transactInfoItem.dataId },
389
- [sortKeyFieldName]: { S: featureEntityValue },
390
- },
391
- });
392
- }
393
- else if (transactInfoItem.kind === "delete") {
394
- prepareTransactData.push({
395
- partitionKeyFieldName,
396
- kind: transactInfoItem.kind,
397
- tableName: tableFullName,
398
- keyQuery: {
399
- [partitionKeyFieldName]: { S: transactInfoItem.dataId },
400
- [sortKeyFieldName]: { S: featureEntityValue },
401
- },
402
- });
403
- }
404
- }
405
- return Promise.resolve(prepareTransactData);
406
- }
407
- async mocody_executeTransaction({ transactInfo }) {
408
- const transactData = {
409
- TransactItems: [],
410
- };
411
- for (const item of transactInfo) {
412
- if (item.kind === "create") {
413
- const query01 = {
414
- [item.partitionKeyFieldName]: { $exists: false },
415
- };
416
- const { expressionAttributeNames, expressionAttributeValues, filterExpression, } = this._mocody_queryFilter.processQueryFilter({
417
- queryDefs: query01,
418
- projectionFields: undefined,
419
- });
420
- transactData.TransactItems?.push({
421
- Put: {
422
- TableName: item.tableName,
423
- Item: item.data,
424
- ConditionExpression: filterExpression,
425
- ExpressionAttributeNames: expressionAttributeNames,
426
- ExpressionAttributeValues: expressionAttributeValues,
427
- },
428
- });
429
- }
430
- else if (item.kind === "update") {
431
- const query01 = {
432
- [item.partitionKeyFieldName]: { $exists: true },
433
- };
434
- const { expressionAttributeNames, expressionAttributeValues, filterExpression, } = this._mocody_queryFilter.processQueryFilter({
435
- queryDefs: query01,
436
- projectionFields: undefined,
437
- });
438
- transactData.TransactItems?.push({
439
- Put: {
440
- TableName: item.tableName,
441
- Item: item.data,
442
- ConditionExpression: filterExpression,
443
- ExpressionAttributeNames: expressionAttributeNames,
444
- ExpressionAttributeValues: expressionAttributeValues,
445
- },
446
- });
447
- }
448
- else if (item.kind === "delete") {
449
- const query01 = {
450
- [item.partitionKeyFieldName]: { $exists: true },
451
- };
452
- const { expressionAttributeNames, expressionAttributeValues, filterExpression, } = this._mocody_queryFilter.processQueryFilter({
453
- queryDefs: query01,
454
- projectionFields: undefined,
455
- });
456
- transactData.TransactItems?.push({
457
- Delete: {
458
- TableName: item.tableName,
459
- Key: item.keyQuery,
460
- ConditionExpression: filterExpression,
461
- ExpressionAttributeNames: expressionAttributeNames,
462
- ExpressionAttributeValues: expressionAttributeValues,
463
- },
464
- });
465
- }
466
- }
467
- await this._mocody_dynamoInit().transactWriteItems(transactData);
468
- }
469
- _mocody_getProjectionFields({ excludeFields, fields, }) {
470
- return mocody_utils_1.MocodyUtil.getProjectionFields({
471
- excludeFields,
472
- fields,
473
- entityFields: Array.from(this._mocody_entityFieldsKeySet),
474
- });
475
- }
476
- async mocody_getManyByIds({ dataIds, fields, excludeFields, withCondition, }) {
477
- dataIds.forEach((dataId) => {
478
- this._mocody_errorHelper.mocody_helper_validateRequiredString({
479
- BatchGetDataId: dataId,
480
- });
481
- });
482
- const originalIds = this._mocody_removeDuplicateString(dataIds);
483
- const BATCH_SIZE = 80;
484
- const batchIds = lodash_1.default.chunk(originalIds, BATCH_SIZE);
485
- logging_service_1.LoggingService.log({
486
- batchIds,
487
- "@mocody_getManyByIds batchIds": batchIds.length,
488
- });
489
- let resultAll = [];
490
- const fieldKeys = this._mocody_getProjectionFields({ fields, excludeFields });
491
- for (const batch of batchIds) {
492
- const callByIds = await this.mocody_batchGetManyByIdsBasePrivate({
493
- dataIds: batch,
494
- fields: fieldKeys,
495
- withCondition,
496
- });
497
- resultAll = [...resultAll, ...callByIds];
498
- }
499
- logging_service_1.LoggingService.log("@mocody_getManyByIds batchIds result Out: ", resultAll.length);
500
- return resultAll;
501
- }
502
- async mocody_batchGetManyByIdsBasePrivate({ dataIds, fields, excludeFields, withCondition, }) {
503
- const { tableFullName, partitionKeyFieldName, sortKeyFieldName, featureEntityValue, } = this._mocody_getLocalVariables();
504
- const dataIdsNoDup = this._mocody_removeDuplicateString(dataIds);
505
- let projectionExpression;
506
- let expressionAttributeNames;
507
- const fields01 = this._mocody_getProjectionFields({ fields, excludeFields });
508
- if (fields01?.length) {
509
- const fieldKeys = new Set(fields01);
510
- if (withCondition?.length) {
511
- withCondition.forEach((condition) => {
512
- fieldKeys.add(condition.field);
513
- });
514
- }
515
- expressionAttributeNames = {};
516
- fieldKeys.forEach((fieldName) => {
517
- if (typeof fieldName === "string") {
518
- if (expressionAttributeNames) {
519
- const attrKeyHash = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
520
- expressionAttributeNames[attrKeyHash] = fieldName;
521
- }
522
- }
523
- });
524
- if (Object.keys(expressionAttributeNames)?.length) {
525
- projectionExpression = Object.keys(expressionAttributeNames).join(",");
526
- }
527
- else {
528
- projectionExpression = undefined;
529
- expressionAttributeNames = undefined;
530
- }
531
- }
532
- const getArray = dataIdsNoDup.map((dataId) => {
533
- const params01 = {
534
- [partitionKeyFieldName]: { S: dataId },
535
- [sortKeyFieldName]: { S: featureEntityValue },
536
- };
537
- return params01;
538
- });
539
- const params = {
540
- RequestItems: {
541
- [tableFullName]: {
542
- Keys: [...getArray],
543
- ProjectionExpression: projectionExpression,
544
- ExpressionAttributeNames: expressionAttributeNames,
545
- },
546
- },
547
- };
548
- logging_service_1.LoggingService.log({ batchGetItemParams: params });
549
- let hasNext = true;
550
- let params01 = { ...params };
551
- let returnedItems = [];
552
- const dynamo = this._mocody_dynamoInit();
553
- logging_service_1.LoggingService.log({ fetchForDataIds: dataIds });
554
- while (hasNext) {
555
- try {
556
- const { Responses, UnprocessedKeys } = await dynamo.batchGetItem(params01);
557
- if (Responses) {
558
- const itemListRaw = Responses[tableFullName];
559
- if (itemListRaw?.length) {
560
- const itemList = itemListRaw.map((item) => mocody_utils_1.MocodyUtil.unmarshallToJson(item));
561
- returnedItems = [...returnedItems, ...itemList];
562
- }
563
- }
564
- if (UnprocessedKeys && Object.keys(UnprocessedKeys).length) {
565
- params01 = {
566
- RequestItems: UnprocessedKeys,
567
- };
568
- logging_service_1.LoggingService.log({ dynamoBatchGetParams: params01 });
569
- }
570
- else {
571
- hasNext = false;
572
- break;
573
- }
574
- }
575
- catch (error) {
576
- hasNext = false;
577
- logging_service_1.LoggingService.error(error);
578
- if (!returnedItems?.length) {
579
- throw error;
580
- }
581
- else {
582
- break;
583
- }
584
- }
585
- }
586
- if (returnedItems?.length && withCondition?.length) {
587
- const returnedItems01 = [];
588
- returnedItems.forEach((item) => {
589
- const canInclude = withCondition.every((condition) => item[condition.field] === condition.equals);
590
- if (canInclude) {
591
- returnedItems01.push(item);
592
- }
593
- });
594
- return returnedItems01;
595
- }
596
- return returnedItems;
597
- }
598
- async mocody_getManyByIndex(paramOption) {
599
- const result = await this._mocody_getManyBySecondaryIndexPaginateBase({
600
- paramOption,
601
- canPaginate: false,
602
- enableRelationFetch: false,
603
- });
604
- if (result?.paginationResults) {
605
- return result.paginationResults;
606
- }
607
- return [];
608
- }
609
- async mocody_getManyByIndexPaginate(paramOption) {
610
- return this._mocody_getManyBySecondaryIndexPaginateBase({
611
- paramOption,
612
- canPaginate: true,
613
- enableRelationFetch: false,
614
- });
615
- }
616
- async mocody_getManyWithRelation(paramOption) {
617
- const result = await this._mocody_getManyBySecondaryIndexPaginateBase({
618
- paramOption,
619
- canPaginate: false,
620
- enableRelationFetch: true,
621
- });
622
- if (result?.paginationResults) {
623
- return result.paginationResults;
624
- }
625
- return [];
626
- }
627
- async mocody_getManyWithRelationPaginate(paramOption) {
628
- return this._mocody_getManyBySecondaryIndexPaginateBase({
629
- paramOption,
630
- canPaginate: true,
631
- enableRelationFetch: true,
632
- });
633
- }
634
- async _mocody_getManyBySecondaryIndexPaginateBase({ paramOption, canPaginate, enableRelationFetch, }) {
635
- const { tableFullName, secondaryIndexOptions, partitionKeyFieldName, sortKeyFieldName, featureEntityValue, } = this._mocody_getLocalVariables();
636
- if (!secondaryIndexOptions?.length) {
637
- throw this._mocody_createGenericError("Invalid secondary index definitions");
638
- }
639
- const paramOption01 = { ...paramOption };
640
- if (!paramOption01?.indexName) {
641
- throw this._mocody_createGenericError("Invalid index name input");
642
- }
643
- const secondaryIndex = secondaryIndexOptions.find((item) => {
644
- return item.indexName === paramOption01.indexName;
645
- });
646
- if (!secondaryIndex) {
647
- throw this._mocody_createGenericError(`Secondary index '${paramOption01.indexName}' not named/defined in secondaryIndexOptions`);
648
- }
649
- let projectionFields;
650
- const fields01 = this._mocody_getProjectionFields({
651
- fields: paramOption01.fields,
652
- excludeFields: paramOption01.excludeFields,
653
- });
654
- if (canPaginate && fields01?.length) {
655
- const fieldSet01 = new Set(fields01);
656
- fieldSet01.add(partitionKeyFieldName);
657
- projectionFields = Array.from(fieldSet01);
658
- }
659
- else {
660
- projectionFields = fields01;
661
- }
662
- let evaluationLimit01;
663
- let resultLimit01;
664
- if (paramOption01.limit && util_service_1.UtilService.isNumericInteger(paramOption01.limit)) {
665
- resultLimit01 = Number(paramOption01.limit);
666
- }
667
- if (paramOption01?.pagingParams?.evaluationLimit) {
668
- evaluationLimit01 = paramOption01?.pagingParams?.evaluationLimit;
669
- }
670
- else if (!paramOption01.query && resultLimit01) {
671
- evaluationLimit01 = resultLimit01;
672
- }
673
- const index_PartitionKeyFieldName = secondaryIndex.partitionKeyFieldName;
674
- const index_SortKeyFieldName = secondaryIndex.sortKeyFieldName;
675
- const main_partitionAndSortKey = [partitionKeyFieldName, sortKeyFieldName];
676
- const index_partitionAndSortKey = [index_PartitionKeyFieldName, index_SortKeyFieldName];
677
- const partitionSortKeyQuery = paramOption01.sortKeyQuery
678
- ? {
679
- ...{ [index_SortKeyFieldName]: paramOption01.sortKeyQuery },
680
- ...{ [index_PartitionKeyFieldName]: paramOption01.partitionKeyValue },
681
- }
682
- : { [index_PartitionKeyFieldName]: paramOption01.partitionKeyValue };
683
- if (!enableRelationFetch) {
684
- const localVariables = this._mocody_getLocalVariables();
685
- const hasFeatureEntity = [
686
- index_PartitionKeyFieldName,
687
- index_SortKeyFieldName,
688
- ].includes(localVariables.sortKeyFieldName);
689
- if (!hasFeatureEntity) {
690
- paramOption01.query = (paramOption01.query || {});
691
- paramOption01.query = {
692
- ...paramOption01.query,
693
- ...this._mocody_featureEntity_Key_Value,
694
- };
695
- }
696
- else if (index_PartitionKeyFieldName !== localVariables.sortKeyFieldName) {
697
- if (localVariables.sortKeyFieldName === index_SortKeyFieldName) {
698
- partitionSortKeyQuery[index_SortKeyFieldName] = { $eq: localVariables.featureEntityValue };
699
- }
700
- }
701
- }
702
- const mainFilter = this._mocody_queryFilter.processQueryFilter({
703
- queryDefs: partitionSortKeyQuery,
704
- projectionFields,
705
- });
706
- let otherFilterExpression;
707
- let otherExpressionAttributeValues;
708
- let otherExpressionAttributeNames;
709
- if (paramOption01.query) {
710
- const otherFilter = this._mocody_queryFilter.processQueryFilter({
711
- queryDefs: paramOption01.query,
712
- projectionFields: null,
713
- });
714
- otherExpressionAttributeValues = otherFilter.expressionAttributeValues;
715
- otherExpressionAttributeNames = otherFilter.expressionAttributeNames;
716
- if (otherFilter?.filterExpression?.length && otherFilter?.filterExpression.length > 1) {
717
- otherFilterExpression = otherFilter.filterExpression;
718
- }
719
- }
720
- const params = {
721
- TableName: tableFullName,
722
- IndexName: paramOption01.indexName,
723
- KeyConditionExpression: mainFilter.filterExpression,
724
- ExpressionAttributeValues: {
725
- ...otherExpressionAttributeValues,
726
- ...mainFilter.expressionAttributeValues,
727
- },
728
- FilterExpression: otherFilterExpression ?? undefined,
729
- ExpressionAttributeNames: {
730
- ...otherExpressionAttributeNames,
731
- ...mainFilter.expressionAttributeNames,
732
- },
733
- };
734
- const orderDesc = paramOption01?.sort === "desc";
735
- if (orderDesc) {
736
- params.ScanIndexForward = false;
737
- }
738
- else {
739
- params.ScanIndexForward = true;
740
- }
741
- if (mainFilter.projectionExpressionAttr) {
742
- params.ProjectionExpression = mainFilter.projectionExpressionAttr;
743
- }
744
- let nextPageHash01 = paramOption01?.pagingParams?.nextPageHash;
745
- if (nextPageHash01 === "undefined") {
746
- nextPageHash01 = undefined;
747
- }
748
- if (nextPageHash01 === "null") {
749
- nextPageHash01 = undefined;
750
- }
751
- const result = await this._mocody_queryScanProcessor.mocody__helperDynamoQueryProcessor({
752
- dynamoDb: () => this._mocody_dynamoInit(),
753
- params,
754
- orderDesc,
755
- canPaginate,
756
- tableFullName,
757
- featureEntityValue,
758
- main_partitionAndSortKey,
759
- index_partitionAndSortKey,
760
- evaluationLimit: evaluationLimit01,
761
- nextPageHash: nextPageHash01,
762
- resultLimit: resultLimit01,
763
- });
764
- return result;
765
- }
766
- async mocody_deleteById({ dataId, withCondition, }) {
767
- this._mocody_errorHelper.mocody_helper_validateRequiredString({ Del1SortKey: dataId });
768
- const { tableFullName, partitionKeyFieldName, sortKeyFieldName, featureEntityValue, } = this._mocody_getLocalVariables();
769
- const dataExist = await this.mocody_getOneById({ dataId, withCondition });
770
- if (!(dataExist && dataExist[partitionKeyFieldName])) {
771
- throw this._mocody_errorHelper.mocody_helper_createFriendlyError("Record does NOT exists");
772
- }
773
- const params = {
774
- TableName: tableFullName,
775
- Key: {
776
- [partitionKeyFieldName]: { S: dataId },
777
- [sortKeyFieldName]: { S: featureEntityValue },
778
- },
779
- };
780
- try {
781
- await this._mocody_dynamoInit().deleteItem(params);
782
- }
783
- catch (err) {
784
- if (err && err.code === "ResourceNotFoundException") {
785
- throw this._mocody_errorHelper.mocody_helper_createFriendlyError("Table not found");
786
- }
787
- else if (err && err.code === "ResourceInUseException") {
788
- throw this._mocody_errorHelper.mocody_helper_createFriendlyError("Table in use");
789
- }
790
- else {
791
- throw err;
792
- }
793
- }
794
- return dataExist;
795
- }
796
- }
797
- exports.DynamoDataOperation = DynamoDataOperation;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DynamoDataOperation = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const util_service_1 = require("./../helpers/util-service");
6
+ const mocody_utils_1 = require("../helpers/mocody-utils");
7
+ const model_1 = require("../model");
8
+ const errors_1 = require("./../helpers/errors");
9
+ const joi_1 = tslib_1.__importDefault(require("joi"));
10
+ const base_joi_helper_1 = require("../helpers/base-joi-helper");
11
+ const base_schema_1 = require("../core/base-schema");
12
+ const dynamo_manage_table_1 = require("./dynamo-manage-table");
13
+ const logging_service_1 = require("../helpers/logging-service");
14
+ const dynamo_filter_query_operation_1 = require("./dynamo-filter-query-operation");
15
+ const dynamo_query_scan_processor_1 = require("./dynamo-query-scan-processor");
16
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
17
+ const dynamo_helper_1 = require("./dynamo-helper");
18
+ const dynamo_query_partiql_processor_1 = require("./dynamo-query-partiql-processor");
19
+ const dynamo_filter_query_partiql_operation_1 = require("./dynamo-filter-query-partiql-operation");
20
+ class DynamoDataOperation extends model_1.RepoModel {
21
+ constructor({ schemaDef, dynamoDbInitializer, secondaryIndexOptions, featureEntityValue, baseTableName, strictRequiredFields, dataKeyGenerator, }) {
22
+ super();
23
+ Object.defineProperty(this, "_mocody_partitionKeyFieldName", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: "id"
28
+ });
29
+ Object.defineProperty(this, "_mocody_sortKeyFieldName", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: "featureEntity"
34
+ });
35
+ Object.defineProperty(this, "_mocody_featureEntity_Key_Value", {
36
+ enumerable: true,
37
+ configurable: true,
38
+ writable: true,
39
+ value: void 0
40
+ });
41
+ Object.defineProperty(this, "_mocody_dynamoDb", {
42
+ enumerable: true,
43
+ configurable: true,
44
+ writable: true,
45
+ value: void 0
46
+ });
47
+ Object.defineProperty(this, "_mocody_dataKeyGenerator", {
48
+ enumerable: true,
49
+ configurable: true,
50
+ writable: true,
51
+ value: void 0
52
+ });
53
+ Object.defineProperty(this, "_mocody_schema", {
54
+ enumerable: true,
55
+ configurable: true,
56
+ writable: true,
57
+ value: void 0
58
+ });
59
+ Object.defineProperty(this, "_mocody_tableFullName", {
60
+ enumerable: true,
61
+ configurable: true,
62
+ writable: true,
63
+ value: void 0
64
+ });
65
+ Object.defineProperty(this, "_mocody_strictRequiredFields", {
66
+ enumerable: true,
67
+ configurable: true,
68
+ writable: true,
69
+ value: void 0
70
+ });
71
+ Object.defineProperty(this, "_mocody_featureEntityValue", {
72
+ enumerable: true,
73
+ configurable: true,
74
+ writable: true,
75
+ value: void 0
76
+ });
77
+ Object.defineProperty(this, "_mocody_secondaryIndexOptions", {
78
+ enumerable: true,
79
+ configurable: true,
80
+ writable: true,
81
+ value: void 0
82
+ });
83
+ Object.defineProperty(this, "_mocody_queryFilter", {
84
+ enumerable: true,
85
+ configurable: true,
86
+ writable: true,
87
+ value: void 0
88
+ });
89
+ Object.defineProperty(this, "_mocody_queryPartiQlFilter", {
90
+ enumerable: true,
91
+ configurable: true,
92
+ writable: true,
93
+ value: void 0
94
+ });
95
+ Object.defineProperty(this, "_mocody_queryScanProcessor", {
96
+ enumerable: true,
97
+ configurable: true,
98
+ writable: true,
99
+ value: void 0
100
+ });
101
+ Object.defineProperty(this, "_mocody_queryPartiQlProcessor", {
102
+ enumerable: true,
103
+ configurable: true,
104
+ writable: true,
105
+ value: void 0
106
+ });
107
+ Object.defineProperty(this, "_mocody_errorHelper", {
108
+ enumerable: true,
109
+ configurable: true,
110
+ writable: true,
111
+ value: void 0
112
+ });
113
+ Object.defineProperty(this, "_mocody_entityFieldsKeySet", {
114
+ enumerable: true,
115
+ configurable: true,
116
+ writable: true,
117
+ value: void 0
118
+ });
119
+ Object.defineProperty(this, "_mocody_tableManager", {
120
+ enumerable: true,
121
+ configurable: true,
122
+ writable: true,
123
+ value: void 0
124
+ });
125
+ this._mocody_dynamoDb = dynamoDbInitializer;
126
+ this._mocody_dataKeyGenerator = dataKeyGenerator;
127
+ this._mocody_tableFullName = baseTableName;
128
+ this._mocody_featureEntityValue = featureEntityValue;
129
+ this._mocody_secondaryIndexOptions = secondaryIndexOptions;
130
+ this._mocody_strictRequiredFields = strictRequiredFields;
131
+ this._mocody_queryFilter = new dynamo_filter_query_operation_1.DynamoFilterQueryOperation();
132
+ this._mocody_queryPartiQlFilter = new dynamo_filter_query_partiql_operation_1.DynamoFilterQueryPartiQlOperation();
133
+ this._mocody_queryScanProcessor = new dynamo_query_scan_processor_1.DynamoQueryScanProcessor();
134
+ this._mocody_queryPartiQlProcessor = new dynamo_query_partiql_processor_1.DynamoQueryPartiqlProcessor();
135
+ this._mocody_errorHelper = new errors_1.MocodyErrorUtils();
136
+ this._mocody_featureEntity_Key_Value = { featureEntity: featureEntityValue };
137
+ this._mocody_entityFieldsKeySet = new Set();
138
+ const fullSchemaMapDef = {
139
+ ...schemaDef,
140
+ ...base_schema_1.coreSchemaDefinition,
141
+ };
142
+ Object.keys(fullSchemaMapDef).forEach((key) => {
143
+ this._mocody_entityFieldsKeySet.add(key);
144
+ });
145
+ this._mocody_schema = joi_1.default.object().keys(fullSchemaMapDef);
146
+ }
147
+ mocody_tableManager() {
148
+ if (!this._mocody_tableManager) {
149
+ this._mocody_tableManager = new dynamo_manage_table_1.DynamoManageTable({
150
+ dynamoDb: () => this._mocody_dynamoDb(),
151
+ secondaryIndexOptions: this._mocody_secondaryIndexOptions,
152
+ tableFullName: this._mocody_tableFullName,
153
+ partitionKeyFieldName: this._mocody_partitionKeyFieldName,
154
+ sortKeyFieldName: this._mocody_sortKeyFieldName,
155
+ });
156
+ }
157
+ return this._mocody_tableManager;
158
+ }
159
+ _mocody_dynamoInit() {
160
+ return this._mocody_dynamoDb();
161
+ }
162
+ _mocody_generateDynamoTableKey() {
163
+ return this._mocody_dataKeyGenerator();
164
+ }
165
+ _mocody_getLocalVariables() {
166
+ return {
167
+ partitionKeyFieldName: this._mocody_partitionKeyFieldName,
168
+ sortKeyFieldName: this._mocody_sortKeyFieldName,
169
+ featureEntityValue: this._mocody_featureEntityValue,
170
+ tableFullName: this._mocody_tableFullName,
171
+ secondaryIndexOptions: this._mocody_secondaryIndexOptions,
172
+ strictRequiredFields: this._mocody_strictRequiredFields,
173
+ };
174
+ }
175
+ _mocody_getBaseObject({ dataId }) {
176
+ const { partitionKeyFieldName, sortKeyFieldName, featureEntityValue } = this._mocody_getLocalVariables();
177
+ const dataMust = {
178
+ [partitionKeyFieldName]: dataId,
179
+ [sortKeyFieldName]: featureEntityValue,
180
+ };
181
+ return dataMust;
182
+ }
183
+ _mocody_checkValidateMustBeAnObjectDataType(data) {
184
+ if (!data || typeof data !== "object") {
185
+ throw this._mocody_createGenericError(`Data MUST be valid object`);
186
+ }
187
+ }
188
+ _mocody_checkValidateStrictRequiredFields(onDataObj) {
189
+ this._mocody_checkValidateMustBeAnObjectDataType(onDataObj);
190
+ const { strictRequiredFields } = this._mocody_getLocalVariables();
191
+ if (strictRequiredFields?.length) {
192
+ strictRequiredFields.forEach((field) => {
193
+ if (onDataObj[field] === null || onDataObj[field] === undefined) {
194
+ throw this._mocody_createGenericError(`Strict required field: '${field}', NOT defined`);
195
+ }
196
+ });
197
+ }
198
+ }
199
+ _mocody_createGenericError(error) {
200
+ return new errors_1.MocodyGenericError(error);
201
+ }
202
+ _mocody_withConditionPassed({ item, withCondition, }) {
203
+ if (item && typeof item === "object" && withCondition?.length) {
204
+ const isPassed = withCondition.every(({ field, equals }) => {
205
+ return item[field] !== undefined && item[field] === equals;
206
+ });
207
+ return isPassed;
208
+ }
209
+ return true;
210
+ }
211
+ _mocody_removeDuplicateString(strArray) {
212
+ return Array.from(new Set([...strArray]));
213
+ }
214
+ async _mocody_allHelpValidateGetValue(data) {
215
+ const { error, value } = this._mocody_schema.validate(data, {
216
+ stripUnknown: true,
217
+ });
218
+ if (error) {
219
+ const msg = (0, base_joi_helper_1.getJoiValidationErrors)(error) ?? "Validation error occured";
220
+ throw this._mocody_errorHelper.mocody_helper_createFriendlyError(msg);
221
+ }
222
+ return await Promise.resolve({ validatedData: value });
223
+ }
224
+ _mocody_formatTTL(fullData) {
225
+ if (fullData?.dangerouslyExpireAt) {
226
+ fullData.dangerouslyExpireAtTTL = util_service_1.UtilService.getEpochTime(fullData.dangerouslyExpireAt);
227
+ }
228
+ else {
229
+ delete fullData.dangerouslyExpireAtTTL;
230
+ }
231
+ return fullData;
232
+ }
233
+ async mocody_createOne({ data, fieldAliases }) {
234
+ const { tableFullName, partitionKeyFieldName, featureEntityValue } = this._mocody_getLocalVariables();
235
+ try {
236
+ const { marshalled, validatedData } = await this._mocody_validateReady({ data });
237
+ const query01 = {
238
+ [partitionKeyFieldName]: { $exists: false },
239
+ };
240
+ mocody_utils_1.MocodyUtil.validateFieldAlias({
241
+ fieldAliases,
242
+ data: validatedData,
243
+ featureEntity: featureEntityValue,
244
+ });
245
+ const { expressionAttributeNames, expressionAttributeValues, filterExpression, } = this._mocody_queryFilter.processQueryFilter({
246
+ queryDefs: query01,
247
+ projectionFields: undefined,
248
+ });
249
+ const params = {
250
+ TableName: tableFullName,
251
+ Item: marshalled,
252
+ ConditionExpression: filterExpression,
253
+ ExpressionAttributeNames: expressionAttributeNames,
254
+ ExpressionAttributeValues: expressionAttributeValues,
255
+ };
256
+ await this._mocody_dynamoInit().putItem(params);
257
+ const result = { ...validatedData };
258
+ return result;
259
+ }
260
+ catch (error) {
261
+ if (error?.name === "ConditionalCheckFailedException") {
262
+ throw this._mocody_errorHelper.mocody_helper_createFriendlyError(`Field, ${partitionKeyFieldName} already exists`);
263
+ }
264
+ throw error;
265
+ }
266
+ }
267
+ async mocody_validateFormatData({ data }) {
268
+ const { marshalled } = await this._mocody_validateReady({ data });
269
+ return JSON.stringify(marshalled);
270
+ }
271
+ async mocody_formatForDump({ dataList }) {
272
+ const bulkData = [];
273
+ for (const data of dataList) {
274
+ const { marshalled } = await this._mocody_validateReady({ data });
275
+ bulkData.push(JSON.stringify({ Item: marshalled }));
276
+ }
277
+ return bulkData;
278
+ }
279
+ async _mocody_validateReady({ data }) {
280
+ const { partitionKeyFieldName, featureEntityValue } = this._mocody_getLocalVariables();
281
+ let dataId = data[partitionKeyFieldName];
282
+ if (!dataId) {
283
+ dataId = this._mocody_generateDynamoTableKey();
284
+ }
285
+ if (!(dataId && typeof dataId === "string")) {
286
+ throw this._mocody_createGenericError("Invalid dataId generation");
287
+ }
288
+ const dataMust = this._mocody_getBaseObject({ dataId });
289
+ const fullData = { ...data, ...dataMust };
290
+ if (fullData.featureEntity !== featureEntityValue) {
291
+ throw this._mocody_createGenericError("FeatureEntity mismatched");
292
+ }
293
+ const { validatedData } = await this._mocody_allHelpValidateGetValue(fullData);
294
+ this._mocody_checkValidateStrictRequiredFields(validatedData);
295
+ const validatedDataTTL = this._mocody_formatTTL(validatedData);
296
+ const ready = {
297
+ validatedData,
298
+ marshalled: mocody_utils_1.MocodyUtil.marshallFromJson(validatedDataTTL),
299
+ };
300
+ return ready;
301
+ }
302
+ async mocody_getOneById({ dataId, withCondition, }) {
303
+ const { partitionKeyFieldName, sortKeyFieldName, featureEntityValue, tableFullName, } = this._mocody_getLocalVariables();
304
+ this._mocody_errorHelper.mocody_helper_validateRequiredString({
305
+ QueryGetOnePartitionKey: dataId,
306
+ QueryGetOneSortKey: featureEntityValue,
307
+ });
308
+ const params = {
309
+ TableName: tableFullName,
310
+ Key: {
311
+ [partitionKeyFieldName]: { S: dataId },
312
+ [sortKeyFieldName]: { S: featureEntityValue },
313
+ },
314
+ };
315
+ const result = await this._mocody_dynamoInit().getItem(params);
316
+ const item01 = result.Item;
317
+ if (!item01) {
318
+ return null;
319
+ }
320
+ const item = mocody_utils_1.MocodyUtil.unmarshallToJson(item01);
321
+ const isPassed = this._mocody_withConditionPassed({ withCondition, item });
322
+ if (!isPassed) {
323
+ return null;
324
+ }
325
+ return item;
326
+ }
327
+ async mocody_updateOne({ dataId, updateData, withCondition, fieldAliases, }) {
328
+ const { tableFullName, partitionKeyFieldName, sortKeyFieldName, featureEntityValue, } = this._mocody_getLocalVariables();
329
+ this._mocody_errorHelper.mocody_helper_validateRequiredString({ Update1DataId: dataId });
330
+ const dataInDb = await this.mocody_getOneById({ dataId });
331
+ if (!dataInDb?.[partitionKeyFieldName]) {
332
+ throw this._mocody_errorHelper.mocody_helper_createFriendlyError("Data does NOT exists");
333
+ }
334
+ if (dataInDb?.[sortKeyFieldName] !== featureEntityValue) {
335
+ throw this._mocody_createGenericError("Record does not exists");
336
+ }
337
+ const isPassed = this._mocody_withConditionPassed({
338
+ withCondition,
339
+ item: dataInDb,
340
+ });
341
+ if (!isPassed) {
342
+ throw this._mocody_createGenericError("Update condition failed");
343
+ }
344
+ const dataMust = this._mocody_getBaseObject({ dataId });
345
+ const fullData = {
346
+ ...dataInDb,
347
+ ...updateData,
348
+ ...dataMust,
349
+ };
350
+ const { validatedData } = await this._mocody_allHelpValidateGetValue(fullData);
351
+ mocody_utils_1.MocodyUtil.validateFieldAlias({
352
+ data: validatedData,
353
+ fieldAliases,
354
+ featureEntity: featureEntityValue,
355
+ });
356
+ this._mocody_checkValidateStrictRequiredFields(validatedData);
357
+ const validatedData01 = this._mocody_formatTTL(validatedData);
358
+ const query01 = {
359
+ [partitionKeyFieldName]: { $exists: true },
360
+ };
361
+ const { expressionAttributeNames, expressionAttributeValues, filterExpression, } = this._mocody_queryFilter.processQueryFilter({
362
+ queryDefs: query01,
363
+ projectionFields: undefined,
364
+ });
365
+ const params = {
366
+ TableName: tableFullName,
367
+ Item: mocody_utils_1.MocodyUtil.marshallFromJson(validatedData01),
368
+ ConditionExpression: filterExpression,
369
+ ExpressionAttributeNames: expressionAttributeNames,
370
+ ExpressionAttributeValues: expressionAttributeValues,
371
+ };
372
+ await this._mocody_dynamoInit().putItem(params);
373
+ const result = validatedData;
374
+ return result;
375
+ }
376
+ async mocody_prepareTransaction({ transactPrepareInfo, }) {
377
+ const { tableFullName, partitionKeyFieldName, sortKeyFieldName, featureEntityValue, } = this._mocody_getLocalVariables();
378
+ const prepareTransactData = [];
379
+ for (const transactInfoItem of transactPrepareInfo) {
380
+ if (transactInfoItem.kind === "create") {
381
+ const { marshalled } = await this._mocody_validateReady({ data: transactInfoItem.data });
382
+ prepareTransactData.push({
383
+ kind: transactInfoItem.kind,
384
+ tableName: tableFullName,
385
+ data: marshalled,
386
+ partitionKeyFieldName,
387
+ });
388
+ }
389
+ else if (transactInfoItem.kind === "update") {
390
+ const dataMust = this._mocody_getBaseObject({ dataId: transactInfoItem.dataId });
391
+ const fullData = {
392
+ ...transactInfoItem.data,
393
+ ...dataMust,
394
+ };
395
+ const { validatedData } = await this._mocody_allHelpValidateGetValue(fullData);
396
+ this._mocody_checkValidateStrictRequiredFields(validatedData);
397
+ const validatedData01 = this._mocody_formatTTL(validatedData);
398
+ prepareTransactData.push({
399
+ partitionKeyFieldName,
400
+ kind: transactInfoItem.kind,
401
+ tableName: tableFullName,
402
+ data: mocody_utils_1.MocodyUtil.marshallFromJson(validatedData01),
403
+ keyQuery: {
404
+ [partitionKeyFieldName]: { S: transactInfoItem.dataId },
405
+ [sortKeyFieldName]: { S: featureEntityValue },
406
+ },
407
+ });
408
+ }
409
+ else if (transactInfoItem.kind === "delete") {
410
+ prepareTransactData.push({
411
+ partitionKeyFieldName,
412
+ kind: transactInfoItem.kind,
413
+ tableName: tableFullName,
414
+ keyQuery: {
415
+ [partitionKeyFieldName]: { S: transactInfoItem.dataId },
416
+ [sortKeyFieldName]: { S: featureEntityValue },
417
+ },
418
+ });
419
+ }
420
+ }
421
+ return Promise.resolve(prepareTransactData);
422
+ }
423
+ async mocody_executeTransaction({ transactInfo }) {
424
+ const transactData = {
425
+ TransactItems: [],
426
+ };
427
+ for (const item of transactInfo) {
428
+ if (item.kind === "create") {
429
+ const query01 = {
430
+ [item.partitionKeyFieldName]: { $exists: false },
431
+ };
432
+ const { expressionAttributeNames, expressionAttributeValues, filterExpression, } = this._mocody_queryFilter.processQueryFilter({
433
+ queryDefs: query01,
434
+ projectionFields: undefined,
435
+ });
436
+ transactData.TransactItems?.push({
437
+ Put: {
438
+ TableName: item.tableName,
439
+ Item: item.data,
440
+ ConditionExpression: filterExpression,
441
+ ExpressionAttributeNames: expressionAttributeNames,
442
+ ExpressionAttributeValues: expressionAttributeValues,
443
+ },
444
+ });
445
+ }
446
+ else if (item.kind === "update") {
447
+ const query01 = {
448
+ [item.partitionKeyFieldName]: { $exists: true },
449
+ };
450
+ const { expressionAttributeNames, expressionAttributeValues, filterExpression, } = this._mocody_queryFilter.processQueryFilter({
451
+ queryDefs: query01,
452
+ projectionFields: undefined,
453
+ });
454
+ transactData.TransactItems?.push({
455
+ Put: {
456
+ TableName: item.tableName,
457
+ Item: item.data,
458
+ ConditionExpression: filterExpression,
459
+ ExpressionAttributeNames: expressionAttributeNames,
460
+ ExpressionAttributeValues: expressionAttributeValues,
461
+ },
462
+ });
463
+ }
464
+ else if (item.kind === "delete") {
465
+ const query01 = {
466
+ [item.partitionKeyFieldName]: { $exists: true },
467
+ };
468
+ const { expressionAttributeNames, expressionAttributeValues, filterExpression, } = this._mocody_queryFilter.processQueryFilter({
469
+ queryDefs: query01,
470
+ projectionFields: undefined,
471
+ });
472
+ transactData.TransactItems?.push({
473
+ Delete: {
474
+ TableName: item.tableName,
475
+ Key: item.keyQuery,
476
+ ConditionExpression: filterExpression,
477
+ ExpressionAttributeNames: expressionAttributeNames,
478
+ ExpressionAttributeValues: expressionAttributeValues,
479
+ },
480
+ });
481
+ }
482
+ }
483
+ await this._mocody_dynamoInit().transactWriteItems(transactData);
484
+ }
485
+ _mocody_getProjectionFields({ excludeFields, fields, }) {
486
+ return mocody_utils_1.MocodyUtil.getProjectionFields({
487
+ excludeFields,
488
+ fields,
489
+ entityFields: Array.from(this._mocody_entityFieldsKeySet),
490
+ });
491
+ }
492
+ async mocody_getManyByIds({ dataIds, fields, excludeFields, withCondition, }) {
493
+ dataIds.forEach((dataId) => {
494
+ this._mocody_errorHelper.mocody_helper_validateRequiredString({
495
+ BatchGetDataId: dataId,
496
+ });
497
+ });
498
+ const originalIds = this._mocody_removeDuplicateString(dataIds);
499
+ const BATCH_SIZE = 80;
500
+ const batchIds = lodash_1.default.chunk(originalIds, BATCH_SIZE);
501
+ logging_service_1.LoggingService.log({
502
+ batchIds,
503
+ "@mocody_getManyByIds batchIds": batchIds.length,
504
+ });
505
+ let resultAll = [];
506
+ const fieldKeys = this._mocody_getProjectionFields({ fields, excludeFields });
507
+ for (const batch of batchIds) {
508
+ const callByIds = await this.mocody_batchGetManyByIdsBasePrivate({
509
+ dataIds: batch,
510
+ fields: fieldKeys,
511
+ withCondition,
512
+ });
513
+ resultAll = [...resultAll, ...callByIds];
514
+ }
515
+ logging_service_1.LoggingService.log("@mocody_getManyByIds batchIds result Out: ", resultAll.length);
516
+ return resultAll;
517
+ }
518
+ async mocody_batchGetManyByIdsBasePrivate({ dataIds, fields, excludeFields, withCondition, }) {
519
+ const { tableFullName, partitionKeyFieldName, sortKeyFieldName, featureEntityValue, } = this._mocody_getLocalVariables();
520
+ const dataIdsNoDup = this._mocody_removeDuplicateString(dataIds);
521
+ let projectionExpression;
522
+ let expressionAttributeNames;
523
+ const fields01 = this._mocody_getProjectionFields({ fields, excludeFields });
524
+ if (fields01?.length) {
525
+ const fieldKeys = new Set(fields01);
526
+ if (withCondition?.length) {
527
+ withCondition.forEach((condition) => {
528
+ fieldKeys.add(condition.field);
529
+ });
530
+ }
531
+ expressionAttributeNames = {};
532
+ fieldKeys.forEach((fieldName) => {
533
+ if (typeof fieldName === "string") {
534
+ if (expressionAttributeNames) {
535
+ const attrKeyHash = (0, dynamo_helper_1.getDynamoRandomKeyOrHash)("#");
536
+ expressionAttributeNames[attrKeyHash] = fieldName;
537
+ }
538
+ }
539
+ });
540
+ if (Object.keys(expressionAttributeNames)?.length) {
541
+ projectionExpression = Object.keys(expressionAttributeNames).join(",");
542
+ }
543
+ else {
544
+ projectionExpression = undefined;
545
+ expressionAttributeNames = undefined;
546
+ }
547
+ }
548
+ const getArray = dataIdsNoDup.map((dataId) => {
549
+ const params01 = {
550
+ [partitionKeyFieldName]: { S: dataId },
551
+ [sortKeyFieldName]: { S: featureEntityValue },
552
+ };
553
+ return params01;
554
+ });
555
+ const params = {
556
+ RequestItems: {
557
+ [tableFullName]: {
558
+ Keys: [...getArray],
559
+ ProjectionExpression: projectionExpression,
560
+ ExpressionAttributeNames: expressionAttributeNames,
561
+ },
562
+ },
563
+ };
564
+ logging_service_1.LoggingService.log({ batchGetItemParams: params });
565
+ let hasNext = true;
566
+ let params01 = { ...params };
567
+ let returnedItems = [];
568
+ const dynamo = this._mocody_dynamoInit();
569
+ logging_service_1.LoggingService.log({ fetchForDataIds: dataIds });
570
+ while (hasNext) {
571
+ try {
572
+ const { Responses, UnprocessedKeys } = await dynamo.batchGetItem(params01);
573
+ if (Responses) {
574
+ const itemListRaw = Responses[tableFullName];
575
+ if (itemListRaw?.length) {
576
+ const itemList = itemListRaw.map((item) => mocody_utils_1.MocodyUtil.unmarshallToJson(item));
577
+ returnedItems = [...returnedItems, ...itemList];
578
+ }
579
+ }
580
+ if (UnprocessedKeys && Object.keys(UnprocessedKeys).length) {
581
+ params01 = {
582
+ RequestItems: UnprocessedKeys,
583
+ };
584
+ logging_service_1.LoggingService.log({ dynamoBatchGetParams: params01 });
585
+ }
586
+ else {
587
+ hasNext = false;
588
+ break;
589
+ }
590
+ }
591
+ catch (error) {
592
+ hasNext = false;
593
+ logging_service_1.LoggingService.error(error);
594
+ if (!returnedItems?.length) {
595
+ throw error;
596
+ }
597
+ else {
598
+ break;
599
+ }
600
+ }
601
+ }
602
+ if (returnedItems?.length && withCondition?.length) {
603
+ const returnedItems01 = [];
604
+ returnedItems.forEach((item) => {
605
+ const canInclude = withCondition.every((condition) => item[condition.field] === condition.equals);
606
+ if (canInclude) {
607
+ returnedItems01.push(item);
608
+ }
609
+ });
610
+ return returnedItems01;
611
+ }
612
+ return returnedItems;
613
+ }
614
+ async mocody_getManyByIndex(paramOption) {
615
+ const result = await this._mocody_getManyBySecondaryIndexPaginateBase({
616
+ paramOption,
617
+ canPaginate: false,
618
+ enableRelationFetch: false,
619
+ });
620
+ if (result?.paginationResults) {
621
+ return result.paginationResults;
622
+ }
623
+ return [];
624
+ }
625
+ async mocody_getManyByIndexPaginate(paramOption) {
626
+ return this._mocody_getManyBySecondaryIndexPaginateBase({
627
+ paramOption,
628
+ canPaginate: true,
629
+ enableRelationFetch: false,
630
+ });
631
+ }
632
+ async mocody_getManyWithRelation(paramOption) {
633
+ const result = await this._mocody_getManyBySecondaryIndexPaginateBase({
634
+ paramOption,
635
+ canPaginate: false,
636
+ enableRelationFetch: true,
637
+ });
638
+ if (result?.paginationResults) {
639
+ return result.paginationResults;
640
+ }
641
+ return [];
642
+ }
643
+ async mocody_getManyWithRelationPaginate(paramOption) {
644
+ if (process.env.MOCODY_USE_PARTIQL_QUERY_FILTER === "true") {
645
+ return this._mocody_getManyBySecondaryIndexPartiQlPaginateBase({
646
+ paramOption,
647
+ canPaginate: true,
648
+ enableRelationFetch: true,
649
+ });
650
+ }
651
+ return this._mocody_getManyBySecondaryIndexPaginateBase({
652
+ paramOption,
653
+ canPaginate: true,
654
+ enableRelationFetch: true,
655
+ });
656
+ }
657
+ async _mocody_getManyBySecondaryIndexPaginateBase({ paramOption, canPaginate, enableRelationFetch, }) {
658
+ const { tableFullName, secondaryIndexOptions, partitionKeyFieldName, sortKeyFieldName, featureEntityValue, } = this._mocody_getLocalVariables();
659
+ if (!secondaryIndexOptions?.length) {
660
+ throw this._mocody_createGenericError("Invalid secondary index definitions");
661
+ }
662
+ const paramOption01 = { ...paramOption };
663
+ if (!paramOption01?.indexName) {
664
+ throw this._mocody_createGenericError("Invalid index name input");
665
+ }
666
+ const secondaryIndex = secondaryIndexOptions.find((item) => {
667
+ return item.indexName === paramOption01.indexName;
668
+ });
669
+ if (!secondaryIndex) {
670
+ throw this._mocody_createGenericError(`Secondary index '${paramOption01.indexName}' not named/defined in secondaryIndexOptions`);
671
+ }
672
+ let projectionFields;
673
+ const fields01 = this._mocody_getProjectionFields({
674
+ fields: paramOption01.fields,
675
+ excludeFields: paramOption01.excludeFields,
676
+ });
677
+ if (canPaginate && fields01?.length) {
678
+ const fieldSet01 = new Set(fields01);
679
+ fieldSet01.add(partitionKeyFieldName);
680
+ projectionFields = Array.from(fieldSet01);
681
+ }
682
+ else {
683
+ projectionFields = fields01;
684
+ }
685
+ let evaluationLimit01;
686
+ let resultLimit01;
687
+ if (paramOption01.limit && util_service_1.UtilService.isNumericInteger(paramOption01.limit)) {
688
+ resultLimit01 = Number(paramOption01.limit);
689
+ }
690
+ if (paramOption01?.pagingParams?.evaluationLimit) {
691
+ evaluationLimit01 = paramOption01?.pagingParams?.evaluationLimit;
692
+ }
693
+ else if (!paramOption01.query && resultLimit01) {
694
+ evaluationLimit01 = resultLimit01;
695
+ }
696
+ const current_PartitionKeyFieldName = secondaryIndex.partitionKeyFieldName;
697
+ const current_SortKeyFieldName = secondaryIndex.sortKeyFieldName;
698
+ const default_partitionAndSortKey = [partitionKeyFieldName, sortKeyFieldName];
699
+ const current_partitionAndSortKey = [current_PartitionKeyFieldName, current_SortKeyFieldName];
700
+ const partitionSortKeyQuery = paramOption01.sortKeyQuery
701
+ ? {
702
+ ...{ [current_SortKeyFieldName]: paramOption01.sortKeyQuery },
703
+ ...{ [current_PartitionKeyFieldName]: paramOption01.partitionKeyValue },
704
+ }
705
+ : { [current_PartitionKeyFieldName]: paramOption01.partitionKeyValue };
706
+ if (!enableRelationFetch) {
707
+ const localVariables = this._mocody_getLocalVariables();
708
+ const hasFeatureEntity = [
709
+ current_PartitionKeyFieldName,
710
+ current_SortKeyFieldName,
711
+ ].includes(localVariables.sortKeyFieldName);
712
+ if (!hasFeatureEntity) {
713
+ paramOption01.query = (paramOption01.query || {});
714
+ paramOption01.query = {
715
+ ...paramOption01.query,
716
+ ...this._mocody_featureEntity_Key_Value,
717
+ };
718
+ }
719
+ else if (current_PartitionKeyFieldName !== localVariables.sortKeyFieldName) {
720
+ if (localVariables.sortKeyFieldName === current_SortKeyFieldName) {
721
+ partitionSortKeyQuery[current_SortKeyFieldName] = { $eq: localVariables.featureEntityValue };
722
+ }
723
+ }
724
+ }
725
+ const mainFilter = this._mocody_queryFilter.processQueryFilter({
726
+ queryDefs: partitionSortKeyQuery,
727
+ projectionFields,
728
+ });
729
+ let otherFilterExpression;
730
+ let otherExpressionAttributeValues;
731
+ let otherExpressionAttributeNames;
732
+ if (paramOption01.query) {
733
+ const otherFilter = this._mocody_queryFilter.processQueryFilter({
734
+ queryDefs: paramOption01.query,
735
+ projectionFields: null,
736
+ });
737
+ otherExpressionAttributeValues = otherFilter.expressionAttributeValues;
738
+ otherExpressionAttributeNames = otherFilter.expressionAttributeNames;
739
+ if (otherFilter?.filterExpression?.length && otherFilter?.filterExpression.length > 1) {
740
+ otherFilterExpression = otherFilter.filterExpression;
741
+ }
742
+ }
743
+ const params = {
744
+ TableName: tableFullName,
745
+ IndexName: paramOption01.indexName,
746
+ KeyConditionExpression: mainFilter.filterExpression,
747
+ FilterExpression: otherFilterExpression ?? undefined,
748
+ ExpressionAttributeValues: {
749
+ ...otherExpressionAttributeValues,
750
+ ...mainFilter.expressionAttributeValues,
751
+ },
752
+ ExpressionAttributeNames: {
753
+ ...otherExpressionAttributeNames,
754
+ ...mainFilter.expressionAttributeNames,
755
+ },
756
+ };
757
+ const orderDesc = paramOption01?.sort === "desc";
758
+ if (orderDesc) {
759
+ params.ScanIndexForward = false;
760
+ }
761
+ else {
762
+ params.ScanIndexForward = true;
763
+ }
764
+ if (mainFilter.projectionExpressionAttr) {
765
+ params.ProjectionExpression = mainFilter.projectionExpressionAttr;
766
+ }
767
+ let nextPageHash01 = paramOption01?.pagingParams?.nextPageHash;
768
+ if (nextPageHash01 === "undefined") {
769
+ nextPageHash01 = undefined;
770
+ }
771
+ if (nextPageHash01 === "null") {
772
+ nextPageHash01 = undefined;
773
+ }
774
+ const result = await this._mocody_queryScanProcessor.mocody__helperDynamoQueryProcessor({
775
+ dynamoDb: () => this._mocody_dynamoInit(),
776
+ params,
777
+ orderDesc,
778
+ canPaginate,
779
+ tableFullName,
780
+ featureEntityValue,
781
+ default_partitionAndSortKey,
782
+ current_partitionAndSortKey,
783
+ evaluationLimit: evaluationLimit01,
784
+ nextPageHash: nextPageHash01,
785
+ resultLimit: resultLimit01,
786
+ });
787
+ return result;
788
+ }
789
+ async _mocody_getManyBySecondaryIndexPartiQlPaginateBase({ paramOption, canPaginate, enableRelationFetch, }) {
790
+ const { tableFullName, secondaryIndexOptions, partitionKeyFieldName, sortKeyFieldName, featureEntityValue, } = this._mocody_getLocalVariables();
791
+ if (!secondaryIndexOptions?.length) {
792
+ throw this._mocody_createGenericError("Invalid secondary index definitions");
793
+ }
794
+ const paramOption01 = { ...paramOption };
795
+ if (!paramOption01?.indexName) {
796
+ throw this._mocody_createGenericError("Invalid index name input");
797
+ }
798
+ const secondaryIndex = secondaryIndexOptions.find((item) => {
799
+ return item.indexName === paramOption01.indexName;
800
+ });
801
+ if (!secondaryIndex) {
802
+ throw this._mocody_createGenericError(`Secondary index '${paramOption01.indexName}' not named/defined in secondaryIndexOptions`);
803
+ }
804
+ let projectionFields;
805
+ const fields01 = this._mocody_getProjectionFields({
806
+ fields: paramOption01.fields,
807
+ excludeFields: paramOption01.excludeFields,
808
+ });
809
+ if (canPaginate && fields01?.length) {
810
+ const fieldSet01 = new Set(fields01);
811
+ fieldSet01.add(partitionKeyFieldName);
812
+ projectionFields = Array.from(fieldSet01);
813
+ }
814
+ else {
815
+ projectionFields = fields01;
816
+ }
817
+ let evaluationLimit01;
818
+ let resultLimit01;
819
+ if (paramOption01.limit && util_service_1.UtilService.isNumericInteger(paramOption01.limit)) {
820
+ resultLimit01 = Number(paramOption01.limit);
821
+ }
822
+ if (paramOption01?.pagingParams?.evaluationLimit) {
823
+ evaluationLimit01 = paramOption01?.pagingParams?.evaluationLimit;
824
+ }
825
+ else if (!paramOption01.query && resultLimit01) {
826
+ evaluationLimit01 = resultLimit01;
827
+ }
828
+ const current_PartitionKeyFieldName = secondaryIndex.partitionKeyFieldName;
829
+ const current_SortKeyFieldName = secondaryIndex.sortKeyFieldName;
830
+ const default_partitionAndSortKey = [partitionKeyFieldName, sortKeyFieldName];
831
+ const current_partitionAndSortKey = [current_PartitionKeyFieldName, current_SortKeyFieldName];
832
+ const partitionSortKeyQuery = paramOption01.sortKeyQuery
833
+ ? {
834
+ ...{ [current_PartitionKeyFieldName]: paramOption01.partitionKeyValue },
835
+ ...{ [current_SortKeyFieldName]: paramOption01.sortKeyQuery },
836
+ }
837
+ : { [current_PartitionKeyFieldName]: paramOption01.partitionKeyValue };
838
+ if (!enableRelationFetch) {
839
+ const localVariables = this._mocody_getLocalVariables();
840
+ const hasFeatureEntity = [
841
+ current_PartitionKeyFieldName,
842
+ current_SortKeyFieldName,
843
+ ].includes(localVariables.sortKeyFieldName);
844
+ if (!hasFeatureEntity) {
845
+ paramOption01.query = (paramOption01.query || {});
846
+ paramOption01.query = {
847
+ ...paramOption01.query,
848
+ ...this._mocody_featureEntity_Key_Value,
849
+ };
850
+ }
851
+ else if (current_PartitionKeyFieldName !== localVariables.sortKeyFieldName) {
852
+ if (localVariables.sortKeyFieldName === current_SortKeyFieldName) {
853
+ partitionSortKeyQuery[current_SortKeyFieldName] = { $eq: localVariables.featureEntityValue };
854
+ }
855
+ }
856
+ }
857
+ const subStatement = [];
858
+ const subParameter = [];
859
+ const mainFilter = this._mocody_queryPartiQlFilter.processQueryFilter({
860
+ queryDefs: partitionSortKeyQuery,
861
+ });
862
+ logging_service_1.LoggingService.logAsString({ mainFilter });
863
+ if (mainFilter?.subStatement) {
864
+ subStatement.push(mainFilter.subStatement);
865
+ subParameter.push(...mainFilter.subParameter);
866
+ }
867
+ if (paramOption01.query) {
868
+ const otherFilter = this._mocody_queryPartiQlFilter.processQueryFilter({
869
+ queryDefs: paramOption01.query,
870
+ });
871
+ logging_service_1.LoggingService.logAsString({ otherFilter });
872
+ subStatement.push(otherFilter.subStatement);
873
+ subParameter.push(...otherFilter.subParameter);
874
+ }
875
+ const orderDesc = paramOption01?.sort === "desc";
876
+ let nextPageHash01 = paramOption01?.pagingParams?.nextPageHash;
877
+ if (nextPageHash01 === "undefined") {
878
+ nextPageHash01 = undefined;
879
+ }
880
+ if (nextPageHash01 === "null") {
881
+ nextPageHash01 = undefined;
882
+ }
883
+ logging_service_1.LoggingService.logAsString({ subStatement, subParameter });
884
+ const result = await this._mocody_queryPartiQlProcessor.mocody__helperDynamoQueryProcessor({
885
+ dynamoDb: () => this._mocody_dynamoInit(),
886
+ params: { subStatement, subParameter },
887
+ orderDesc,
888
+ canPaginate,
889
+ tableFullName,
890
+ indexName: paramOption01.indexName,
891
+ featureEntityValue,
892
+ projectionFields: projectionFields,
893
+ default_partitionAndSortKey,
894
+ current_partitionAndSortKey,
895
+ evaluationLimit: evaluationLimit01,
896
+ nextPageHash: nextPageHash01,
897
+ resultLimit: resultLimit01,
898
+ });
899
+ return result;
900
+ }
901
+ async mocody_deleteById({ dataId, withCondition }) {
902
+ this._mocody_errorHelper.mocody_helper_validateRequiredString({ Del1SortKey: dataId });
903
+ const { tableFullName, partitionKeyFieldName, sortKeyFieldName, featureEntityValue, } = this._mocody_getLocalVariables();
904
+ const dataExist = await this.mocody_getOneById({ dataId, withCondition });
905
+ if (!(dataExist && dataExist[partitionKeyFieldName])) {
906
+ throw this._mocody_errorHelper.mocody_helper_createFriendlyError("Record does NOT exists");
907
+ }
908
+ const params = {
909
+ TableName: tableFullName,
910
+ Key: {
911
+ [partitionKeyFieldName]: { S: dataId },
912
+ [sortKeyFieldName]: { S: featureEntityValue },
913
+ },
914
+ };
915
+ try {
916
+ await this._mocody_dynamoInit().deleteItem(params);
917
+ }
918
+ catch (err) {
919
+ if (err && err.code === "ResourceNotFoundException") {
920
+ throw this._mocody_errorHelper.mocody_helper_createFriendlyError("Table not found");
921
+ }
922
+ else if (err && err.code === "ResourceInUseException") {
923
+ throw this._mocody_errorHelper.mocody_helper_createFriendlyError("Table in use");
924
+ }
925
+ else {
926
+ throw err;
927
+ }
928
+ }
929
+ return dataExist;
930
+ }
931
+ }
932
+ exports.DynamoDataOperation = DynamoDataOperation;
798
933
  //# sourceMappingURL=dynamo-data-operation.js.map