How to Use AWS SDK v3 for DynamoDB: Syntax and Example
To use the AWS SDK v3 for DynamoDB, import the required client and command classes from
@aws-sdk/client-dynamodb, create a DynamoDB client instance, and then call commands like PutItemCommand or GetItemCommand with parameters. The SDK uses a modular approach, so you only import what you need, making your code efficient and easy to manage.Syntax
The AWS SDK v3 for DynamoDB uses a modular import system. You first import the DynamoDBClient to create a client instance. Then, you import specific command classes like PutItemCommand or GetItemCommand to perform operations. You create a command with parameters and send it using the client.
- DynamoDBClient: The main client to connect to DynamoDB.
- Command classes: Represent specific operations (e.g.,
PutItemCommandto add data). - send(): Method on the client to execute commands.
javascript
import { DynamoDBClient, PutItemCommand, GetItemCommand } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({ region: "us-east-1" }); const putParams = { TableName: "YourTableName", Item: { "PrimaryKey": { S: "key1" }, "Attribute": { S: "value1" } } }; const putCommand = new PutItemCommand(putParams); const getParams = { TableName: "YourTableName", Key: { "PrimaryKey": { S: "key1" } } }; const getCommand = new GetItemCommand(getParams); // To add an item await client.send(putCommand); // To get an item const data = await client.send(getCommand); console.log(data.Item);
Example
This example shows how to add an item to a DynamoDB table and then retrieve it using the AWS SDK v3 for DynamoDB. It demonstrates creating the client, preparing commands, sending them, and handling the response.
javascript
import { DynamoDBClient, PutItemCommand, GetItemCommand } from "@aws-sdk/client-dynamodb"; async function runExample() { const client = new DynamoDBClient({ region: "us-east-1" }); const putParams = { TableName: "TestTable", Item: { "Id": { S: "123" }, "Name": { S: "Alice" } } }; const putCommand = new PutItemCommand(putParams); await client.send(putCommand); const getParams = { TableName: "TestTable", Key: { "Id": { S: "123" } } }; const getCommand = new GetItemCommand(getParams); const data = await client.send(getCommand); console.log("Retrieved item:", data.Item); } runExample().catch(console.error);
Output
Retrieved item: { Id: { S: '123' }, Name: { S: 'Alice' } }
Common Pitfalls
Common mistakes when using AWS SDK v3 for DynamoDB include:
- Not importing the correct command classes, leading to runtime errors.
- Forgetting to await the
send()method, causing unexpected behavior. - Incorrectly formatting the item attributes; DynamoDB expects data types like
Sfor strings,Nfor numbers. - Not specifying the AWS region in the client configuration.
Always check your parameters and use await with asynchronous calls.
javascript
/* Wrong: Missing await and wrong attribute format */ const putParamsWrong = { TableName: "TestTable", Item: { "Id": "123", // Missing { S: "123" } "Name": "Alice" } }; const putCommandWrong = new PutItemCommand(putParamsWrong); await client.send(putCommandWrong); // Added await /* Right: Correct attribute format and await */ const putParamsRight = { TableName: "TestTable", Item: { "Id": { S: "123" }, "Name": { S: "Alice" } } }; const putCommandRight = new PutItemCommand(putParamsRight); await client.send(putCommandRight);
Quick Reference
| Concept | Description | Example |
|---|---|---|
| DynamoDBClient | Creates a client to connect to DynamoDB | const client = new DynamoDBClient({ region: "us-east-1" }); |
| PutItemCommand | Command to add an item to a table | new PutItemCommand(params) |
| GetItemCommand | Command to get an item by key | new GetItemCommand(params) |
| send() | Method to execute a command | await client.send(command) |
| Item format | Attributes must specify data types | {"Id": { S: "123" }} |
Key Takeaways
Import only needed clients and commands from @aws-sdk/client-dynamodb for efficient code.
Always create a DynamoDBClient with the correct AWS region before sending commands.
Use command classes like PutItemCommand and GetItemCommand with properly formatted parameters.
Remember to await the client.send() method to handle asynchronous operations correctly.
Ensure item attributes specify DynamoDB data types like S for string and N for number.