The Amazon SDK for JavaScript V3 API
Reference Guide
Reading and writing items in batch in DynamoDB
This Node.js code example shows:
How to read and write batches of items in a DynamoDB table.
The scenario
In this example, you use a series of Node.js modules to put a batch of items in a DynamoDB table and read a batch of items. The code uses the SDK for JavaScript to perform batch read and write operations using these methods of the DynamoDB client class:
Prerequisite tasks
To set up and run this example, first complete these tasks:
-
Set up the project environment to run these Node TypeScript examples, and install the required Amazon SDK for JavaScript and third-party modules. Follow the instructions on GitHub
. Create a shared configurations file with your user credentials. For more information about providing a shared credentials file, see Loading credentials in Node.js from the shared credentials file.
Create a DynamoDB table whose items you can access. For more information about creating a DynamoDB table, see Creating and using tables in DynamoDB.
Important
These examples use ECMAScript6 (ES6). This requires Node.js version 13.x or higher. To download and install the latest version of
Node.js, see Node.js downloads.
However, if you prefer to use CommonJS syntax, please refer to JavaScript ES6/CommonJS syntax.
Note
For information about the data types used in these examples, see Supported data types and naming rules in Amazon DynamoDB
Reading items in a batch
Create a Node.js module with the file name batch-get-item.js
. Be
sure to configure the SDK as previously shown, including downloading the required
clients and packages. To access DynamoDB, create a DynamoDB
client
service object. Create a JSON object containing the parameters needed to get a batch
of items, which in this example includes the name of one or more tables from which
to read, the values of keys to read in each table, and the projection expression
that specifies the attributes to return. Call the BatchGetItemCommand
method of the DynamoDB service object.
import { BatchGetItemCommand, DynamoDBClient } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new BatchGetItemCommand({ RequestItems: { // Each key in this object is the name of a table. This example refers // to a PageAnalytics table. PageAnalytics: { // Each entry in Keys is an object that specifies a primary key. Keys: [ { // "PageName" is the partition key (simple primary key). // "S" specifies a string as the data type for the value "Home". // For more information about data types, // see https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes and // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.LowLevelAPI.html#Programming.LowLevelAPI.DataTypeDescriptors PageName: { S: "Home" }, }, { PageName: { S: "About" }, }, ], // Only return the "PageName" and "PageViews" attributes. ProjectionExpression: "PageName, PageViews", }, }, }); const response = await client.send(command); console.log(response.Responses['PageAnalytics']); return response; };
To run the example, enter the following at the command prompt.
node batch-get-item.js
This example code can be found here on GitHub
Writing items in a batch
Create a Node.js module with the file name batch-write-item.js
. Be
sure to configure the SDK as previously shown, including downloading the required
clients and packages. To access DynamoDB, create a DynamoDB
client
service object. Create a JSON object containing the parameters needed to get a batch
of items, which in this example includes the table into which you want to write
items, the keys you want to write for each item, and the attributes along with
their values. Call the BatchWriteItemCommand
method of the DynamoDB
service object.
import { BatchWriteItemCommand, DynamoDBClient, } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new BatchWriteItemCommand({ RequestItems: { // Each key in this object is the name of a table. This example refers // to a Coffees table. Coffees: [ // Each entry in Coffees is an object that defines either a PutRequest or DeleteRequest. { // Each PutRequest object defines one item to be inserted into the table. PutRequest: { // The keys of Item are attribute names. Each attribute value is an object with a data type and value. // For more information about data types, // see https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes Item: { Name: { S: "Donkey Kick" }, Process: { S: "Wet-Hulled" }, Flavors: { SS: ["Earth", "Syrup", "Spice"] }, }, }, }, { PutRequest: { Item: { Name: { S: "Flora Ethiopia" }, Process: { S: "Washed" }, Flavors: { SS: ["Stone Fruit", "Toasted Almond", "Delicate"] }, }, }, }, ], }, }); const response = await client.send(command); console.log(response); return response; };
To run the example, enter the following at the command prompt.
node batch-write-item.js
This example code can be found here on GitHub