Creating and using tables 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).

Creating and using tables in DynamoDB

JavaScript code example that applies to Node.js execution

This Node.js code example shows:

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

Creating a table

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"; export const client = new DynamoDBClient({ region: "us-east-1" })

This code is available here on GitHub.

Create a Node.js module with the file name ddb_createtable.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 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 CreateTableCommand method of the DynamoDB service object.

Note

Replace TABLE_NAMEwith the name of the table, ATTRIBUTE_NAME_1 with the name of the partition key, ATTRIBUTE_NAME_2 with the name of the sort key (optionally), and ATTRIBUTE_TYPE with the type of the attribute (for example, N [for a number], S [for a string] etc.).

Note

The primary key for the table is composed of the following attributes:

  • Season

  • Episode

import { CreateTableCommand, DynamoDBClient } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new CreateTableCommand({ TableName: "EspressoDrinks", // 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 AttributeDefinitions: [ { AttributeName: "DrinkName", AttributeType: "S", }, ], KeySchema: [ { AttributeName: "DrinkName", KeyType: "HASH", }, ], ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1, }, }); const response = await client.send(command); console.log(response); return response; };

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

node ddb_createtable.js

This example code can be found here on GitHub.

Listing your tables

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"; export const client = new DynamoDBClient({ region: "us-east-1" })

This code is available here on GitHub.

Create a Node.js module with the file name ddb_listtables.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 list your tables, which in this example limits the number of tables listed to 10. Call the ListTablesCommand method of the DynamoDB service object.

import { ListTablesCommand, DynamoDBClient } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new ListTablesCommand({}); const response = await client.send(command); console.log(response); return response; };

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

node ddb_listtables.js

This example code can be found here on GitHub.

Describing a table

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"; export const client = new DynamoDBClient({ region: "us-east-1" })

This code is available here on GitHub.

Create a Node.js module with the file name ddb_describetable.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 describe a DescribeTableCommand method of the DynamoDB service object.

Note

Replace TABLE_NAME with the name of the table.

import { DescribeTableCommand, DynamoDBClient } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new DescribeTableCommand({ TableName: "Pastries", }); const response = await client.send(command); console.log(`TABLE NAME: ${response.Table.TableName}`); console.log(`TABLE ITEM COUNT: ${response.Table.ItemCount}`); return response; };

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

node ddb_describetable.js

This example code can be found here on GitHub.

Deleting a table

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"; export const client = new DynamoDBClient({ region: "us-east-1" })

This code is available here on GitHub.

Create a Node.js module with the file name ddb_deletetable.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 a table, which in this example includes the name of the table provided as a command-line parameter. Call the DeleteTableCommand method of the DynamoDB service object.

import { DeleteTableCommand, DynamoDBClient } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new DeleteTableCommand({ TableName: "DecafCoffees", }); const response = await client.send(command); console.log(response); return response; };

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

node ddb_deletetable.js

This example code can be found here on GitHub.