DynamoDB: Create if Not Exists (Guide w/ Code Examples)
Provided by Rafal Wilinski
You can use attribute_not_exists
method with the ConditionExpression
parameter to create an item only if it doesn't already exist. Here's an example of how to use it with the put
operation.
const AWS = require('aws-sdk'); const dynamoDb = new AWS.DynamoDB.DocumentClient(); const tableName = 'myTable'; const item = { id: '123', name: 'John' }; const params = { TableName: tableName, Item: item, ConditionExpression: 'attribute_not_exists(id)', }; dynamoDb.put(params).promise() .then(() => { console.log(Item ${item.id} created successfully ); }) .catch(err => { if (err.code === 'ConditionalCheckFailedException') { console.log(Item ${item.id} already exists ); } else { console.error(err); } });
Similar Code Examples
- DynamoDB BatchWriteItem in Typescript
- DynamoDB: Increment Counter
- DynamoDB: Query Greater Than
- DynamoDB: Bulk Insert
- DynamoDB Get in Typescript
- DynamoDB: Query Ends With
- DynamoDB: Query Items
- DynamoDB: Query Group By
- DynamoDB: Not Between
- DynamoDB: Get Last 10 Records
- DynamoDB: Delete All Items With Partition Key
- DynamoDB: Get List Of Items
- DynamoDB: Delete Table
- DynamoDB: Get Query
- DynamoDB: Like
Spend less time in the AWS console, use Dynobase.
First 7 days are. No credit card needed.
Product Features
DynamoDB Tools
DynamoDB Info
© 2024 Dynobase