Reading and writing a single item in DynamoDB - AWS SDK for JavaScript

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:

  • Set up the project environment to run these Node.js examples, and install the required AWS 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 Shared config and credentials files in the AWS SDKs and Tools Reference Guide.

  • 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.

Writing an item

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

import { PutItemCommand, DynamoDBClient } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new PutItemCommand({ TableName: "Cookies", // 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 Item: { Flavor: { S: "Chocolate Chip" }, Variants: { SS: ["White Chocolate Chip", "Chocolate Chunk"] }, }, }); const response = await client.send(command); console.log(response); return response; };

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

node put-item.js

This example code can be found here on GitHub.

Update an item

Create a Node.js module with the file name update-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 add an item, which in this example includes the name of the table, the key to update, and the date expression that maps the new attribute names, and values for each new attribute. Call the UpdateItemCommand method of the DynamoDB client service object.

import { UpdateItemCommand, DynamoDBClient } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new UpdateItemCommand({ TableName: "IceCreams", // 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 Key: { Flavor: { S: "Vanilla" }, }, UpdateExpression: "set HasChunks = :chunks", ExpressionAttributeValues: { ":chunks": { BOOL: "false" }, }, ReturnValues: "ALL_NEW", }); const response = await client.send(command); console.log(response); return response; };

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

node update-item.js

This example code can be found here on GitHub.

Getting an item

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

The following code example retrieves an item from a table with a primary key composed of only a partition key and not of both a partition 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 { GetItemCommand, DynamoDBClient } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new GetItemCommand({ TableName: "CafeTreats", // 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 Key: { TreatId: { N: "101" }, }, }); const response = await client.send(command); console.log(response); return response; };

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

node get-item.js

This example code can be found here on GitHub.

Deleting an item

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

import { DeleteItemCommand, DynamoDBClient } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new DeleteItemCommand({ TableName: "Drinks", // 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 Key: { Name: { S: "Pumpkin Spice Latte" }, }, }); const response = await client.send(command); console.log(response); return response; };

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

node delete-item.js

This example code can be found here on GitHub.