AWS Security ChangesHomeSearch

AWS code-library documentation change

Service: code-library · 2025-03-23 · Documentation low

File: code-library/latest/ug/javascript_3_dynamodb_code_examples.md

Summary

Added new PartiQL CRUD operation examples (DELETE, INSERT, SELECT, UPDATE) with comprehensive documentation and example usages

Security assessment

New documentation for PartiQL operations focuses on general database operations rather than security features or vulnerability mitigation

Diff

diff --git a/code-library/latest/ug/javascript_3_dynamodb_code_examples.md b/code-library/latest/ug/javascript_3_dynamodb_code_examples.md
index 9154851db..575356b39 100644
--- a/code-library/latest/ug/javascript_3_dynamodb_code_examples.md
+++ b/code-library/latest/ug/javascript_3_dynamodb_code_examples.md
@@ -811 +811 @@ Describe TTL configuration on an existing DynamoDB table using AWS SDK for JavaS
-    const describeTableTTL = async (tableName, region) => {
+    export const describeTTL = async (tableName, region) => {
@@ -833,2 +833,2 @@ Describe TTL configuration on an existing DynamoDB table using AWS SDK for JavaS
-    // enter table name and change region if desired.
-    describeTableTTL('your-table-name', 'us-east-1');
+    // Example usage (commented out for testing)
+    // describeTTL('your-table-name', 'us-east-1');
@@ -1204 +1204 @@ Enable TTL on an existing DynamoDB table.
-    const enableTTL = async (tableName, ttlAttribute) => {
+    export const enableTTL = async (tableName, ttlAttribute, region = 'us-east-1') => {
@@ -1206 +1206,4 @@ Enable TTL on an existing DynamoDB table.
-        const client = new DynamoDBClient({});
+        const client = new DynamoDBClient({
+            region: region,
+            endpoint: `https://dynamodb.${region}.amazonaws.com`
+        });
@@ -1230,2 +1233,2 @@ Enable TTL on an existing DynamoDB table.
-    // call with your own values
-    enableTTL('ExampleTable', 'exampleTtlAttribute');
+    // Example usage (commented out for testing)
+    // enableTTL('ExampleTable', 'exampleTtlAttribute');
@@ -1239 +1242 @@ Disable TTL on an existing DynamoDB table.
-    const disableTTL = async (tableName, ttlAttribute) => {
+    export const disableTTL = async (tableName, ttlAttribute, region = 'us-east-1') => {
@@ -1241 +1244,4 @@ Disable TTL on an existing DynamoDB table.
-        const client = new DynamoDBClient({});
+        const client = new DynamoDBClient({
+            region: region,
+            endpoint: `https://dynamodb.${region}.amazonaws.com`
+        });
@@ -1265,2 +1271,2 @@ Disable TTL on an existing DynamoDB table.
-    // call with your own values
-    disableTTL('ExampleTable', 'exampleTtlAttribute');
+    // Example usage (commented out for testing)
+    // disableTTL('ExampleTable', 'exampleTtlAttribute');
@@ -1307 +1313 @@ Update TTL on on an existing DynamoDB Item in a table, with a condition.
-    const updateDynamoDBItem = async (tableName, region, partitionKey, sortKey, newAttribute) => {
+    export const updateItemConditional = async (tableName, partitionKey, sortKey, region = 'us-east-1', newAttribute = 'default-value') => {
@@ -1345,2 +1351,2 @@ Update TTL on on an existing DynamoDB Item in a table, with a condition.
-    // Enter your values here
-    updateDynamoDBItem('your-table-name', "us-east-1",'your-partition-key-value', 'your-sort-key-value', 'your-new-attribute-value');
+    // Example usage (commented out for testing)
+    // updateItemConditional('your-table-name', 'your-partition-key-value', 'your-sort-key-value');
@@ -1393 +1399 @@ Create DynamoDB table with warm throughput setting using AWS SDK for JavaScript.
-    async function createDynamoDBTableWithWarmThroughput(
+    export async function createDynamoDBTableWithWarmThroughput(
@@ -1454,0 +1461 @@ Create DynamoDB table with warm throughput setting using AWS SDK for JavaScript.
+        return response;
@@ -1460,0 +1468,14 @@ Create DynamoDB table with warm throughput setting using AWS SDK for JavaScript.
+    // Example usage (commented out for testing)
+    /*
+    createDynamoDBTableWithWarmThroughput(
+      'example-table',
+      'pk',
+      'sk',
+      'gsiKey',
+      'data',
+      10, 10, 5, 5,
+      'example-index',
+      5, 5, 2, 2
+    );
+    */
+    
@@ -1476 +1497 @@ The following code example shows how to create an item with TTL.
-    function createDynamoDBItem(table_name, region, partition_key, sort_key) {
+    export function createDynamoDBItem(table_name, region, partition_key, sort_key) {
@@ -1507 +1528 @@ The following code example shows how to create an item with TTL.
-                console.log("Exception encountered when creating item %s, here's what happened: ", data, ex);
+                console.log("Exception encountered when creating item %s, here's what happened: ", data, err);
@@ -1516,2 +1537,2 @@ The following code example shows how to create an item with TTL.
-    // use your own values
-    createDynamoDBItem('your-table-name', 'us-east-1', 'your-partition-key-value', 'your-sort-key-value');
+    // Example usage (commented out for testing)
+    // createDynamoDBItem('your-table-name', 'us-east-1', 'your-partition-key-value', 'your-sort-key-value');
@@ -1524,0 +1546,447 @@ The following code example shows how to create an item with TTL.
+The following code example shows how to delete data using PartiQL DELETE statements.
+
+**SDK for JavaScript (v3)**
+    
+
+Delete items from a DynamoDB table using PartiQL DELETE statements with AWS SDK for JavaScript.
+    
+    
+    /**
+     * This example demonstrates how to delete items from a DynamoDB table using PartiQL.
+     * It shows different ways to delete documents with various index types.
+     */
+    import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
+    import {
+      DynamoDBDocumentClient,
+      ExecuteStatementCommand,
+      BatchExecuteStatementCommand,
+    } from "@aws-sdk/lib-dynamodb";
+    
+    /**
+     * Delete a single item by its partition key using PartiQL.
+     * 
+     * @param tableName - The name of the DynamoDB table
+     * @param partitionKeyName - The name of the partition key attribute
+     * @param partitionKeyValue - The value of the partition key
+     * @returns The response from the ExecuteStatementCommand
+     */
+    export const deleteItemByPartitionKey = async (
+      tableName: string,
+      partitionKeyName: string,
+      partitionKeyValue: string | number
+    ) => {
+      const client = new DynamoDBClient({});
+      const docClient = DynamoDBDocumentClient.from(client);
+    
+      const params = {
+        Statement: `DELETE FROM "${tableName}" WHERE ${partitionKeyName} = ?`,
+        Parameters: [partitionKeyValue],
+      };
+    
+      try {
+        const data = await docClient.send(new ExecuteStatementCommand(params));
+        console.log("Item deleted successfully");
+        return data;
+      } catch (err) {
+        console.error("Error deleting item:", err);
+        throw err;
+      }
+    };
+    
+    /**
+     * Delete an item by its composite key (partition key + sort key) using PartiQL.
+     * 
+     * @param tableName - The name of the DynamoDB table
+     * @param partitionKeyName - The name of the partition key attribute
+     * @param partitionKeyValue - The value of the partition key
+     * @param sortKeyName - The name of the sort key attribute
+     * @param sortKeyValue - The value of the sort key
+     * @returns The response from the ExecuteStatementCommand
+     */
+    export const deleteItemByCompositeKey = async (
+      tableName: string,
+      partitionKeyName: string,
+      partitionKeyValue: string | number,
+      sortKeyName: string,
+      sortKeyValue: string | number
+    ) => {
+      const client = new DynamoDBClient({});
+      const docClient = DynamoDBDocumentClient.from(client);
+    
+      const params = {
+        Statement: `DELETE FROM "${tableName}" WHERE ${partitionKeyName} = ? AND ${sortKeyName} = ?`,
+        Parameters: [partitionKeyValue, sortKeyValue],
+      };
+    
+      try {
+        const data = await docClient.send(new ExecuteStatementCommand(params));
+        console.log("Item deleted successfully");
+        return data;
+      } catch (err) {
+        console.error("Error deleting item:", err);
+        throw err;
+      }
+    };
+    
+    /**
+     * Delete an item with a condition to ensure the delete only happens if a condition is met.
+     * 
+     * @param tableName - The name of the DynamoDB table
+     * @param partitionKeyName - The name of the partition key attribute
+     * @param partitionKeyValue - The value of the partition key
+     * @param conditionAttribute - The attribute to check in the condition
+     * @param conditionValue - The value to compare against in the condition
+     * @returns The response from the ExecuteStatementCommand
+     */
+    export const deleteItemWithCondition = async (
+      tableName: string,
+      partitionKeyName: string,
+      partitionKeyValue: string | number,
+      conditionAttribute: string,
+      conditionValue: any
+    ) => {
+      const client = new DynamoDBClient({});
+      const docClient = DynamoDBDocumentClient.from(client);
+    
+      const params = {
+        Statement: `DELETE FROM "${tableName}" WHERE ${partitionKeyName} = ? AND ${conditionAttribute} = ?`,
+        Parameters: [partitionKeyValue, conditionValue],
+      };
+    
+      try {
+        const data = await docClient.send(new ExecuteStatementCommand(params));
+        console.log("Item deleted with condition successfully");
+        return data;
+      } catch (err) {
+        console.error("Error deleting item with condition:", err);
+        throw err;
+      }
+    };
+