@izara_project/izara-core-library-dynamodb 1.0.18 → 1.0.19
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.
- package/package.json +6 -29
- package/src/DynamoDBSharedLib.js +292 -115
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@izara_project/izara-core-library-dynamodb",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.19",
|
|
4
4
|
"description": "Connecting with AWS DynamoDB Resource",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -14,41 +14,18 @@
|
|
|
14
14
|
"license": "AGPL-3.0-or-later",
|
|
15
15
|
"homepage": "https://bitbucket.org/izara-core-libraries/izara-core-library-dynamodb/src/master/README.md",
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"jest": "^30.4.
|
|
17
|
+
"jest": "^30.4.2"
|
|
18
18
|
},
|
|
19
19
|
"jest": {
|
|
20
20
|
"testEnvironment": "node"
|
|
21
21
|
},
|
|
22
22
|
"type": "module",
|
|
23
23
|
"peerDependencies": {
|
|
24
|
-
"@aws-sdk/client-dynamodb": "^3.0.0",
|
|
25
|
-
"@aws-sdk/lib-dynamodb": "^3.0.0",
|
|
26
|
-
"@aws-sdk/util-dynamodb": "^3.0.0",
|
|
27
|
-
"@izara_project/izara-core-library-core": "^1.0.28",
|
|
28
|
-
"lodash": "^4.0.0"
|
|
29
|
-
},
|
|
30
|
-
"peerDependenciesMeta": {
|
|
31
|
-
"@aws-sdk/client-dynamodb": {
|
|
32
|
-
"optional": false
|
|
33
|
-
},
|
|
34
|
-
"@aws-sdk/lib-dynamodb": {
|
|
35
|
-
"optional": false
|
|
36
|
-
},
|
|
37
|
-
"@aws-sdk/util-dynamodb": {
|
|
38
|
-
"optional": false
|
|
39
|
-
},
|
|
40
|
-
"@izara_project/izara-core-library-core": {
|
|
41
|
-
"optional": false
|
|
42
|
-
},
|
|
43
|
-
"lodash": {
|
|
44
|
-
"optional": true
|
|
45
|
-
}
|
|
46
|
-
},
|
|
47
|
-
"dependencies": {
|
|
48
24
|
"@aws-sdk/client-dynamodb": "^3.1045.0",
|
|
49
25
|
"@aws-sdk/lib-dynamodb": "^3.1045.0",
|
|
50
26
|
"@aws-sdk/util-dynamodb": "^3.996.2",
|
|
51
|
-
"@izara_project/izara-core-library-core": "^1.0.
|
|
52
|
-
|
|
53
|
-
}
|
|
27
|
+
"@izara_project/izara-core-library-core": "^1.0.28"
|
|
28
|
+
},
|
|
29
|
+
"peerDependenciesMeta": {},
|
|
30
|
+
"dependencies": {}
|
|
54
31
|
}
|
package/src/DynamoDBSharedLib.js
CHANGED
|
@@ -19,8 +19,6 @@ import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
|
|
|
19
19
|
import { DynamoDB } from '@aws-sdk/client-dynamodb';
|
|
20
20
|
import { marshall, unmarshall } from '@aws-sdk/util-dynamodb';
|
|
21
21
|
|
|
22
|
-
import cloneDeep from 'lodash/cloneDeep';
|
|
23
|
-
|
|
24
22
|
// External Izara project imports
|
|
25
23
|
import { NoRetryError } from '@izara_project/izara-core-library-core';
|
|
26
24
|
// import { getServiceNameWithCache } = require('@izara_project/izara-core-library-service-schemas').serviceConfig;
|
|
@@ -41,13 +39,13 @@ const dynamodb = DynamoDBDocument.from(new DynamoDB(), {
|
|
|
41
39
|
// const dynamodb = require('@izara_project/izara-core-library-external-request').dynamodb
|
|
42
40
|
|
|
43
41
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* @param {
|
|
47
|
-
* @param {string}
|
|
48
|
-
* @
|
|
42
|
+
* Constructs the full DynamoDB table name based on the service tag, environment stage, and table suffix.
|
|
43
|
+
*
|
|
44
|
+
* @param {Object} _izContext - The Izara context object containing logger, correlation IDs, and request details.
|
|
45
|
+
* @param {string} tableName - The base name or suffix of the DynamoDB table.
|
|
46
|
+
* @param {string} [serviceTag=null] - Optional service tag to override the default environment service tag (`process.env.iz_serviceTag`).
|
|
47
|
+
* @returns {string} The fully constructed table name.
|
|
49
48
|
*/
|
|
50
|
-
|
|
51
49
|
function tableName(_izContext, tableName, serviceTag = null) {
|
|
52
50
|
if (!serviceTag) {
|
|
53
51
|
return process.env.iz_serviceTag + process.env.iz_stage + tableName;
|
|
@@ -57,10 +55,12 @@ function tableName(_izContext, tableName, serviceTag = null) {
|
|
|
57
55
|
}
|
|
58
56
|
|
|
59
57
|
/**
|
|
60
|
-
* Creates a
|
|
61
|
-
*
|
|
58
|
+
* Creates a DynamoDB String Set from an array of strings.
|
|
59
|
+
* Used for formatting arrays into a Set structure compatible with DynamoDB DocumentClient.
|
|
62
60
|
*
|
|
63
|
-
* @
|
|
61
|
+
* @param {string[]} stringSetArray - An array of strings to be converted into a Set.
|
|
62
|
+
* @returns {Set<string>} A JavaScript Set containing the string values.
|
|
63
|
+
* @throws {Error} If the Set cannot be created.
|
|
64
64
|
*/
|
|
65
65
|
function arrayToStringSet(stringSetArray) {
|
|
66
66
|
try {
|
|
@@ -77,19 +77,36 @@ function arrayToStringSet(stringSetArray) {
|
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
/**
|
|
80
|
-
*
|
|
81
|
-
*
|
|
80
|
+
* Converts a DynamoDB StringSet object retrieved from Get or Query operations into a standard JavaScript array.
|
|
81
|
+
* Performs a deep clone to ensure the returned array does not mutate the original object.
|
|
82
82
|
*
|
|
83
|
-
* @
|
|
83
|
+
* @param {Object} stringSet - The DynamoDB StringSet object containing a `values` property.
|
|
84
|
+
* @param {string[]} stringSet.values - The array of string values inside the DynamoDB StringSet.
|
|
85
|
+
* @returns {string[]} An array of strings extracted from the StringSet.
|
|
84
86
|
*/
|
|
85
87
|
function stringSetToArray(stringSet) {
|
|
86
|
-
return
|
|
88
|
+
return structuredClone(stringSet.values);
|
|
87
89
|
}
|
|
88
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Removes undefined values from a given attributes object using DynamoDB's marshall and unmarshall utility.
|
|
93
|
+
*
|
|
94
|
+
* @param {Object} attributes - The attributes object to be processed.
|
|
95
|
+
* @returns {Object} A new object with all undefined values removed, compatible with DynamoDB item structure.
|
|
96
|
+
*/
|
|
89
97
|
function removeUndefined(attributes) {
|
|
90
98
|
return unmarshall(marshall(attributes, { removeUndefinedValues: true }));
|
|
91
99
|
}
|
|
92
100
|
|
|
101
|
+
/**
|
|
102
|
+
* Extracts and cleans correlation ID values from the Izara context object.
|
|
103
|
+
* Removes any undefined properties before returning.
|
|
104
|
+
*
|
|
105
|
+
* @param {Object} _izContext - The Izara context object.
|
|
106
|
+
* @param {string} _izContext.awsRequestId - The AWS request ID.
|
|
107
|
+
* @param {Object} _izContext.correlationIds - The correlation IDs manager.
|
|
108
|
+
* @returns {Object} An object containing the extracted `awsRequestId` and `xCorrelationId` properties.
|
|
109
|
+
*/
|
|
93
110
|
function correlationIdValues(_izContext) {
|
|
94
111
|
return removeUndefined({
|
|
95
112
|
awsRequestId: _izContext.awsRequestId,
|
|
@@ -98,6 +115,17 @@ function correlationIdValues(_izContext) {
|
|
|
98
115
|
}
|
|
99
116
|
|
|
100
117
|
// ----- Helper function for reformConditionExpression
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Validates and extracts the value of a specific logical side (e.g., left-hand side or right-hand side)
|
|
121
|
+
* for a DynamoDB condition expression. It checks that only one property format is set per side and prepends
|
|
122
|
+
* appropriate prefixes (e.g., `:` for references).
|
|
123
|
+
*
|
|
124
|
+
* @param {Object} logicalElement - The logical element object containing the condition details.
|
|
125
|
+
* @param {string} prefixSide - The prefix identifying the side to check (e.g., 'lhs', 'rhs', 'betweenStart').
|
|
126
|
+
* @returns {string|number} The validated value or reference string for the expression.
|
|
127
|
+
* @throws {NoRetryError} If the side logic is missing, duplicated, or uses an unsupported reference type.
|
|
128
|
+
*/
|
|
101
129
|
function validateSideHand(logicalElement, prefixSide) {
|
|
102
130
|
// helper function
|
|
103
131
|
// _izContext.logger.debug('logicalElement in prefixSide', prefixSide);
|
|
@@ -146,6 +174,17 @@ function validateSideHand(logicalElement, prefixSide) {
|
|
|
146
174
|
} // end validateSideHand
|
|
147
175
|
|
|
148
176
|
// ----- Helper function for query dynamodb
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Helper function to reform query elements into a DynamoDB ConditionExpression and ExpressionAttributeValues.
|
|
180
|
+
* It processes logical elements recursively to build the condition string and maps additional attributes to expression values.
|
|
181
|
+
*
|
|
182
|
+
* @param {Object} _izContext - The Izara context object.
|
|
183
|
+
* @param {Object} queryElements - The elements specifying the query conditions.
|
|
184
|
+
* @param {Object[]} [queryElements.logicalElements] - Array of logical condition definitions.
|
|
185
|
+
* @param {Object} [queryElements.additionalAttributes] - Key-value pairs to be added to ExpressionAttributeValues.
|
|
186
|
+
* @returns {[string|undefined, Object|null]} An array where the first element is the constructed `ConditionExpression` string, and the second is the `ExpressionAttributeValues` object.
|
|
187
|
+
*/
|
|
149
188
|
function reformConditionExpression(_izContext, queryElements) {
|
|
150
189
|
// _izContext.logger.debug('queryElements', queryElements);
|
|
151
190
|
// ----- add ConditionExpression to payload if queryElements has prop logicalElements
|
|
@@ -176,10 +215,14 @@ function reformConditionExpression(_izContext, queryElements) {
|
|
|
176
215
|
}
|
|
177
216
|
|
|
178
217
|
/**
|
|
179
|
-
*
|
|
180
|
-
*
|
|
218
|
+
* Recursively processes an array of logical elements to construct a DynamoDB ConditionalExpression string.
|
|
219
|
+
* Supports logical comparisons (equals, between, etc.), logical operators (and, or), functions (begins_with, attribute_exists), and nested grouping.
|
|
181
220
|
*
|
|
182
|
-
* @
|
|
221
|
+
* @param {Object} _izContext - The Izara context object containing the logger.
|
|
222
|
+
* @param {Object[]} logicalElements - An array of objects defining the logical conditions and operators.
|
|
223
|
+
* @param {number} level - The current recursion depth level to prevent infinite recursion.
|
|
224
|
+
* @returns {string|null} The constructed conditional expression string, or null if the elements array is empty.
|
|
225
|
+
* @throws {NoRetryError|Error} If validation fails, syntax is incorrect, or maximum recursion depth is exceeded.
|
|
183
226
|
*/
|
|
184
227
|
function processLogicalElements(_izContext, logicalElements, level) {
|
|
185
228
|
/*
|
|
@@ -393,15 +436,17 @@ function processLogicalElements(_izContext, logicalElements, level) {
|
|
|
393
436
|
} // module processLogicalElements
|
|
394
437
|
|
|
395
438
|
/**
|
|
396
|
-
* Executes a GetItem
|
|
397
|
-
* @param {string} tableName
|
|
398
|
-
* @param {object} keyValues
|
|
399
|
-
* @param {object} [queryElements={}] // eg returned only some attributes (ProjectionExpression)
|
|
400
|
-
* @param {object} [settings={}]
|
|
401
|
-
* @param {boolean} [settings.errorOnNoRecordFound=false]
|
|
402
|
-
* @param {boolean} [settings.retryOnErrorNoRecordFound=false]
|
|
439
|
+
* Executes a GetItem operation on DynamoDB to retrieve a single record.
|
|
403
440
|
*
|
|
404
|
-
* @
|
|
441
|
+
* @param {Object} _izContext - The Izara context object containing the logger and tracing info.
|
|
442
|
+
* @param {string} tableName - The name of the DynamoDB table to read from.
|
|
443
|
+
* @param {Object} keyValues - The primary key (partition key and optional sort key) of the item to retrieve.
|
|
444
|
+
* @param {Object} [queryElements={}] - Additional query options (e.g., ProjectionExpression features). Note: GSI/LSI are not supported for getItem.
|
|
445
|
+
* @param {Object} [settings={}] - Additional behavior settings for the operation.
|
|
446
|
+
* @param {boolean} [settings.errorOnNoRecordFound=false] - If true, throws an error when no item is found instead of returning null.
|
|
447
|
+
* @param {boolean} [settings.retryOnErrorNoRecordFound=false] - If true and errorOnNoRecordFound is also true, throws a retryable Error instead of a NoRetryError.
|
|
448
|
+
* @returns {Promise<Object|null>} A promise that resolves to the retrieved item object, or null if no record is found (and errorOnNoRecordFound is false).
|
|
449
|
+
* @throws {Error|NoRetryError} If the database operation fails or if no record is found and errorOnNoRecordFound is true.
|
|
405
450
|
*/
|
|
406
451
|
async function getItem(
|
|
407
452
|
_izContext,
|
|
@@ -476,22 +521,42 @@ async function getItem(
|
|
|
476
521
|
}
|
|
477
522
|
} // end module getItem
|
|
478
523
|
|
|
524
|
+
// /**
|
|
525
|
+
// * Executes a Query on DynamoDB
|
|
526
|
+
// * @param {string} tableName
|
|
527
|
+
// * @param {object} partitionKeyValue - partitionKey
|
|
528
|
+
// * @param {object} [queryElements={}]
|
|
529
|
+
// * @param {object} queryElements.sortKeyCondition - comparisons and between
|
|
530
|
+
// * @param {string[]} queryElements.projectionExpression - return specific attribute, array of string attribute names.
|
|
531
|
+
// * @param {string} queryElements.select - return specific item eg. ALL_ATTRIBUTES | COUNT
|
|
532
|
+
// * @param {number} queryElements.limit - integer of limit item per pagequery
|
|
533
|
+
// * @param {object} queryElements.exclusiveStartKey - partitionKey and sortKey
|
|
534
|
+
// * @param {string} queryElements.indexName - global secondary index name
|
|
535
|
+
// * @param {string} queryElements.descending - ScanIndexForward be false
|
|
536
|
+
// * @param {object} [settings={}]
|
|
537
|
+
// * @param {numeric} [settings.numPagesToRequest=1] // not sure will use? Script will automatically request multiple pages of results
|
|
538
|
+
// *
|
|
539
|
+
// * @returns {object[], object} array of records from .Items in query result, and queryInfo object with properties such as LastEvaluatedKey
|
|
540
|
+
// */
|
|
479
541
|
/**
|
|
480
|
-
* Executes a Query on DynamoDB
|
|
481
|
-
*
|
|
482
|
-
* @param {object} partitionKeyValue - partitionKey
|
|
483
|
-
* @param {object} [queryElements={}]
|
|
484
|
-
* @param {object} queryElements.sortKeyCondition - comparisons and between
|
|
485
|
-
* @param {string[]} queryElements.projectionExpression - return specific attribute, array of string attribute names.
|
|
486
|
-
* @param {string} queryElements.select - return specific item eg. ALL_ATTRIBUTES | COUNT
|
|
487
|
-
* @param {number} queryElements.limit - integer of limit item per pagequery
|
|
488
|
-
* @param {object} queryElements.exclusiveStartKey - partitionKey and sortKey
|
|
489
|
-
* @param {string} queryElements.indexName - global secondary index name
|
|
490
|
-
* @param {string} queryElements.descending - ScanIndexForward be false
|
|
491
|
-
* @param {object} [settings={}]
|
|
492
|
-
* @param {numeric} [settings.numPagesToRequest=1] // not sure will use? Script will automatically request multiple pages of results
|
|
542
|
+
* Executes a Query operation on DynamoDB to retrieve one or multiple records based on partition and sort key conditions.
|
|
543
|
+
* Supports pagination, indexing, and specific attribute projection.
|
|
493
544
|
*
|
|
494
|
-
* @
|
|
545
|
+
* @param {Object} _izContext - The Izara context object containing the logger and tracing info.
|
|
546
|
+
* @param {string} tableName - The name of the DynamoDB table to query.
|
|
547
|
+
* @param {Object} partitionKeyValue - An object containing a single key-value pair representing the partition key.
|
|
548
|
+
* @param {Object} [queryElements={}] - Elements to configure the query such as conditions, limits, and projections.
|
|
549
|
+
* @param {Object} [queryElements.sortKeyCondition] - Condition for the sort key, requiring `name`, `comparison`, and `value` (or `betweenStartValue`/`betweenEndValue`).
|
|
550
|
+
* @param {string[]} [queryElements.projectionExpression] - Array of attribute names to return (cannot be used with `select` unless `select` is 'SPECIFIC_ATTRIBUTES').
|
|
551
|
+
* @param {string} [queryElements.select] - Type of data to return (e.g., 'ALL_ATTRIBUTES', 'COUNT', 'SPECIFIC_ATTRIBUTES').
|
|
552
|
+
* @param {number} [queryElements.limit] - Maximum number of items to evaluate per page query.
|
|
553
|
+
* @param {Object} [queryElements.exclusiveStartKey] - Pagination token corresponding to `LastEvaluatedKey` from a previous query.
|
|
554
|
+
* @param {string} [queryElements.indexName] - The name of a Global Secondary Index (GSI) or Local Secondary Index (LSI) to query.
|
|
555
|
+
* @param {boolean} [queryElements.descending] - If true, reverses the sort order (sets `ScanIndexForward` to false).
|
|
556
|
+
* @param {Object} [settings={}] - Additional execution settings.
|
|
557
|
+
* @param {number} [settings.numPagesToRequest=1] - The maximum number of pages to auto-fetch. If > 1, it will paginate automatically until the limit or data ends.
|
|
558
|
+
* @returns {Promise<Object>} A promise that resolves to the DynamoDB query response object, typically containing `Items`, `Count`, `ScannedCount`, and `LastEvaluatedKey`.
|
|
559
|
+
* @throws {NoRetryError|Error} If query configuration is invalid, limits are exceeded, or the database operation fails.
|
|
495
560
|
*/
|
|
496
561
|
async function query(
|
|
497
562
|
_izContext,
|
|
@@ -792,20 +857,31 @@ async function query(
|
|
|
792
857
|
}
|
|
793
858
|
}
|
|
794
859
|
|
|
860
|
+
// /**
|
|
861
|
+
// * Executes a Query on DynamoDB and get only Items
|
|
862
|
+
// * @param {string} tableName
|
|
863
|
+
// * @param {object} partitionKeyValue - partitionKey
|
|
864
|
+
// * @param {object} [queryElements={}]
|
|
865
|
+
// * @param {object} queryElements.sortKeyCondition - comparisons and between
|
|
866
|
+
// * @param {string[]} queryElements.projectionExpression - return specific attribute, array of string attribute names.
|
|
867
|
+
// * @param {string} queryElements.select - return specific item eg. ALL_ATTRIBUTES | COUNT
|
|
868
|
+
// * @param {number} queryElements.limit - integer of limit item per pagequery
|
|
869
|
+
// * @param {object} queryElements.exclusiveStartKey - partitionKey and sortKey
|
|
870
|
+
// * @param {object} [settings={}]
|
|
871
|
+
// * @param {numeric} [settings.numPagesToRequest=1] // not sure will use? Script will automatically request multiple pages of results
|
|
872
|
+
// *
|
|
873
|
+
// * @returns {object[], object} array of records from .Items in query result, and queryInfo object with properties such as LastEvaluatedKey
|
|
874
|
+
// */
|
|
795
875
|
/**
|
|
796
|
-
* Executes a Query on DynamoDB and
|
|
797
|
-
*
|
|
798
|
-
* @param {object} partitionKeyValue - partitionKey
|
|
799
|
-
* @param {object} [queryElements={}]
|
|
800
|
-
* @param {object} queryElements.sortKeyCondition - comparisons and between
|
|
801
|
-
* @param {string[]} queryElements.projectionExpression - return specific attribute, array of string attribute names.
|
|
802
|
-
* @param {string} queryElements.select - return specific item eg. ALL_ATTRIBUTES | COUNT
|
|
803
|
-
* @param {number} queryElements.limit - integer of limit item per pagequery
|
|
804
|
-
* @param {object} queryElements.exclusiveStartKey - partitionKey and sortKey
|
|
805
|
-
* @param {object} [settings={}]
|
|
806
|
-
* @param {numeric} [settings.numPagesToRequest=1] // not sure will use? Script will automatically request multiple pages of results
|
|
876
|
+
* Executes a Query operation on DynamoDB and returns only the array of `Items`.
|
|
877
|
+
* Acts as a wrapper around the `query` function for convenience when pagination metadata isn't needed.
|
|
807
878
|
*
|
|
808
|
-
* @
|
|
879
|
+
* @param {Object} _izContext - The Izara context object containing the logger.
|
|
880
|
+
* @param {string} tableName - The name of the DynamoDB table to query.
|
|
881
|
+
* @param {Object} partitionKeyValue - An object containing a single key-value pair for the partition key.
|
|
882
|
+
* @param {Object} [queryElements={}] - Elements to configure the query (conditions, limits, projections).
|
|
883
|
+
* @param {Object} [settings={}] - Additional execution settings (e.g., auto-pagination `numPagesToRequest`).
|
|
884
|
+
* @returns {Promise<Object[]>} A promise that resolves to an array of the retrieved item objects.
|
|
809
885
|
*/
|
|
810
886
|
async function queryResults(
|
|
811
887
|
_izContext,
|
|
@@ -824,25 +900,45 @@ async function queryResults(
|
|
|
824
900
|
return queryResults.Items;
|
|
825
901
|
}
|
|
826
902
|
|
|
903
|
+
// /**
|
|
904
|
+
// * Executes a Scan operation on DynamoDB
|
|
905
|
+
// * Scans the entire table or a secondary index with optional filters.
|
|
906
|
+
// * NOTE: Scan operations are expensive and should be used sparingly.
|
|
907
|
+
// *
|
|
908
|
+
// * @param {object} _izContext - Izara context object for logging
|
|
909
|
+
// * @param {string} tableName - Name of the DynamoDB table
|
|
910
|
+
// * @param {object} [queryElements={}] - Optional query parameters
|
|
911
|
+
// * @param {object[]} queryElements.logicalElements - Filter conditions (uses FilterExpression)
|
|
912
|
+
// * @param {object} queryElements.additionalAttributes - Additional attribute values for filters
|
|
913
|
+
// * @param {string[]} queryElements.projectionExpression - Array of attribute names to return
|
|
914
|
+
// * @param {string} queryElements.select - Return mode: ALL_ATTRIBUTES | COUNT | SPECIFIC_ATTRIBUTES
|
|
915
|
+
// * @param {number} queryElements.limit - Maximum items per page
|
|
916
|
+
// * @param {object} queryElements.exclusiveStartKey - Pagination start key
|
|
917
|
+
// * @param {string} queryElements.indexName - Global secondary index name
|
|
918
|
+
// * @param {object} [settings={}] - Operation settings
|
|
919
|
+
// * @param {number} [settings.numPagesToRequest=1] - Number of pages to auto-paginate (default: 1)
|
|
920
|
+
// *
|
|
921
|
+
// * @returns {Promise<object>} Object containing Items, Count, ScannedCount, and LastEvaluatedKey
|
|
922
|
+
// */
|
|
827
923
|
/**
|
|
828
|
-
* Executes a Scan operation on DynamoDB
|
|
829
|
-
*
|
|
830
|
-
* NOTE: Scan operations are expensive and should be used sparingly.
|
|
831
|
-
*
|
|
832
|
-
* @param {object} _izContext - Izara context object for logging
|
|
833
|
-
* @param {string} tableName - Name of the DynamoDB table
|
|
834
|
-
* @param {object} [queryElements={}] - Optional query parameters
|
|
835
|
-
* @param {object[]} queryElements.logicalElements - Filter conditions (uses FilterExpression)
|
|
836
|
-
* @param {object} queryElements.additionalAttributes - Additional attribute values for filters
|
|
837
|
-
* @param {string[]} queryElements.projectionExpression - Array of attribute names to return
|
|
838
|
-
* @param {string} queryElements.select - Return mode: ALL_ATTRIBUTES | COUNT | SPECIFIC_ATTRIBUTES
|
|
839
|
-
* @param {number} queryElements.limit - Maximum items per page
|
|
840
|
-
* @param {object} queryElements.exclusiveStartKey - Pagination start key
|
|
841
|
-
* @param {string} queryElements.indexName - Global secondary index name
|
|
842
|
-
* @param {object} [settings={}] - Operation settings
|
|
843
|
-
* @param {number} [settings.numPagesToRequest=1] - Number of pages to auto-paginate (default: 1)
|
|
924
|
+
* Executes a Scan operation on DynamoDB to evaluate all items in a table or secondary index.
|
|
925
|
+
* Allows applying optional filters using `logicalElements`.
|
|
926
|
+
* NOTE: Scan operations are computationally expensive and should be used sparingly.
|
|
844
927
|
*
|
|
845
|
-
* @
|
|
928
|
+
* @param {Object} _izContext - The Izara context object containing the logger.
|
|
929
|
+
* @param {string} tableName - The name of the DynamoDB table to scan.
|
|
930
|
+
* @param {Object} [queryElements={}] - Optional query parameters for filtering, limiting, and projecting.
|
|
931
|
+
* @param {Object[]} [queryElements.logicalElements] - Filter conditions mapped to `FilterExpression`.
|
|
932
|
+
* @param {Object} [queryElements.additionalAttributes] - Additional attribute values required for the filter conditions.
|
|
933
|
+
* @param {string[]} [queryElements.projectionExpression] - Array of specific attribute names to return.
|
|
934
|
+
* @param {string} [queryElements.select] - Type of data to return (e.g., 'ALL_ATTRIBUTES', 'COUNT').
|
|
935
|
+
* @param {number} [queryElements.limit] - Maximum number of items to evaluate per page.
|
|
936
|
+
* @param {Object} [queryElements.exclusiveStartKey] - Pagination token (`LastEvaluatedKey`) to resume scanning.
|
|
937
|
+
* @param {string} [queryElements.indexName] - Name of a Global Secondary Index (GSI) to scan.
|
|
938
|
+
* @param {Object} [settings={}] - Additional operation settings.
|
|
939
|
+
* @param {number} [settings.numPagesToRequest=1] - Number of pages to auto-paginate before returning.
|
|
940
|
+
* @returns {Promise<Object>} A promise resolving to an object containing `Items`, `Count`, `ScannedCount`, and `LastEvaluatedKey`.
|
|
941
|
+
* @throws {NoRetryError|Error} If configuration is invalid or execution fails.
|
|
846
942
|
*/
|
|
847
943
|
async function scan(_izContext, tableName, queryElements = {}, settings = {}) {
|
|
848
944
|
try {
|
|
@@ -1008,14 +1104,14 @@ async function scan(_izContext, tableName, queryElements = {}, settings = {}) {
|
|
|
1008
1104
|
}
|
|
1009
1105
|
|
|
1010
1106
|
/**
|
|
1011
|
-
* Executes a Scan on DynamoDB and returns only Items
|
|
1012
|
-
*
|
|
1013
|
-
* @param {string} tableName - Name of the DynamoDB table
|
|
1014
|
-
* @param {object} [queryElements={}] - Optional query parameters
|
|
1015
|
-
* @param {object} [settings={}] - Operation settings
|
|
1016
|
-
* @param {number} [settings.numPagesToRequest=1] - Number of pages to request
|
|
1107
|
+
* Executes a Scan operation on DynamoDB and returns only the array of `Items`.
|
|
1108
|
+
* Acts as a wrapper around the `scan` function for convenience when pagination metadata isn't needed.
|
|
1017
1109
|
*
|
|
1018
|
-
* @
|
|
1110
|
+
* @param {Object} _izContext - The Izara context object containing the logger.
|
|
1111
|
+
* @param {string} tableName - The name of the DynamoDB table to scan.
|
|
1112
|
+
* @param {Object} [queryElements={}] - Optional parameters for filtering and projection.
|
|
1113
|
+
* @param {Object} [settings={}] - Operation settings, including auto-pagination rules.
|
|
1114
|
+
* @returns {Promise<Object[]>} A promise resolving to an array of the retrieved item objects.
|
|
1019
1115
|
*/
|
|
1020
1116
|
async function scanResults(
|
|
1021
1117
|
_izContext,
|
|
@@ -1033,22 +1129,24 @@ async function scanResults(
|
|
|
1033
1129
|
}
|
|
1034
1130
|
|
|
1035
1131
|
/**
|
|
1036
|
-
* Executes a PutItem
|
|
1037
|
-
*
|
|
1038
|
-
* @param {object} attributes
|
|
1039
|
-
* @param {object} [queryElements={}]
|
|
1040
|
-
* @param {object} queryElements.additionalAttributes //attributes needed for the queery but not saved to the Item, eg used in conditional expressions
|
|
1041
|
-
* @param {object[]} queryElements.logicalElements
|
|
1042
|
-
* @param {string} queryElements.returnValues
|
|
1043
|
-
* @param {object} [settings={}]
|
|
1044
|
-
* @param {boolean} settings.errorOnConditionalExpNotPass=false] // if set then an error will be thrown, otherwise function returns with no error
|
|
1045
|
-
* @param {boolean} [settings.retryOnErrorConditionalExpNotPass=false]
|
|
1046
|
-
* @param {boolean} [settings.complexAttributes=false] // default FALSE
|
|
1047
|
-
* @param {boolean} [settings.returnQuery=false] // default FALSE
|
|
1132
|
+
* Executes a PutItem operation on DynamoDB to create or replace an item.
|
|
1133
|
+
* Supports basic and complex attribute formatting, string sets, and conditional expressions.
|
|
1048
1134
|
*
|
|
1049
|
-
* @
|
|
1135
|
+
* @param {Object} _izContext - The Izara context object containing the logger.
|
|
1136
|
+
* @param {string} tableName - The name of the DynamoDB table.
|
|
1137
|
+
* @param {Object|Array} attributes - The item attributes to store. If `settings.complexAttributes` is true, this must be an array of objects describing actions and values.
|
|
1138
|
+
* @param {Object} [queryElements={}] - Additional query options (e.g., conditional expressions).
|
|
1139
|
+
* @param {Object} [queryElements.additionalAttributes] - Attributes needed for the query but not saved to the Item (used in conditional expressions).
|
|
1140
|
+
* @param {Object[]} [queryElements.logicalElements] - Array of logical elements mapped to `ConditionExpression`.
|
|
1141
|
+
* @param {string} [queryElements.returnValues] - Values to return (e.g., 'NONE', 'ALL_OLD').
|
|
1142
|
+
* @param {Object} [settings={}] - Additional behavior settings for the operation.
|
|
1143
|
+
* @param {boolean} [settings.errorOnConditionalExpNotPass=false] - If true, throws an error when a conditional expression fails.
|
|
1144
|
+
* @param {boolean} [settings.retryOnErrorConditionalExpNotPass=false] - If true and a condition fails, throws a retryable Error instead of NoRetryError.
|
|
1145
|
+
* @param {boolean} [settings.complexAttributes=false] - If true, processes `attributes` as an array of complex directives (e.g., handles string sets automatically).
|
|
1146
|
+
* @param {boolean} [settings.returnQuery=false] - If true, returns the payload immediately without sending it to DynamoDB.
|
|
1147
|
+
* @returns {Promise<Object|void>} A promise resolving to the return values (if `ReturnValues` != 'NONE'), the payload (if `returnQuery` is true), or void.
|
|
1148
|
+
* @throws {NoRetryError|Error} If attributes are invalid, a conditional expression fails, or a database error occurs.
|
|
1050
1149
|
*/
|
|
1051
|
-
|
|
1052
1150
|
async function putItem(
|
|
1053
1151
|
_izContext,
|
|
1054
1152
|
tableName,
|
|
@@ -1677,18 +1775,22 @@ async function updateItem(
|
|
|
1677
1775
|
}
|
|
1678
1776
|
|
|
1679
1777
|
/**
|
|
1680
|
-
* Executes a DeleteItem
|
|
1681
|
-
*
|
|
1682
|
-
* @param {object} keyValues
|
|
1683
|
-
* @param {[object]} queryElements
|
|
1684
|
-
* @param {object} [queryElements.additionalAttributes]
|
|
1685
|
-
* @param {object} [queryElements.logicalElements]
|
|
1686
|
-
* @param {object} [queryElements.returnValues]
|
|
1687
|
-
* @param {object} settings
|
|
1688
|
-
* @param {numeric} [settings.errorOnConditionalExpNotPass=false] // if set then an error will be thrown, otherwise function returns with no error
|
|
1689
|
-
* @param {numeric} [settings.retryOnErrorConditionalExpNotPass=false]
|
|
1778
|
+
* Executes a DeleteItem operation on DynamoDB.
|
|
1779
|
+
* Supports conditional deletions and returning the old item's attributes.
|
|
1690
1780
|
*
|
|
1691
|
-
* @
|
|
1781
|
+
* @param {Object} _izContext - The Izara context object containing the logger.
|
|
1782
|
+
* @param {string} tableName - The name of the DynamoDB table.
|
|
1783
|
+
* @param {Object} keyValues - The primary key (partition key and optional sort key) of the item to delete.
|
|
1784
|
+
* @param {Object} [queryElements={}] - Additional query options (e.g., conditional expressions).
|
|
1785
|
+
* @param {Object} [queryElements.additionalAttributes] - Attributes needed for the conditional expression but not saved to the Item.
|
|
1786
|
+
* @param {Object[]} [queryElements.logicalElements] - Array of logical elements mapped to `ConditionExpression`.
|
|
1787
|
+
* @param {string} [queryElements.returnValues] - Values to return (e.g., 'NONE', 'ALL_OLD').
|
|
1788
|
+
* @param {Object} [settings={}] - Additional behavior settings.
|
|
1789
|
+
* @param {boolean} [settings.errorOnConditionalExpNotPass=false] - If true, throws an error when a conditional expression fails.
|
|
1790
|
+
* @param {boolean} [settings.retryOnErrorConditionalExpNotPass=false] - If true and a condition fails, throws a retryable Error instead of NoRetryError.
|
|
1791
|
+
* @param {boolean} [settings.returnQuery=false] - If true, returns the payload immediately without sending it to DynamoDB.
|
|
1792
|
+
* @returns {Promise<Object|void>} A promise resolving to the return values (if `ReturnValues` != 'NONE'), the payload (if `returnQuery` is true), or void.
|
|
1793
|
+
* @throws {NoRetryError|Error} If a conditional expression fails, or a database error occurs.
|
|
1692
1794
|
*/
|
|
1693
1795
|
async function deleteItem(
|
|
1694
1796
|
_izContext,
|
|
@@ -1777,6 +1879,17 @@ async function deleteItem(
|
|
|
1777
1879
|
|
|
1778
1880
|
// ----------- helper function -----------
|
|
1779
1881
|
|
|
1882
|
+
/**
|
|
1883
|
+
* Helper function to generate logical elements and additional attributes for a ConditionExpression
|
|
1884
|
+
* that checks if a string set exists and matches the old record's string set.
|
|
1885
|
+
*
|
|
1886
|
+
* @param {Object} _izContext - The Izara context object containing the logger.
|
|
1887
|
+
* @param {Object} oldRecord - The existing DynamoDB item used to compare against.
|
|
1888
|
+
* @param {string} stringSetAttName - The attribute name of the string set.
|
|
1889
|
+
* @param {Object} [additionalAttributes={}] - Existing additional attributes object to append to.
|
|
1890
|
+
* @param {Object[]} [logicalElements=[]] - Existing logical elements array to append to.
|
|
1891
|
+
* @returns {[Object, Object[]]} An array containing the updated `additionalAttributes` and `logicalElements`.
|
|
1892
|
+
*/
|
|
1780
1893
|
function QueryElementConditionalCheckStringSetEquals(
|
|
1781
1894
|
_izContext,
|
|
1782
1895
|
oldRecord, // filterMain
|
|
@@ -1838,7 +1951,15 @@ function QueryElementConditionalCheckStringSetEquals(
|
|
|
1838
1951
|
} // end QueryElementConditionalCheckStringSetEquals
|
|
1839
1952
|
|
|
1840
1953
|
//---- Helper fucntion -----
|
|
1841
|
-
|
|
1954
|
+
|
|
1955
|
+
/**
|
|
1956
|
+
* Helper function to generate an array of logical elements asserting that specified attributes
|
|
1957
|
+
* either exist or do not exist in the DynamoDB table.
|
|
1958
|
+
*
|
|
1959
|
+
* @param {Object} attributes - An object containing the attribute keys to check.
|
|
1960
|
+
* @param {boolean} [existed=false] - If true, asserts `attribute_exists`; otherwise asserts `attribute_not_exists`.
|
|
1961
|
+
* @returns {Object[]} An array of logical elements combined with 'AND' operators.
|
|
1962
|
+
*/
|
|
1842
1963
|
function generateNotExistLogicalElements(attributes, existed = false) {
|
|
1843
1964
|
if (!existed) {
|
|
1844
1965
|
existed = 'attribute_not_exists';
|
|
@@ -1864,6 +1985,14 @@ function generateNotExistLogicalElements(attributes, existed = false) {
|
|
|
1864
1985
|
return logicalElements;
|
|
1865
1986
|
}
|
|
1866
1987
|
|
|
1988
|
+
/**
|
|
1989
|
+
* Helper function to refactor simple key-value attributes into an array of complex attribute directives
|
|
1990
|
+
* (specifically enforcing the 'SET' action) for `updateItem` or `putItem`.
|
|
1991
|
+
*
|
|
1992
|
+
* @param {Object} attributes - A simple key-value map of attributes to update.
|
|
1993
|
+
* @param {boolean} [forPutQuery=false] - If true, refrains from enforcing the `action: 'set'` property required by updates.
|
|
1994
|
+
* @returns {Object[]} An array of attribute directive objects.
|
|
1995
|
+
*/
|
|
1867
1996
|
function refractorAttributesToComplex(attributes, forPutQuery = false) {
|
|
1868
1997
|
// only for SET update
|
|
1869
1998
|
let returnAttributes = new Array();
|
|
@@ -2006,7 +2135,6 @@ async function transactWriteItems(_izContext, writeItems) {
|
|
|
2006
2135
|
}
|
|
2007
2136
|
|
|
2008
2137
|
// this.transactWriteItems()
|
|
2009
|
-
|
|
2010
2138
|
/* NOTE for batchWriteItems cannot perform multiple operations. you cannot put and delete the same item in the same BatchWriteItem request.
|
|
2011
2139
|
Item not exceed more than 25 requests in the batch, and not batch exceeds 400 KB.The total request size exceeds 16 MB
|
|
2012
2140
|
action: PutItem | DeleteItem
|
|
@@ -2014,6 +2142,19 @@ async function transactWriteItems(_izContext, writeItems) {
|
|
|
2014
2142
|
REF: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#batchWriteItem-property
|
|
2015
2143
|
*/
|
|
2016
2144
|
|
|
2145
|
+
/**
|
|
2146
|
+
* Executes a batch of delete operations on DynamoDB, automatically chunking requests
|
|
2147
|
+
* into groups of 25 (the maximum allowed by AWS DynamoDB batchWriteItem).
|
|
2148
|
+
*
|
|
2149
|
+
* @param {Object} _izContext - The Izara context object containing the logger and tracing info.
|
|
2150
|
+
* @param {string} tableName - The target DynamoDB table name.
|
|
2151
|
+
* @param {Object[]} writeItems - An array of items to delete.
|
|
2152
|
+
* @param {Object} keyFieldName - Configuration specifying the partition and sort key names.
|
|
2153
|
+
* @param {string} keyFieldName.partitionKeyFieldName - The partition key property name in `writeItems`.
|
|
2154
|
+
* @param {string} [keyFieldName.sortKeyFieldName] - The sort key property name in `writeItems`.
|
|
2155
|
+
* @returns {Promise<void>} A promise that resolves when all batch operations complete successfully.
|
|
2156
|
+
* @throws {Error|string} If the batch deletion process fails.
|
|
2157
|
+
*/
|
|
2017
2158
|
async function batchDeleteItems(
|
|
2018
2159
|
_izContext,
|
|
2019
2160
|
tableName,
|
|
@@ -2073,9 +2214,44 @@ async function batchDeleteItems(
|
|
|
2073
2214
|
_izContext.logger.info('----- finish all batchDeleteItems -----');
|
|
2074
2215
|
}
|
|
2075
2216
|
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2217
|
+
/**
|
|
2218
|
+
* Executes a batch of put operations to DynamoDB, automatically chunking requests
|
|
2219
|
+
* into groups of 25 (the maximum allowed by AWS DynamoDB batchWriteItem).
|
|
2220
|
+
*
|
|
2221
|
+
* @see https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#batchWriteItem-property
|
|
2222
|
+
*
|
|
2223
|
+
* @description
|
|
2224
|
+
* **Note:** Cannot use complex settings inside PutRequest.
|
|
2225
|
+
*
|
|
2226
|
+
* After completing the DynamoDB query, this function collects capacity units via
|
|
2227
|
+
* `captureCapacityUsed` and stores them in `_izContext.resourceUse`.
|
|
2228
|
+
* Middleware will capture this and send it to the ResourceUse Service later.
|
|
2229
|
+
*
|
|
2230
|
+
* @param {Object} _izContext - The application context object, containing logger and resource tracking.
|
|
2231
|
+
* @param {string} tableNameData - The target DynamoDB table name (or data used to resolve it).
|
|
2232
|
+
* @param {Object[]} writeItems - An array of item objects to be written to the database.
|
|
2233
|
+
* @param {Object} writeItems[].attributes - The core attributes/fields to insert into DynamoDB.
|
|
2234
|
+
* @param {Object} [writeItems[].queryElements] - Optional query elements for validation or processing.
|
|
2235
|
+
* @param {Object} [writeItems[].settings] - Optional item-specific settings (complex settings will be ignored).
|
|
2236
|
+
* @param {Object} [settings] - Settings for the overall batchWriteItem command (e.g., ReturnConsumedCapacity).
|
|
2237
|
+
*
|
|
2238
|
+
* @throws {Error} Throws an error if the batch put process fails.
|
|
2239
|
+
* @returns {Promise<void>} Resolves when all items are successfully written to DynamoDB.
|
|
2240
|
+
*
|
|
2241
|
+
* @example
|
|
2242
|
+
* const items = [
|
|
2243
|
+
* {
|
|
2244
|
+
* attributes: {
|
|
2245
|
+
* configKey: 'configKey_1',
|
|
2246
|
+
* configTag: 'configTag_2',
|
|
2247
|
+
* configValue: 'configValue_3'
|
|
2248
|
+
* },
|
|
2249
|
+
* queryElements: {},
|
|
2250
|
+
* settings: {}
|
|
2251
|
+
* }
|
|
2252
|
+
* ];
|
|
2253
|
+
* await batchPutItems(_izContext, 'MyTable', items, { ReturnConsumedCapacity: 'TOTAL' });
|
|
2254
|
+
*/
|
|
2079
2255
|
async function batchPutItems(
|
|
2080
2256
|
_izContext,
|
|
2081
2257
|
tableNameData,
|
|
@@ -2154,20 +2330,21 @@ async function batchPutItems(
|
|
|
2154
2330
|
);
|
|
2155
2331
|
} // end loop
|
|
2156
2332
|
} catch (err) {
|
|
2157
|
-
throw ('Error:
|
|
2333
|
+
throw ('Error:batchPutItems', err);
|
|
2158
2334
|
}
|
|
2159
2335
|
}
|
|
2160
2336
|
|
|
2161
2337
|
/**
|
|
2338
|
+
* Captures and accumulates DynamoDB consumed capacity metrics after a query operation.
|
|
2339
|
+
* The collected metrics are stored in `_izContext.resourceUse` for later reporting by middleware.
|
|
2340
|
+
* Supports both single objects and arrays of capacity records (e.g., from `transactWrite`).
|
|
2162
2341
|
*
|
|
2163
|
-
* @param {
|
|
2164
|
-
* @param {
|
|
2165
|
-
* @param {string} capacityStatus
|
|
2166
|
-
* @param {string} queryOperation
|
|
2167
|
-
*
|
|
2168
|
-
*
|
|
2169
|
-
* This function will collect capacity units and store in _izContext.resourceUse
|
|
2170
|
-
* Middleware will capture resourceUse and send to ResourceUse Service later
|
|
2342
|
+
* @param {Object} _izContext - The Izara context object containing the resource tracker (`resourceUse`).
|
|
2343
|
+
* @param {Object|Object[]} capacityUsed - The `ConsumedCapacity` object or array of objects returned by DynamoDB.
|
|
2344
|
+
* @param {string} capacityStatus - The category of the operation capacity ('read' or 'write').
|
|
2345
|
+
* @param {string} queryOperation - The specific DynamoDB operation performed (e.g., 'get', 'query', 'delete', 'put', 'transactionWrite', 'scan').
|
|
2346
|
+
* @returns {Promise<void>} Resolves when the capacity tracking completes.
|
|
2347
|
+
* @throws {NoRetryError} If `capacityStatus` is not 'read' or 'write'.
|
|
2171
2348
|
*/
|
|
2172
2349
|
async function captureCapacityUsed(
|
|
2173
2350
|
_izContext,
|