This article demonstrates using the AWS SDK with Node.js to interact with DynamoDB for CRUD operations.
In this lesson, we will demonstrate how to use the AWS SDK (version 3) with Node.js to interact with DynamoDB. You will learn how to create, update, delete, and retrieve entries from a DynamoDB table. Although this demo uses Node.js, these techniques are applicable to other programming languages such as Python.
The first step is to install the DynamoDB client library. For Node.js, use npm:
Copy
Ask AI
npm install @aws-sdk/client-dynamodb
After installation, you should see an output similar to this:
Copy
Ask AI
C:\...>npm install @aws-sdk/client-dynamodbadded 83 packages, and audited 84 packages in 2s2 packages are looking for fundingrun `npm fund` for detailsfound 0 vulnerabilities
Once the library is installed, import the necessary modules and create an instance of the DynamoDB client. Remember to provide your AWS region and credentials.
Never hardcode your credentials in production applications. Use environment variables or secure secrets management instead.
Copy
Ask AI
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";const client = new DynamoDBClient({ region: "us-east-1", credentials: { accessKeyId: "AKIA4IAWSJ5UZT3W7PEN", secretAccessKey: "GDJYKyQifTDaaA8SRm7gXyQ2CYXkgz/DJBRje0dJ", },});
When you run your Node.js application, the npm installation output should resemble:
Copy
Ask AI
C:\...>npm install @aws-sdk/client-dynamodbadded 83 packages, and audited 84 packages in 2s2 packages are looking for fundingrun `npm fund` for detailsfound 0 vulnerabilities
Assume you have a DynamoDB table named products that stores items with attributes such as ID (string), category (string), inventory (number), name (string), price (number), and onSale (Boolean). To retrieve an item—for example, an item with an ID of “80”—use the GetItem command.
To add an item to your DynamoDB table, import the PutItemCommand and define an object representing the new item. The following example demonstrates how to add a new product with an ID of “3000”.
A successful response from DynamoDB will include an HTTP status code of 200. Note that by default, DynamoDB does not return the newly created item unless additional parameters are provided.
The DynamoDB Document Client offers a higher-level abstraction, allowing you to work with native JavaScript types without explicitly specifying data types (such as { S: "value" } or { N: "value" }). First, install the additional library:
Copy
Ask AI
npm install @aws-sdk/lib-dynamodb
Then, initialize the Document Client alongside the standard DynamoDB client:
Copy
Ask AI
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";const client = new DynamoDBClient({ region: "us-east-1", credentials: { accessKeyId: "AKIA4IAWSJ5UZT3W7PEN", secretAccessKey: "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", },});const docClient = new DynamoDBDocumentClient(client);
Begin by defining an array of JavaScript objects that represent the items to insert. Then, convert each object into the required format by mapping it to an object with a PutRequest property:
Retrieve items using the low-level GetItemCommand.
Insert new items using the PutItemCommand.
Leverage the higher-level DynamoDB Document Client to work with native JavaScript types.
Perform bulk write operations using the BatchWriteCommand.
These techniques form the foundation for building applications that perform basic CRUD operations via an API, and future lessons will explore these concepts further.