@izara_project/izara-core-library-dynamodb 1.0.19 → 1.0.20

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@izara_project/izara-core-library-dynamodb",
3
- "version": "1.0.19",
3
+ "version": "1.0.20",
4
4
  "description": "Connecting with AWS DynamoDB Resource",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -16,7 +16,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
  */
17
17
 
18
18
  import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
19
- import { DynamoDB } from '@aws-sdk/client-dynamodb';
19
+ import { DynamoDB, DescribeTableCommand, DynamoDBClient } from '@aws-sdk/client-dynamodb';
20
20
  import { marshall, unmarshall } from '@aws-sdk/util-dynamodb';
21
21
 
22
22
  // External Izara project imports
@@ -33,6 +33,7 @@ const dynamodb = DynamoDBDocument.from(new DynamoDB(), {
33
33
  }
34
34
  });
35
35
 
36
+
36
37
  // Commented imports and alternatives
37
38
  // const izara = require("@izara_project/izara-middleware");
38
39
  // const dynamodb = DynamoDBDocument.from(new DynamoDB());
@@ -2162,39 +2163,44 @@ async function batchDeleteItems(
2162
2163
  keyFieldName
2163
2164
  ) {
2164
2165
  try {
2165
- let size = 25; // maybe should check size < 400 KB
2166
+ const size = 25;
2166
2167
  let chunked = [];
2167
-
2168
2168
  for (let i = 0; i < writeItems.length; i += size) {
2169
2169
  chunked.push(writeItems.slice(i, i + size));
2170
2170
  }
2171
2171
 
2172
- // ---- perform each chuck < 25 ----
2173
2172
  for (let chunkIdx = 0; chunkIdx < chunked.length; chunkIdx++) {
2174
2173
  let chunkSet = chunked[chunkIdx];
2175
- let requestItems = new Array();
2176
- // console.log('chunkSet', chunkSet);
2177
- // check sortKeyFieldName
2174
+ let requestItems = [];
2175
+
2178
2176
  for (let i = 0; i < chunkSet.length; i++) {
2177
+ const item = chunkSet[i];
2178
+ const pKeyName = keyFieldName.partitionKeyFieldName;
2179
+ const sKeyName = keyFieldName.sortKeyFieldName;
2180
+
2181
+ // Construct the primary key object
2182
+ let dynamoKey = {
2183
+ [pKeyName]: item[pKeyName]
2184
+ };
2185
+
2186
+ // 💡 FIX: Only add Sort Key if it exists in the table schema
2187
+ if (sKeyName) {
2188
+ dynamoKey[sKeyName] = item[sKeyName];
2189
+ }
2190
+
2179
2191
  requestItems.push({
2180
2192
  DeleteRequest: {
2181
- Key: {
2182
- [keyFieldName.partitionKeyFieldName]:
2183
- chunkSet[i][keyFieldName.partitionKeyFieldName],
2184
- [keyFieldName.sortKeyFieldName]:
2185
- chunkSet[i][keyFieldName.sortKeyFieldName]
2186
- }
2193
+ Key: dynamoKey
2187
2194
  }
2188
2195
  });
2189
2196
  }
2190
2197
 
2191
2198
  let payload = {
2192
- RequestItems: {
2193
- [tableName]: requestItems
2194
- },
2199
+ RequestItems: { [tableName]: requestItems },
2195
2200
  ReturnConsumedCapacity: 'TOTAL'
2196
2201
  };
2197
- _izContext.logger.debug('payload writeItems... ', payload);
2202
+
2203
+ _izContext.logger.debug('Final Payload for BatchWrite: ', JSON.stringify(payload, null, 2));
2198
2204
 
2199
2205
  const returnValue = await dynamodb.batchWrite(payload);
2200
2206
  _izContext.logger.debug(
@@ -2392,6 +2398,47 @@ async function captureCapacityUsed(
2392
2398
  }
2393
2399
  }
2394
2400
 
2401
+ /**
2402
+ * helper function to get partitionKeys and sortKeys for table
2403
+ * @param {*} _izContext
2404
+ * @param {*} tableName
2405
+ * @returns
2406
+ */
2407
+ async function describeTable(
2408
+ _izContext,
2409
+ payload
2410
+ ) {
2411
+ const region = process.env.iz_region;
2412
+ if (!region) {
2413
+ throw new Error("Environment variable 'iz_region' is missing or undefined.");
2414
+ }
2415
+
2416
+ const dynamoDbClient = new DynamoDBClient({ region });
2417
+
2418
+ if (!payload || !payload.TableName) {
2419
+ throw new Error("Missing required parameter: TableName");
2420
+ }
2421
+
2422
+ try {
2423
+ const result = await dynamoDbClient.send(
2424
+ new DescribeTableCommand({ TableName: payload.TableName })
2425
+ );
2426
+
2427
+ const partitionKey = result.Table.KeySchema.find(k => k.KeyType === 'HASH');
2428
+ const sortKey = result.Table.KeySchema.find(k => k.KeyType === 'RANGE');
2429
+
2430
+ // Return a clean object with field names
2431
+ return {
2432
+ partitionKeyFieldName: partitionKey?.AttributeName,
2433
+ sortKeyFieldName: sortKey?.AttributeName // Might be undefined if table has no Sort Key
2434
+ };
2435
+
2436
+ } catch (error) {
2437
+ console.error(`[describeTable Error] Failed for table: ${payload.TableName}`, error);
2438
+ throw error;
2439
+ }
2440
+ }
2441
+
2395
2442
  // Consolidated exports
2396
2443
  export {
2397
2444
  // Table name functions
@@ -2422,5 +2469,6 @@ export {
2422
2469
  // Helper functions
2423
2470
  QueryElementConditionalCheckStringSetEquals,
2424
2471
  generateNotExistLogicalElements,
2425
- refractorAttributesToComplex
2472
+ refractorAttributesToComplex,
2473
+ describeTable
2426
2474
  };