This repository contains examples of using the AWS JavaScript SDK v3 with DynamoDB, focusing on scenarios related to the ConditionExpression
attribute. The examples cover the following operations:
-
putItem: Records will be overwritten by default.
-
putItem with ConditionExpression: Prevents overwrites by specifying a condition.
-
updateItem: Updates a record or inserts it if it doesn't exist.
-
updateItem with ConditionExpression: Avoids insertion if the record doesn't exist by using a condition.
Before running the examples, ensure you have the following prerequisites:
- Node.js installed
- AWS SDK v3 for JavaScript installed
npm install
Make sure to configure your AWS credentials using the AWS CLI or by setting environment variables.
export AWS_ACCESS_KEY_ID=your_access_key_id
export AWS_SECRET_ACCESS_KEY=your_secret_access_key
export AWS_REGION=your_region
The putItem example demonstrates how to add items to DynamoDB, overwriting existing records by default.
In this example, putItemWithCondition, a ConditionExpression
is used to prevent overwriting existing records. The item will only be added if the specified condition is met.
The updateItem example showcases updating existing records in DynamoDB. If the record doesn't exist, a new one will be created.
This example, updateItemWithCondition, demonstrates using a ConditionExpression
with updateItem
to avoid inserting a new record if the one we wanted to update doesn't exist.
Additionally, this example also covers how to write DynamoDB Expressions to access nested objects and expect a `ConditionalCheckFailedException`` to analyze the current values of the record and understand why it failed.
Execute each example by running the corresponding JavaScript file.
node ./samples/putItem.js
node ./samples/putItemWithCondition.js
node ./samples/updateItem.js
node ./samples/updateItemWithCondition.js