Reading and writing items in batch in DynamoDB - AWS SDK for JavaScript

Help us improve the AWS SDK for JavaScript version 3 (V3) documentation by providing feedback using the Feedback link, or create an issue or pull request on GitHub.

The AWS SDK for JavaScript V3 API Reference Guide describes in detail all the API operations for the AWS SDK for JavaScript version 3 (V3).

Reading and writing items in batch in DynamoDB

JavaScript code example that applies to Node.js execution

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:

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 sytax, please refer to JavaScript ES6/CommonJS syntax

Reading items in Batch

Create a libs directory, and create a Node.js module with the file name ddbClient.js. Copy and paste the code below into it, which creates the DynamoDB client object. Replace REGION with your AWS region.

import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon DynamoDB service client object. const ddbClient = new DynamoDBClient({ region: REGION }); export { ddbClient };

This code is available here on GitHub.

Create a Node.js module with the file name ddb_batchgetitem.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.

Note

Replace TABLE_NAME with the name of the table, KEY_NAME with the primary key of the table, KEY_VALUE with the value of the primary key row containing the attribute value, and ATTRIBUTE_NAME the name of the attribute column containing the attribute value.

Note

This the following code below batch retrieves items from a table with a primary key composed of only a partion key - KEY_NAME - and not of both a partion and sort key. If the table has a primary key composed of a partition key and a sort key, you must also specify the sort key name and attribute for each item.

// Import required AWS SDK clients and commands for Node.js import { BatchGetItemCommand } from "@aws-sdk/client-dynamodb"; import { ddbClient } from "./libs/ddbClient.js"; // Set the parameters const params = { RequestItems: { TABLE_NAME: { Keys: [ { KEY_NAME_1: { N: "KEY_VALUE" }, KEY_NAME_2: { N: "KEY_VALUE" }, KEY_NAME_3: { N: "KEY_VALUE" }, }, ], ProjectionExpression: "ATTRIBUTE_NAME", }, }, }; const run = async () => { try { const data = await ddbClient.send(new BatchGetItemCommand(params)); console.log("Success, items retrieved", data); return data; } catch (err) { console.log("Error", err); } }; run();

To run the example, enter the following at the command prompt.

node ddb_batchgetitem.js

This example code can be found here on GitHub.

Writing items in Batch

Create a libs directory, and create a Node.js module with the file name ddbClient.js. Copy and paste the code below into it, which creates the DynamoDB client object. Replace REGION with your AWS region.

import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon DynamoDB service client object. const ddbClient = new DynamoDBClient({ region: REGION }); export { ddbClient };

This code is available here on GitHub.

Create a Node.js module with the file name ddb_batchwriteitem.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.

Note

Replace TABLE_NAME with the name of the table, KEYS with the primary key of the item, KEY_VALUE with the value of the primary key row containing the attribute value, and ATTRIBUTE_NAME the name of the attribute column containing the attribute value.

The following code example batch writes items to a table with a primary key composed of only a partion key - KEY_NAME - and not of both a partion and sort key. If the table has a primary key composed of a partition key and a sort key, you must also specify the sort key name and attribute for each item.

// Import required AWS SDK clients and commands for Node.js import { BatchWriteItemCommand } from "@aws-sdk/client-dynamodb"; import { ddbClient } from "./libs/ddbClient.js"; // Set the parameters const params = { RequestItems: { TABLE_NAME: [ { PutRequest: { Item: { KEY: { N: "KEY_VALUE" }, ATTRIBUTE_1: { S: "ATTRIBUTE_1_VALUE" }, ATTRIBUTE_2: { N: "ATTRIBUTE_2_VALUE" }, }, }, }, { PutRequest: { Item: { KEY: { N: "KEY_VALUE" }, ATTRIBUTE_1: { S: "ATTRIBUTE_1_VALUE" }, ATTRIBUTE_2: { N: "ATTRIBUTE_2_VALUE" }, }, }, }, ], }, }; const run = async () => { try { const data = await ddbClient.send(new BatchWriteItemCommand(params)); console.log("Success, items inserted", data); return data; } catch (err) { console.log("Error", err); } }; run();

To run the example, enter the following at the command prompt.

node ddb_batchwriteitem.js

This example code can be found here on GitHub.