mocody 0.2.14 → 0.2.17

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