mocody 0.4.38 → 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 -690
  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,420 +1,420 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DynamoManageTable = void 0;
4
- const util_service_1 = require("../helpers/util-service");
5
- const logging_service_1 = require("../helpers/logging-service");
6
- const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
7
- class DynamoManageTable {
8
- constructor({ dynamoDb, secondaryIndexOptions, tableFullName, partitionKeyFieldName, sortKeyFieldName, }) {
9
- Object.defineProperty(this, "partitionKeyFieldName", {
10
- enumerable: true,
11
- configurable: true,
12
- writable: true,
13
- value: void 0
14
- });
15
- Object.defineProperty(this, "sortKeyFieldName", {
16
- enumerable: true,
17
- configurable: true,
18
- writable: true,
19
- value: void 0
20
- });
21
- Object.defineProperty(this, "dynamoDb", {
22
- enumerable: true,
23
- configurable: true,
24
- writable: true,
25
- value: void 0
26
- });
27
- Object.defineProperty(this, "tableFullName", {
28
- enumerable: true,
29
- configurable: true,
30
- writable: true,
31
- value: void 0
32
- });
33
- Object.defineProperty(this, "secondaryIndexOptions", {
34
- enumerable: true,
35
- configurable: true,
36
- writable: true,
37
- value: void 0
38
- });
39
- this.dynamoDb = dynamoDb;
40
- this.tableFullName = tableFullName;
41
- this.partitionKeyFieldName = partitionKeyFieldName;
42
- this.sortKeyFieldName = sortKeyFieldName;
43
- this.secondaryIndexOptions = secondaryIndexOptions;
44
- }
45
- _tbl_getLocalVariables() {
46
- return {
47
- partitionKeyFieldName: this.partitionKeyFieldName,
48
- sortKeyFieldName: this.sortKeyFieldName,
49
- tableFullName: this.tableFullName,
50
- secondaryIndexOptions: this.secondaryIndexOptions,
51
- };
52
- }
53
- _tbl_dynamoDb() {
54
- return this.dynamoDb();
55
- }
56
- async mocody_tbl_getListOfTablesNamesOnline() {
57
- const params = {
58
- Limit: 99,
59
- };
60
- const dynamo = await this._tbl_dynamoDb().getInstance();
61
- const listOfTables = await dynamo.send(new client_dynamodb_1.ListTablesCommand(params));
62
- return listOfTables?.TableNames;
63
- }
64
- async mocody_updateTTL({ isEnabled }) {
65
- const { tableFullName } = this._tbl_getLocalVariables();
66
- const fieldName = "dangerouslyExpireAtTTL";
67
- const params = {
68
- TableName: tableFullName,
69
- TimeToLiveSpecification: {
70
- AttributeName: fieldName,
71
- Enabled: isEnabled,
72
- },
73
- };
74
- const dynamo = await this._tbl_dynamoDb().getInstance();
75
- const result = await dynamo.send(new client_dynamodb_1.UpdateTimeToLiveCommand(params));
76
- if (result?.TimeToLiveSpecification) {
77
- return result.TimeToLiveSpecification;
78
- }
79
- return null;
80
- }
81
- async mocody_tbl_getTableInfo() {
82
- try {
83
- const { tableFullName } = this._tbl_getLocalVariables();
84
- const params = {
85
- TableName: tableFullName,
86
- };
87
- const dynamo = await this._tbl_dynamoDb().getInstance();
88
- const result = await dynamo.send(new client_dynamodb_1.DescribeTableCommand(params));
89
- if (result?.Table?.TableName === tableFullName) {
90
- return result.Table;
91
- }
92
- return null;
93
- }
94
- catch (error) {
95
- logging_service_1.LoggingService.log({ "@allGetTableInfoBase": "", error: error?.message });
96
- return null;
97
- }
98
- }
99
- async mocody_tbl_checkTableExists() {
100
- const result = await this.mocody_tbl_getTableInfo();
101
- if (!result) {
102
- return false;
103
- }
104
- if (result?.GlobalSecondaryIndexes) {
105
- }
106
- return true;
107
- }
108
- async _allUpdateGlobalSecondaryIndexBase({ secondaryIndexOptions, existingTableInfo, }) {
109
- try {
110
- const existingIndexNames = [];
111
- const staledIndexNames = [];
112
- const allIndexNames = [];
113
- const newSecondaryIndexOptions = [];
114
- const updateResults = [];
115
- if (existingTableInfo?.GlobalSecondaryIndexes?.length) {
116
- existingTableInfo?.GlobalSecondaryIndexes.forEach((indexInfo) => {
117
- if (indexInfo.IndexName) {
118
- existingIndexNames.push(indexInfo.IndexName);
119
- }
120
- });
121
- }
122
- secondaryIndexOptions?.forEach((newIndexInfo) => {
123
- allIndexNames.push(newIndexInfo.indexName);
124
- const indexExists = existingIndexNames.includes(newIndexInfo.indexName);
125
- if (!indexExists) {
126
- newSecondaryIndexOptions.push(newIndexInfo);
127
- }
128
- });
129
- existingIndexNames.forEach((indexName) => {
130
- const existsInList = allIndexNames.includes(indexName);
131
- if (!existsInList) {
132
- staledIndexNames.push(indexName);
133
- }
134
- });
135
- if (!(newSecondaryIndexOptions.length || staledIndexNames.length)) {
136
- return null;
137
- }
138
- let canUpdate = false;
139
- const { tableFullName } = this._tbl_getLocalVariables();
140
- logging_service_1.LoggingService.log({
141
- secondaryIndexOptions: secondaryIndexOptions.length,
142
- newSecondaryIndexOptions: newSecondaryIndexOptions.length,
143
- staledIndexNames: staledIndexNames.length,
144
- tableName: tableFullName,
145
- });
146
- if (newSecondaryIndexOptions.length) {
147
- canUpdate = true;
148
- let indexCount = 0;
149
- for (const indexOption of newSecondaryIndexOptions) {
150
- const params = {
151
- TableName: tableFullName,
152
- GlobalSecondaryIndexUpdates: [],
153
- };
154
- indexCount++;
155
- const creationParams = this._getGlobalSecondaryIndexCreationParams({
156
- secondaryIndexOptions: [indexOption],
157
- });
158
- const indexName = creationParams.xGlobalSecondaryIndex[0].IndexName;
159
- params.AttributeDefinitions = [...creationParams.xAttributeDefinitions];
160
- creationParams.xGlobalSecondaryIndex.forEach((gsi) => {
161
- params.GlobalSecondaryIndexUpdates?.push({
162
- Create: {
163
- ...gsi,
164
- },
165
- });
166
- });
167
- const dynamo = await this._tbl_dynamoDb().getInstance();
168
- const result = await dynamo.send(new client_dynamodb_1.UpdateTableCommand(params));
169
- if (result?.TableDescription) {
170
- updateResults.push(result?.TableDescription);
171
- }
172
- logging_service_1.LoggingService.log([
173
- `Creating Index: '${indexName}'`,
174
- `on table '${tableFullName}' started:`,
175
- new Date().toTimeString(),
176
- ].join(" "));
177
- if (indexCount !== newSecondaryIndexOptions.length) {
178
- await util_service_1.UtilService.waitUntilMunites(5);
179
- }
180
- }
181
- }
182
- if (staledIndexNames.length) {
183
- if (canUpdate) {
184
- await util_service_1.UtilService.waitUntilMunites(4);
185
- }
186
- canUpdate = true;
187
- let indexCount = 0;
188
- for (const indexName of staledIndexNames) {
189
- const params = {
190
- TableName: tableFullName,
191
- GlobalSecondaryIndexUpdates: [],
192
- };
193
- indexCount++;
194
- params.GlobalSecondaryIndexUpdates?.push({
195
- Delete: {
196
- IndexName: indexName,
197
- },
198
- });
199
- const dynamo = await this._tbl_dynamoDb().getInstance();
200
- const result = await dynamo.send(new client_dynamodb_1.UpdateTableCommand(params));
201
- if (result?.TableDescription) {
202
- updateResults.push(result?.TableDescription);
203
- }
204
- logging_service_1.LoggingService.log([
205
- `Deleting Index: '${indexName}'`,
206
- `on table '${tableFullName}' started:`,
207
- new Date().toTimeString(),
208
- ].join(" "));
209
- if (indexCount !== staledIndexNames.length) {
210
- await util_service_1.UtilService.waitUntilMunites(1);
211
- }
212
- }
213
- }
214
- if (!canUpdate) {
215
- return null;
216
- }
217
- if (updateResults.length) {
218
- logging_service_1.LoggingService.log({
219
- "@allCreateGlobalSecondaryIndexBase": "",
220
- updateResults,
221
- });
222
- return updateResults;
223
- }
224
- return null;
225
- }
226
- catch (error) {
227
- logging_service_1.LoggingService.log({
228
- "@allCreateGlobalSecondaryIndexBase": "",
229
- error: error?.message,
230
- });
231
- return null;
232
- }
233
- }
234
- async mocody_createOrUpdateDefinedIndexes() {
235
- const { secondaryIndexOptions } = this._tbl_getLocalVariables();
236
- const existingTableInfo = await this.mocody_tbl_getTableInfo();
237
- if (existingTableInfo) {
238
- if (secondaryIndexOptions?.length) {
239
- const result01 = await this._allUpdateGlobalSecondaryIndexBase({
240
- secondaryIndexOptions,
241
- existingTableInfo,
242
- });
243
- logging_service_1.LoggingService.log(result01);
244
- return result01;
245
- }
246
- }
247
- logging_service_1.LoggingService.log("Table does not exists");
248
- return null;
249
- }
250
- async mocody_tbl_createTableIfNotExists() {
251
- const { secondaryIndexOptions } = this._tbl_getLocalVariables();
252
- const existingTableInfo = await this.mocody_tbl_getTableInfo();
253
- if (existingTableInfo) {
254
- if (secondaryIndexOptions?.length) {
255
- await this._allUpdateGlobalSecondaryIndexBase({
256
- secondaryIndexOptions,
257
- existingTableInfo,
258
- });
259
- }
260
- else if (existingTableInfo.GlobalSecondaryIndexes?.length) {
261
- await this._allUpdateGlobalSecondaryIndexBase({
262
- secondaryIndexOptions: [],
263
- existingTableInfo,
264
- });
265
- }
266
- return null;
267
- }
268
- return await this.mocody_tbl_createTable();
269
- }
270
- _getGlobalSecondaryIndexCreationParams({ secondaryIndexOptions, }) {
271
- const { tableFullName } = this._tbl_getLocalVariables();
272
- const params = {
273
- KeySchema: [],
274
- AttributeDefinitions: [],
275
- TableName: tableFullName,
276
- GlobalSecondaryIndexes: [],
277
- };
278
- const attributeDefinitionsNameList = [];
279
- secondaryIndexOptions.forEach((sIndex) => {
280
- const { indexName, partitionKeyFieldName, sortKeyFieldName, dataTypes, projectionFieldsInclude } = sIndex;
281
- const partitionKeyFieldName01 = partitionKeyFieldName;
282
- const sortKeyFieldName01 = sortKeyFieldName;
283
- if (!attributeDefinitionsNameList.includes(partitionKeyFieldName01)) {
284
- attributeDefinitionsNameList.push(partitionKeyFieldName01);
285
- params?.AttributeDefinitions?.push({
286
- AttributeName: partitionKeyFieldName01,
287
- AttributeType: dataTypes.partitionKeyDataType,
288
- });
289
- }
290
- if (!attributeDefinitionsNameList.includes(sortKeyFieldName01)) {
291
- attributeDefinitionsNameList.push(sortKeyFieldName01);
292
- params?.AttributeDefinitions?.push({
293
- AttributeName: sortKeyFieldName01,
294
- AttributeType: dataTypes.sortKeyDataType,
295
- });
296
- }
297
- let projectionFields = (projectionFieldsInclude || []);
298
- let projectionType = client_dynamodb_1.ProjectionType.ALL;
299
- if (projectionFields?.length) {
300
- projectionFields = projectionFields.filter((field) => {
301
- return field !== partitionKeyFieldName01 && field !== sortKeyFieldName01;
302
- });
303
- if (projectionFields.length === 0) {
304
- projectionType = client_dynamodb_1.ProjectionType.KEYS_ONLY;
305
- }
306
- else {
307
- projectionType = client_dynamodb_1.ProjectionType.INCLUDE;
308
- }
309
- }
310
- params.GlobalSecondaryIndexes?.push({
311
- IndexName: indexName,
312
- Projection: {
313
- ProjectionType: projectionType,
314
- NonKeyAttributes: projectionType === client_dynamodb_1.ProjectionType.INCLUDE ? projectionFields : undefined,
315
- },
316
- KeySchema: [
317
- {
318
- AttributeName: partitionKeyFieldName01,
319
- KeyType: "HASH",
320
- },
321
- {
322
- AttributeName: sortKeyFieldName01,
323
- KeyType: "RANGE",
324
- },
325
- ],
326
- });
327
- });
328
- return {
329
- xAttributeDefinitions: params.AttributeDefinitions || [],
330
- xGlobalSecondaryIndex: params.GlobalSecondaryIndexes || [],
331
- };
332
- }
333
- async mocody_tbl_createTable() {
334
- const { partitionKeyFieldName, sortKeyFieldName, tableFullName, secondaryIndexOptions } = this._tbl_getLocalVariables();
335
- const params = {
336
- AttributeDefinitions: [
337
- {
338
- AttributeName: partitionKeyFieldName,
339
- AttributeType: "S",
340
- },
341
- {
342
- AttributeName: sortKeyFieldName,
343
- AttributeType: "S",
344
- },
345
- ],
346
- KeySchema: [
347
- {
348
- AttributeName: partitionKeyFieldName,
349
- KeyType: "HASH",
350
- },
351
- {
352
- AttributeName: sortKeyFieldName,
353
- KeyType: "RANGE",
354
- },
355
- ],
356
- BillingMode: "PAY_PER_REQUEST",
357
- TableName: tableFullName,
358
- };
359
- params.Tags = [
360
- {
361
- Key: "tablePrefix",
362
- Value: tableFullName,
363
- },
364
- {
365
- Key: `DDBTableGroupKey-${tableFullName}`,
366
- Value: tableFullName,
367
- },
368
- ];
369
- if (secondaryIndexOptions?.length) {
370
- const creationParams = this._getGlobalSecondaryIndexCreationParams({
371
- secondaryIndexOptions,
372
- });
373
- if (creationParams.xAttributeDefinitions?.length) {
374
- const existAttrDefNames = params.AttributeDefinitions?.map((def) => def.AttributeName);
375
- creationParams.xAttributeDefinitions.forEach((def) => {
376
- const alreadyDefined = existAttrDefNames?.includes(def.AttributeName);
377
- if (!alreadyDefined) {
378
- params.AttributeDefinitions?.push(def);
379
- }
380
- });
381
- }
382
- params.GlobalSecondaryIndexes = [...creationParams.xGlobalSecondaryIndex];
383
- }
384
- logging_service_1.LoggingService.log({
385
- "@allCreateTableBase, table: ": tableFullName,
386
- });
387
- const dynamo = await this._tbl_dynamoDb().getInstance();
388
- const result = await dynamo.send(new client_dynamodb_1.CreateTableCommand(params));
389
- if (result?.TableDescription) {
390
- logging_service_1.LoggingService.log([
391
- `@allCreateTableBase,`,
392
- `Created table: '${result?.TableDescription.TableName}'`,
393
- new Date().toTimeString(),
394
- ].join(" "));
395
- return result.TableDescription?.TableName;
396
- }
397
- return null;
398
- }
399
- async mocody_tbl_deleteGlobalSecondaryIndex(indexName) {
400
- const { tableFullName } = this._tbl_getLocalVariables();
401
- const params = {
402
- TableName: tableFullName,
403
- GlobalSecondaryIndexUpdates: [
404
- {
405
- Delete: {
406
- IndexName: indexName,
407
- },
408
- },
409
- ],
410
- };
411
- const dynamo = await this._tbl_dynamoDb().getInstance();
412
- const result = await dynamo.send(new client_dynamodb_1.UpdateTableCommand(params));
413
- if (result?.TableDescription) {
414
- return result.TableDescription?.TableName;
415
- }
416
- return null;
417
- }
418
- }
419
- exports.DynamoManageTable = DynamoManageTable;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DynamoManageTable = void 0;
4
+ const util_service_1 = require("../helpers/util-service");
5
+ const logging_service_1 = require("../helpers/logging-service");
6
+ const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
7
+ class DynamoManageTable {
8
+ constructor({ dynamoDb, secondaryIndexOptions, tableFullName, partitionKeyFieldName, sortKeyFieldName, }) {
9
+ Object.defineProperty(this, "partitionKeyFieldName", {
10
+ enumerable: true,
11
+ configurable: true,
12
+ writable: true,
13
+ value: void 0
14
+ });
15
+ Object.defineProperty(this, "sortKeyFieldName", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: void 0
20
+ });
21
+ Object.defineProperty(this, "dynamoDb", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: void 0
26
+ });
27
+ Object.defineProperty(this, "tableFullName", {
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true,
31
+ value: void 0
32
+ });
33
+ Object.defineProperty(this, "secondaryIndexOptions", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: void 0
38
+ });
39
+ this.dynamoDb = dynamoDb;
40
+ this.tableFullName = tableFullName;
41
+ this.partitionKeyFieldName = partitionKeyFieldName;
42
+ this.sortKeyFieldName = sortKeyFieldName;
43
+ this.secondaryIndexOptions = secondaryIndexOptions;
44
+ }
45
+ _tbl_getLocalVariables() {
46
+ return {
47
+ partitionKeyFieldName: this.partitionKeyFieldName,
48
+ sortKeyFieldName: this.sortKeyFieldName,
49
+ tableFullName: this.tableFullName,
50
+ secondaryIndexOptions: this.secondaryIndexOptions,
51
+ };
52
+ }
53
+ _tbl_dynamoDb() {
54
+ return this.dynamoDb();
55
+ }
56
+ async mocody_tbl_getListOfTablesNamesOnline() {
57
+ const params = {
58
+ Limit: 99,
59
+ };
60
+ const dynamo = await this._tbl_dynamoDb().getInstance();
61
+ const listOfTables = await dynamo.send(new client_dynamodb_1.ListTablesCommand(params));
62
+ return listOfTables?.TableNames;
63
+ }
64
+ async mocody_updateTTL({ isEnabled }) {
65
+ const { tableFullName } = this._tbl_getLocalVariables();
66
+ const fieldName = "dangerouslyExpireAtTTL";
67
+ const params = {
68
+ TableName: tableFullName,
69
+ TimeToLiveSpecification: {
70
+ AttributeName: fieldName,
71
+ Enabled: isEnabled,
72
+ },
73
+ };
74
+ const dynamo = await this._tbl_dynamoDb().getInstance();
75
+ const result = await dynamo.send(new client_dynamodb_1.UpdateTimeToLiveCommand(params));
76
+ if (result?.TimeToLiveSpecification) {
77
+ return result.TimeToLiveSpecification;
78
+ }
79
+ return null;
80
+ }
81
+ async mocody_tbl_getTableInfo() {
82
+ try {
83
+ const { tableFullName } = this._tbl_getLocalVariables();
84
+ const params = {
85
+ TableName: tableFullName,
86
+ };
87
+ const dynamo = await this._tbl_dynamoDb().getInstance();
88
+ const result = await dynamo.send(new client_dynamodb_1.DescribeTableCommand(params));
89
+ if (result?.Table?.TableName === tableFullName) {
90
+ return result.Table;
91
+ }
92
+ return null;
93
+ }
94
+ catch (error) {
95
+ logging_service_1.LoggingService.log({ "@allGetTableInfoBase": "", error: error?.message });
96
+ return null;
97
+ }
98
+ }
99
+ async mocody_tbl_checkTableExists() {
100
+ const result = await this.mocody_tbl_getTableInfo();
101
+ if (!result) {
102
+ return false;
103
+ }
104
+ if (result?.GlobalSecondaryIndexes) {
105
+ }
106
+ return true;
107
+ }
108
+ async _allUpdateGlobalSecondaryIndexBase({ secondaryIndexOptions, existingTableInfo, }) {
109
+ try {
110
+ const existingIndexNames = [];
111
+ const staledIndexNames = [];
112
+ const allIndexNames = [];
113
+ const newSecondaryIndexOptions = [];
114
+ const updateResults = [];
115
+ if (existingTableInfo?.GlobalSecondaryIndexes?.length) {
116
+ existingTableInfo?.GlobalSecondaryIndexes.forEach((indexInfo) => {
117
+ if (indexInfo.IndexName) {
118
+ existingIndexNames.push(indexInfo.IndexName);
119
+ }
120
+ });
121
+ }
122
+ secondaryIndexOptions?.forEach((newIndexInfo) => {
123
+ allIndexNames.push(newIndexInfo.indexName);
124
+ const indexExists = existingIndexNames.includes(newIndexInfo.indexName);
125
+ if (!indexExists) {
126
+ newSecondaryIndexOptions.push(newIndexInfo);
127
+ }
128
+ });
129
+ existingIndexNames.forEach((indexName) => {
130
+ const existsInList = allIndexNames.includes(indexName);
131
+ if (!existsInList) {
132
+ staledIndexNames.push(indexName);
133
+ }
134
+ });
135
+ if (!(newSecondaryIndexOptions.length || staledIndexNames.length)) {
136
+ return null;
137
+ }
138
+ let canUpdate = false;
139
+ const { tableFullName } = this._tbl_getLocalVariables();
140
+ logging_service_1.LoggingService.log({
141
+ secondaryIndexOptions: secondaryIndexOptions.length,
142
+ newSecondaryIndexOptions: newSecondaryIndexOptions.length,
143
+ staledIndexNames: staledIndexNames.length,
144
+ tableName: tableFullName,
145
+ });
146
+ if (newSecondaryIndexOptions.length) {
147
+ canUpdate = true;
148
+ let indexCount = 0;
149
+ for (const indexOption of newSecondaryIndexOptions) {
150
+ const params = {
151
+ TableName: tableFullName,
152
+ GlobalSecondaryIndexUpdates: [],
153
+ };
154
+ indexCount++;
155
+ const creationParams = this._getGlobalSecondaryIndexCreationParams({
156
+ secondaryIndexOptions: [indexOption],
157
+ });
158
+ const indexName = creationParams.xGlobalSecondaryIndex[0].IndexName;
159
+ params.AttributeDefinitions = [...creationParams.xAttributeDefinitions];
160
+ creationParams.xGlobalSecondaryIndex.forEach((gsi) => {
161
+ params.GlobalSecondaryIndexUpdates?.push({
162
+ Create: {
163
+ ...gsi,
164
+ },
165
+ });
166
+ });
167
+ const dynamo = await this._tbl_dynamoDb().getInstance();
168
+ const result = await dynamo.send(new client_dynamodb_1.UpdateTableCommand(params));
169
+ if (result?.TableDescription) {
170
+ updateResults.push(result?.TableDescription);
171
+ }
172
+ logging_service_1.LoggingService.log([
173
+ `Creating Index: '${indexName}'`,
174
+ `on table '${tableFullName}' started:`,
175
+ new Date().toTimeString(),
176
+ ].join(" "));
177
+ if (indexCount !== newSecondaryIndexOptions.length) {
178
+ await util_service_1.UtilService.waitUntilMunites(5);
179
+ }
180
+ }
181
+ }
182
+ if (staledIndexNames.length) {
183
+ if (canUpdate) {
184
+ await util_service_1.UtilService.waitUntilMunites(4);
185
+ }
186
+ canUpdate = true;
187
+ let indexCount = 0;
188
+ for (const indexName of staledIndexNames) {
189
+ const params = {
190
+ TableName: tableFullName,
191
+ GlobalSecondaryIndexUpdates: [],
192
+ };
193
+ indexCount++;
194
+ params.GlobalSecondaryIndexUpdates?.push({
195
+ Delete: {
196
+ IndexName: indexName,
197
+ },
198
+ });
199
+ const dynamo = await this._tbl_dynamoDb().getInstance();
200
+ const result = await dynamo.send(new client_dynamodb_1.UpdateTableCommand(params));
201
+ if (result?.TableDescription) {
202
+ updateResults.push(result?.TableDescription);
203
+ }
204
+ logging_service_1.LoggingService.log([
205
+ `Deleting Index: '${indexName}'`,
206
+ `on table '${tableFullName}' started:`,
207
+ new Date().toTimeString(),
208
+ ].join(" "));
209
+ if (indexCount !== staledIndexNames.length) {
210
+ await util_service_1.UtilService.waitUntilMunites(1);
211
+ }
212
+ }
213
+ }
214
+ if (!canUpdate) {
215
+ return null;
216
+ }
217
+ if (updateResults.length) {
218
+ logging_service_1.LoggingService.log({
219
+ "@allCreateGlobalSecondaryIndexBase": "",
220
+ updateResults,
221
+ });
222
+ return updateResults;
223
+ }
224
+ return null;
225
+ }
226
+ catch (error) {
227
+ logging_service_1.LoggingService.log({
228
+ "@allCreateGlobalSecondaryIndexBase": "",
229
+ error: error?.message,
230
+ });
231
+ return null;
232
+ }
233
+ }
234
+ async mocody_createOrUpdateDefinedIndexes() {
235
+ const { secondaryIndexOptions } = this._tbl_getLocalVariables();
236
+ const existingTableInfo = await this.mocody_tbl_getTableInfo();
237
+ if (existingTableInfo) {
238
+ if (secondaryIndexOptions?.length) {
239
+ const result01 = await this._allUpdateGlobalSecondaryIndexBase({
240
+ secondaryIndexOptions,
241
+ existingTableInfo,
242
+ });
243
+ logging_service_1.LoggingService.log(result01);
244
+ return result01;
245
+ }
246
+ }
247
+ logging_service_1.LoggingService.log("Table does not exists");
248
+ return null;
249
+ }
250
+ async mocody_tbl_createTableIfNotExists() {
251
+ const { secondaryIndexOptions } = this._tbl_getLocalVariables();
252
+ const existingTableInfo = await this.mocody_tbl_getTableInfo();
253
+ if (existingTableInfo) {
254
+ if (secondaryIndexOptions?.length) {
255
+ await this._allUpdateGlobalSecondaryIndexBase({
256
+ secondaryIndexOptions,
257
+ existingTableInfo,
258
+ });
259
+ }
260
+ else if (existingTableInfo.GlobalSecondaryIndexes?.length) {
261
+ await this._allUpdateGlobalSecondaryIndexBase({
262
+ secondaryIndexOptions: [],
263
+ existingTableInfo,
264
+ });
265
+ }
266
+ return null;
267
+ }
268
+ return await this.mocody_tbl_createTable();
269
+ }
270
+ _getGlobalSecondaryIndexCreationParams({ secondaryIndexOptions, }) {
271
+ const { tableFullName } = this._tbl_getLocalVariables();
272
+ const params = {
273
+ KeySchema: [],
274
+ AttributeDefinitions: [],
275
+ TableName: tableFullName,
276
+ GlobalSecondaryIndexes: [],
277
+ };
278
+ const attributeDefinitionsNameList = [];
279
+ secondaryIndexOptions.forEach((sIndex) => {
280
+ const { indexName, partitionKeyFieldName, sortKeyFieldName, dataTypes, projectionFieldsInclude } = sIndex;
281
+ const partitionKeyFieldName01 = partitionKeyFieldName;
282
+ const sortKeyFieldName01 = sortKeyFieldName;
283
+ if (!attributeDefinitionsNameList.includes(partitionKeyFieldName01)) {
284
+ attributeDefinitionsNameList.push(partitionKeyFieldName01);
285
+ params?.AttributeDefinitions?.push({
286
+ AttributeName: partitionKeyFieldName01,
287
+ AttributeType: dataTypes.partitionKeyDataType,
288
+ });
289
+ }
290
+ if (!attributeDefinitionsNameList.includes(sortKeyFieldName01)) {
291
+ attributeDefinitionsNameList.push(sortKeyFieldName01);
292
+ params?.AttributeDefinitions?.push({
293
+ AttributeName: sortKeyFieldName01,
294
+ AttributeType: dataTypes.sortKeyDataType,
295
+ });
296
+ }
297
+ let projectionFields = (projectionFieldsInclude || []);
298
+ let projectionType = client_dynamodb_1.ProjectionType.ALL;
299
+ if (projectionFields?.length) {
300
+ projectionFields = projectionFields.filter((field) => {
301
+ return field !== partitionKeyFieldName01 && field !== sortKeyFieldName01;
302
+ });
303
+ if (projectionFields.length === 0) {
304
+ projectionType = client_dynamodb_1.ProjectionType.KEYS_ONLY;
305
+ }
306
+ else {
307
+ projectionType = client_dynamodb_1.ProjectionType.INCLUDE;
308
+ }
309
+ }
310
+ params.GlobalSecondaryIndexes?.push({
311
+ IndexName: indexName,
312
+ Projection: {
313
+ ProjectionType: projectionType,
314
+ NonKeyAttributes: projectionType === client_dynamodb_1.ProjectionType.INCLUDE ? projectionFields : undefined,
315
+ },
316
+ KeySchema: [
317
+ {
318
+ AttributeName: partitionKeyFieldName01,
319
+ KeyType: "HASH",
320
+ },
321
+ {
322
+ AttributeName: sortKeyFieldName01,
323
+ KeyType: "RANGE",
324
+ },
325
+ ],
326
+ });
327
+ });
328
+ return {
329
+ xAttributeDefinitions: params.AttributeDefinitions || [],
330
+ xGlobalSecondaryIndex: params.GlobalSecondaryIndexes || [],
331
+ };
332
+ }
333
+ async mocody_tbl_createTable() {
334
+ const { partitionKeyFieldName, sortKeyFieldName, tableFullName, secondaryIndexOptions } = this._tbl_getLocalVariables();
335
+ const params = {
336
+ AttributeDefinitions: [
337
+ {
338
+ AttributeName: partitionKeyFieldName,
339
+ AttributeType: "S",
340
+ },
341
+ {
342
+ AttributeName: sortKeyFieldName,
343
+ AttributeType: "S",
344
+ },
345
+ ],
346
+ KeySchema: [
347
+ {
348
+ AttributeName: partitionKeyFieldName,
349
+ KeyType: "HASH",
350
+ },
351
+ {
352
+ AttributeName: sortKeyFieldName,
353
+ KeyType: "RANGE",
354
+ },
355
+ ],
356
+ BillingMode: "PAY_PER_REQUEST",
357
+ TableName: tableFullName,
358
+ };
359
+ params.Tags = [
360
+ {
361
+ Key: "tablePrefix",
362
+ Value: tableFullName,
363
+ },
364
+ {
365
+ Key: `DDBTableGroupKey-${tableFullName}`,
366
+ Value: tableFullName,
367
+ },
368
+ ];
369
+ if (secondaryIndexOptions?.length) {
370
+ const creationParams = this._getGlobalSecondaryIndexCreationParams({
371
+ secondaryIndexOptions,
372
+ });
373
+ if (creationParams.xAttributeDefinitions?.length) {
374
+ const existAttrDefNames = params.AttributeDefinitions?.map((def) => def.AttributeName);
375
+ creationParams.xAttributeDefinitions.forEach((def) => {
376
+ const alreadyDefined = existAttrDefNames?.includes(def.AttributeName);
377
+ if (!alreadyDefined) {
378
+ params.AttributeDefinitions?.push(def);
379
+ }
380
+ });
381
+ }
382
+ params.GlobalSecondaryIndexes = [...creationParams.xGlobalSecondaryIndex];
383
+ }
384
+ logging_service_1.LoggingService.log({
385
+ "@allCreateTableBase, table: ": tableFullName,
386
+ });
387
+ const dynamo = await this._tbl_dynamoDb().getInstance();
388
+ const result = await dynamo.send(new client_dynamodb_1.CreateTableCommand(params));
389
+ if (result?.TableDescription) {
390
+ logging_service_1.LoggingService.log([
391
+ `@allCreateTableBase,`,
392
+ `Created table: '${result?.TableDescription.TableName}'`,
393
+ new Date().toTimeString(),
394
+ ].join(" "));
395
+ return result.TableDescription?.TableName;
396
+ }
397
+ return null;
398
+ }
399
+ async mocody_tbl_deleteGlobalSecondaryIndex(indexName) {
400
+ const { tableFullName } = this._tbl_getLocalVariables();
401
+ const params = {
402
+ TableName: tableFullName,
403
+ GlobalSecondaryIndexUpdates: [
404
+ {
405
+ Delete: {
406
+ IndexName: indexName,
407
+ },
408
+ },
409
+ ],
410
+ };
411
+ const dynamo = await this._tbl_dynamoDb().getInstance();
412
+ const result = await dynamo.send(new client_dynamodb_1.UpdateTableCommand(params));
413
+ if (result?.TableDescription) {
414
+ return result.TableDescription?.TableName;
415
+ }
416
+ return null;
417
+ }
418
+ }
419
+ exports.DynamoManageTable = DynamoManageTable;
420
420
  //# sourceMappingURL=dynamo-manage-table.js.map