Menu
AWS SDK for JavaScript
Developer Guide

Creating and Using Tables in DynamoDB


                                        Relationship between JavaScript environments, the SDK, and Lambda

This Node.js example shows you how to create and manage tables used to store and retrieve data from DynamoDB.

The Scenario

Similar to other database systems, DynamoDB stores data in tables. A DynamoDB table is a collection of data that's organized into items that are analogous to rows. To store or access data in DynamoDB, you create and work with tables.

In this example, you use a series of Node.js modules to perform basic operations with a DynamoDB table. The code uses the SDK for JavaScript to create and work with tables 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');

Creating a Table

Create a Node.js module with the file name ddb_createtable.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 create a table, which in this example includes the name and data type for each attribute, the key schema, the name of the table, and the units of throughput to provision. Call the createTable 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 = { AttributeDefinitions: [ { AttributeName: 'CUSTOMER_ID', AttributeType: 'N' }, { AttributeName: 'CUSTOMER_NAME', AttributeType: 'S' } ], KeySchema: [ { AttributeName: 'CUSTOMER_ID', KeyType: 'HASH' }, AttributeName: 'CUSTOMER_NAME', AttributeType: 'RANGE' ], ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1 }, TableName: 'CUSTOMER_LIST', StreamSpecification: { StreamEnabled: false } }; // Call DynamoDB to create the table ddb.createTable(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.Table.KeySchema); } });

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

Copy
node ddb_createtable.js

This sample code can be found here on GitHub.

Listing Your Tables

Create a Node.js module with the file name ddb_listtables.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 list your tables, which in this example limits the number of tables listed to 10. Call the listTables 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'}); // Call DynamoDB to retrieve the list of tables ddb.listTables({Limit: 10}, function(err, data) { if (err) { console.log("Error", err.code); } else { console.log("Table names are ", data.TableNames); } });

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

Copy
node ddb_listtables.js

This sample code can be found here on GitHub.

Describing a Table

Create a Node.js module with the file name ddb_describetable.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 describe a table, which in this example includes the name of the table provided as a command-line parameter. Call the describeTable 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: process.argv[2] }; // Call DynamoDB to retrieve the selected table descriptions ddb.describeTable(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.Table.KeySchema); } });

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

Copy
node ddb_describetable.js TABLE_NAME

This sample code can be found here on GitHub.

Deleting a Table

Create a Node.js module with the file name ddb_deletetable.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 describe a table, which in this example includes the name of the table provided as a command-line parameter. Call the deleteTable 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: process.argv[2] }; // Call DynamoDB to delete the specified table ddb.deleteTable(params, function(err, data) { if (err && err.code === 'ResourceNotFoundException') { console.log("Error: Table not found"); } else if (err && err.code === 'ResourceInUseException') { console.log("Error: Table in use"); } else { console.log("Success", data); } });

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

Copy
node ddb_deletetable.js TABLE_NAME

This sample code can be found here on GitHub.