logo
post image

Check DynamoDB Tables Exist using Node.js

We can check whether specific DynamoDB tables exist using the AWS DynamoDB Javascript SDK. This can be installed in a Node.js application through the command :

npm install @aws-sdk/client-dynamodb

Once the package is installed, the ListTablesCommand method can be used to list tables.

The below code examples assumes 3 environment variables have been set — AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY & AWS_REGION.

const { DynamoDBClient, ListTablesCommand } = require("@aws-sdk/client-dynamodb")

// init dynamodb client
const dbclient = new DynamoDBClient({ region: process.env.AWS_REGION })

// list all tables in the region
const data = await dbclient.send(new ListTablesCommand({}))

// data.TableNames will hold an array of table names
console.log(data.TableNames)

We can also search table names starting with a given prefix.

const { DynamoDBClient, ListTablesCommand } = require("@aws-sdk/client-dynamodb")

const dbclient = new DynamoDBClient({ region: process.env.AWS_REGION })

// list all tables in the region whose names start with "TableX"
const data = await dbclient.send(new ListTablesCommand({
	ExclusiveStartTableName: "TableX",
}))

console.log(data.TableNames)