Menu
AWS SDK for JavaScript
Developer Guide for SDK v2.154.0

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 AWS.DynamoDB client class:

Prerequisite Tasks

To set up and run this example, first complete these tasks:

Configuring the SDK

Configure the SDK for JavaScript by creating a global configuration object then setting the region for your code. In this example, the region is set to us-west-2.

Copy
// Load the SDK for JavaScript var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'us-west-2'});

Writing an Item

Create a Node.js module with the file name ddb_putitem.js. Be sure to configure the SDK as previously shown. To access DynamoDB, create an AWS.DynamoDB 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 putItem method of the DynamoDB service object.

Copy
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the DynamoDB service object ddb = new AWS.DynamoDB({apiVersion: '2012-10-08'}); var params = { TableName: 'TABLE', Item: { 'CUSTOMER_ID' : {N: '001'}, 'CUSTOMER_NAME' : {S: 'Richard Roe'}, } }; // Call DynamoDB to add the item to the table ddb.putItem(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });

To run the example, type the following at the command line.

Copy
node ddb_putitem.js

This sample code can be found here on GitHub.

Getting an Item

Create a Node.js module with the file name ddb_getitem.js. Be sure to configure the SDK as previously shown. To access DynamoDB, create an AWS.DynamoDB 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 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 getItem method of the DynamoDB service object.

Copy
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the DynamoDB service object ddb = new AWS.DynamoDB({apiVersion: '2012-10-08'}); var params = { TableName: 'TABLE', Key: { 'KEY_NAME' : {N: '001'}, }, ProjectionExpression: 'ATTRIBUTE_NAME' }; // Call DynamoDB to read the item from the table ddb.getItem(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.Item); } });

To run the example, type the following at the command line.

Copy
node ddb_getitem.js

This sample code can be found here on GitHub.

Deleting an Item

Create a Node.js module with the file name ddb_deleteitem.js. Be sure to configure the SDK as previously shown. To access DynamoDB, create an AWS.DynamoDB 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 both the key name and value for the item you're deleting. Call the deleteItem method of the DynamoDB service object.

Copy
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the DynamoDB service object ddb = new AWS.DynamoDB({apiVersion: '2012-10-08'}); var params = { TableName: 'TABLE', Key: { 'KEY_NAME' : {S: 'VALUE'}, } }; // Call DynamoDB to delete the item from the table ddb.deleteItem(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });

To run the example, type the following at the command line.

Copy
node ddb_deleteitem.js

This sample code can be found here on GitHub.