mocody 0.6.6 → 0.6.8

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