How to Use DynamoDB with Node.js: Simple Guide
To use
DynamoDB with Node.js, install the AWS SDK, create a DynamoDB client, and use its methods like putItem or getItem to interact with your database. The AWS SDK provides easy-to-use functions to read and write data asynchronously in your Node.js app.Syntax
First, import the AWS SDK and create a DynamoDB client. Then use methods like putItem to add data or getItem to retrieve data. Each method requires parameters such as the table name and item details.
- AWS SDK import: Loads the DynamoDB client.
- DynamoDB client: Connects your app to DynamoDB.
- putItem: Adds or updates an item in a table.
- getItem: Fetches an item by its key.
javascript
const { DynamoDBClient, PutItemCommand, GetItemCommand } = require("@aws-sdk/client-dynamodb"); const client = new DynamoDBClient({ region: "us-east-1" }); // To add an item const putParams = { TableName: "YourTableName", Item: { "PrimaryKey": { S: "key1" }, "Attribute": { S: "value" } } }; const putCommand = new PutItemCommand(putParams); // To get an item const getParams = { TableName: "YourTableName", Key: { "PrimaryKey": { S: "key1" } } }; const getCommand = new GetItemCommand(getParams);
Example
This example shows how to add an item to a DynamoDB table and then retrieve it using Node.js. It demonstrates connecting to DynamoDB, putting an item, and getting it back.
javascript
const { DynamoDBClient, PutItemCommand, GetItemCommand } = require("@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" } } }; try { // Add item await client.send(new PutItemCommand(putParams)); console.log("Item added successfully."); const getParams = { TableName: "TestTable", Key: { "Id": { S: "123" } } }; // Get item const data = await client.send(new GetItemCommand(getParams)); console.log("Retrieved item:", data.Item); } catch (err) { console.error("Error:", err); } } runExample();
Output
Item added successfully.
Retrieved item: { Id: { S: '123' }, Name: { S: 'Alice' } }
Common Pitfalls
Common mistakes include:
- Not setting the AWS region in the client configuration.
- Using incorrect data types for item attributes (e.g., string vs number).
- Forgetting to await asynchronous calls, causing unexpected behavior.
- Not handling errors from AWS SDK calls.
Always check your table name and key names match exactly what is in DynamoDB.
javascript
/* Wrong: Missing region and no await */ const clientWrong = new DynamoDBClient({}); clientWrong.send(new PutItemCommand(putParams)); // No await, may cause issues /* Right: Set region and await async calls */ const clientRight = new DynamoDBClient({ region: "us-east-1" }); await clientRight.send(new PutItemCommand(putParams));
Quick Reference
Here is a quick summary of key DynamoDB client methods in Node.js:
| Method | Purpose | Required Parameters |
|---|---|---|
| PutItemCommand | Add or replace an item | TableName, Item |
| GetItemCommand | Retrieve an item by key | TableName, Key |
| UpdateItemCommand | Modify attributes of an item | TableName, Key, UpdateExpression |
| DeleteItemCommand | Remove an item | TableName, Key |
| QueryCommand | Find items by key condition | TableName, KeyConditionExpression |
Key Takeaways
Install and import AWS SDK v3 to use DynamoDB with Node.js.
Create a DynamoDB client with the correct AWS region.
Use async/await to handle asynchronous DynamoDB calls properly.
Match your table and key names exactly to avoid errors.
Handle errors to make your app reliable when accessing DynamoDB.