Menu
AWS SDK for JavaScript
Developer Guide for SDK version 2.122.0

Reading and Writing A Single Item in DynamoDB


                                        Relationship between JavaScript environments, the SDK, and Lambda

This Node.js example shows you how to add, retrieve, and delete a single 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, setting the region, and providing credentials for your code. In this example, you use the JSON file you created to provide the credentials.

Copy
// Load the SDK for JavaScript var AWS = require('aws-sdk'); // Load credentials and set region from JSON file AWS.config.loadFromPath('./config.json');

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'); // Load credentials and set region from JSON file AWS.config.loadFromPath('./config.json'); // 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'); // Load credentials and set region from JSON file AWS.config.loadFromPath('./config.json'); // 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'); // Load credentials and set region from JSON file AWS.config.loadFromPath('./config.json'); // 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.