Reading and writing a single item 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 a single item in DynamoDB

JavaScript code example that applies to Node.js execution

This Node.js code example shows:

  • How to add an item in a DynamoDB table.

  • How to retrieve, an item in a DynamoDB table.

  • How to delete an item in a DynamoDB table.

The scenario

In this example, you use a series of Node.js modules to read and write one item in a DynamoDB table by 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

Writing an item

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_putitem.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 add an item, which in this example includes the name of the table and a map that defines the attributes to set and the values for each attribute. Call the PutItemCommand method of the DynamoDB client service object.

Note

Replace TABLE_NAME with the name of the table.

Note

The following code example writes to a table with a primary key composed of only a partion key - CUSTOMER_ID - and a sort key - CUSTOMER_NAME. If the table's primary key is composed of only a partition key, you only specify the partition key.

// Import required AWS SDK clients and commands for Node.js import { PutItemCommand } from "@aws-sdk/client-dynamodb"; import { ddbClient } from "./libs/ddbClient.js"; // Set the parameters const params = { TableName: "TABLE_NAME", Item: { CUSTOMER_ID: { N: "001" }, CUSTOMER_NAME: { S: "Richard Roe" }, }, }; const run = async () => { try { const data = await ddbClient.send(new PutItemCommand(params)); console.log(data); return data; } catch (err) { console.error(err); } }; run();

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

node ddb_putitem.js

This example code can be found here on GitHub.

Getting an item

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_getitem.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. To identify the item to get, you must provide the value of the primary key for that item in the table. By default, the GetItemCommand method returns all the attribute values defined for the item. To get only a subset of all possible attribute values, specify a projection expression.

Create a JSON object containing the parameters needed to get an item, which in this example includes the name of the table, the name and value of the key for the item you're getting, and a projection expression that identifies the item attribute you want to retrieve. Call the GetItemCommand method of the DynamoDB client service object.

Note

Replace TABLE_NAME with the name of the table, KEY_NAME with the primary key of the table, KEY_NAME_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 retrieves an item 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.

// Import required AWS SDK clients and commands for Node.js import { GetItemCommand } from "@aws-sdk/client-dynamodb"; import { ddbClient } from "./libs/ddbClient.js"; // Set the parameters const params = { TableName: "TABLE_NAME", //TABLE_NAME Key: { KEY_NAME: { N: "KEY_VALUE" }, }, ProjectionExpression: "ATTRIBUTE_NAME", }; const run = async () => { const data = await ddbClient.send(new GetItemCommand(params)); console.log("Success", data.Item); return data; }; run();

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

node ddb_getitem.js

This example code can be found here on GitHub.

Deleting an item

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_deleteitem.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 delete an item, which in this example includes the name of the table and both the key name and value for the item you're deleting. Call the DeleteItemCommand method of the DynamoDB client service object.

Note

Replace TABLE_NAME with the name of the table.

Note

The following code example below deletes a item 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.

// Import required AWS SDK clients and commands for Node.js import { DeleteItemCommand } from "@aws-sdk/client-dynamodb"; import { ddbClient } from "./libs/ddbClient.js"; // Set the parameters var params = { TableName: "TABLE_NAME", Key: { KEY_NAME: { N: "VALUE" }, }, }; const run = async () => { try { const data = await ddbClient.send(new DeleteItemCommand(params)); console.log("Success, table deleted", data); return data; } catch (err) { if (err && err.code === "ResourceNotFoundException") { console.log("Error: Table not found"); } else if (err && err.code === "ResourceInUseException") { console.log("Error: Table in use"); } } }; run();

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

node ddb_deleteitem.js

This example code can be found here on GitHub.