AWS code-library documentation change
Summary
Updated DynamoDB query examples to include begins_with usage, advanced query techniques (pagination, GSIs, complex filters), and date/time pattern queries. Added structured functions with documentation and error handling.
Security assessment
Changes focus on query functionality improvements and code example enhancements. No security vulnerabilities or security features are addressed. The consistent read example relates to data consistency rather than security controls.
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 527e29f8c..3ef5793de 100644 --- a//code-library/latest/ug/javascript_3_dynamodb_code_examples.md +++ b//code-library/latest/ug/javascript_3_dynamodb_code_examples.md @@ -1074 +1074 @@ The following code example shows how to use `Query`. -**SDK for JavaScript (v3)** + * Use the begins_with function in a key condition expression. @@ -1075,0 +1076 @@ The following code example shows how to use `Query`. + * Filter items based on a prefix pattern in the sort key. @@ -1077 +1077,0 @@ The following code example shows how to use `Query`. -###### Note @@ -1079 +1078,0 @@ The following code example shows how to use `Query`. -There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javascriptv3/example_code/dynamodb#code-examples). @@ -1081 +1079,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru -This example uses the document client to simplify working with items in DynamoDB. For API details see [QueryCommand](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-lib-dynamodb/Class/QueryCommand/). @@ -1082,0 +1081 @@ This example uses the document client to simplify working with items in DynamoDB +**SDK for JavaScript (v3)** @@ -1084,2 +1082,0 @@ This example uses the document client to simplify working with items in DynamoDB - import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; - import { QueryCommand, DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; @@ -1087,2 +1084 @@ This example uses the document client to simplify working with items in DynamoDB - const client = new DynamoDBClient({}); - const docClient = DynamoDBDocumentClient.from(client); +Query a DynamoDB table using a begins_with condition on the sort key with AWS SDK for JavaScript. @@ -1090,11 +1085,0 @@ This example uses the document client to simplify working with items in DynamoDB - export const main = async () => { - const command = new QueryCommand({ - TableName: "CoffeeCrop", - KeyConditionExpression: - "OriginCountry = :originCountry AND RoastDate > :roastDate", - ExpressionAttributeValues: { - ":originCountry": "Ethiopia", - ":roastDate": "2023-05-01", - }, - ConsistentRead: true, - }); @@ -1102,3 +1087,37 @@ This example uses the document client to simplify working with items in DynamoDB - const response = await docClient.send(command); - console.log(response); - return response; + const { DynamoDBClient, QueryCommand } = require("@aws-sdk/client-dynamodb"); + + /** + * Queries a DynamoDB table for items where the sort key begins with a specific prefix + * + * @param {Object} config - AWS SDK configuration object + * @param {string} tableName - The name of the DynamoDB table + * @param {string} partitionKeyName - The name of the partition key + * @param {string} partitionKeyValue - The value of the partition key + * @param {string} sortKeyName - The name of the sort key + * @param {string} prefix - The prefix to match at the beginning of the sort key + * @returns {Promise<Object>} - The query response + */ + async function queryWithBeginsWith( + config, + tableName, + partitionKeyName, + partitionKeyValue, + sortKeyName, + prefix + ) { + try { + // Create DynamoDB client + const client = new DynamoDBClient(config); + + // Construct the query input + const input = { + TableName: tableName, + KeyConditionExpression: "#pk = :pkValue AND begins_with(#sk, :prefix)", + ExpressionAttributeNames: { + "#pk": partitionKeyName, + "#sk": sortKeyName + }, + ExpressionAttributeValues: { + ":pkValue": { S: partitionKeyValue }, + ":prefix": { S: prefix } + } @@ -1106,0 +1126,8 @@ This example uses the document client to simplify working with items in DynamoDB + // Execute the query + const command = new QueryCommand(input); + return await client.send(command); + } catch (error) { + console.error(`Error querying with begins_with: ${error}`); + throw error; + } + } @@ -1109 +1135,0 @@ This example uses the document client to simplify working with items in DynamoDB - * For more information, see [AWS SDK for JavaScript Developer Guide](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/dynamodb-example-query-scan.html#dynamodb-example-table-query-scan-querying). @@ -2014,0 +2041,319 @@ For complete source code and instructions on how to set up and run, see the full +The following code example shows how to perform advanced query operations in DynamoDB. + + * Query tables using various filtering and condition techniques. + + * Implement pagination for large result sets. + + * Use Global Secondary Indexes for alternate access patterns. + + * Apply consistency controls based on application requirements. + + + + +**SDK for JavaScript (v3)** + + +Query with strongly consistent reads using AWS SDK for JavaScript. + + + const { DynamoDBClient, QueryCommand } = require("@aws-sdk/client-dynamodb"); + + /** + * Queries a DynamoDB table with configurable read consistency + * + * @param {Object} config - AWS SDK configuration object + * @param {string} tableName - The name of the DynamoDB table + * @param {string} partitionKeyName - The name of the partition key + * @param {string} partitionKeyValue - The value of the partition key + * @param {boolean} useConsistentRead - Whether to use strongly consistent reads + * @returns {Promise<Object>} - The query response + */ + async function queryWithConsistentRead( + config, + tableName, + partitionKeyName, + partitionKeyValue, + useConsistentRead = false + ) { + try { + // Create DynamoDB client + const client = new DynamoDBClient(config); + + // Construct the query input + const input = { + TableName: tableName, + KeyConditionExpression: "#pk = :pkValue", + ExpressionAttributeNames: { + "#pk": partitionKeyName + }, + ExpressionAttributeValues: { + ":pkValue": { S: partitionKeyValue } + }, + ConsistentRead: useConsistentRead + }; + + // Execute the query + const command = new QueryCommand(input); + return await client.send(command); + } catch (error) { + console.error(`Error querying with consistent read: ${error}`); + throw error; + } + } + + + +Query using a Global Secondary Index with AWS SDK for JavaScript. + + + const { DynamoDBClient, QueryCommand } = require("@aws-sdk/client-dynamodb"); + + /** + * Queries a DynamoDB table using the primary key + * + * @param {Object} config - AWS SDK configuration object + * @param {string} tableName - The name of the DynamoDB table + * @param {string} userId - The user ID to query by (partition key) + * @returns {Promise<Object>} - The query response + */ + async function queryTable( + config, + tableName, + userId + ) { + try { + // Create DynamoDB client + const client = new DynamoDBClient(config); + + // Construct the query input for the base table + const input = { + TableName: tableName, + KeyConditionExpression: "user_id = :userId", + ExpressionAttributeValues: { + ":userId": { S: userId } + } + }; + + // Execute the query + const command = new QueryCommand(input); + return await client.send(command); + } catch (error) { + console.error(`Error querying table: ${error}`); + throw error; + } + } + + /** + * Queries a DynamoDB Global Secondary Index (GSI) + * + * @param {Object} config - AWS SDK configuration object + * @param {string} tableName - The name of the DynamoDB table