Skip to main content

Table

Each Table instance describes the configuration of a deployed DynamoDB Table: Its name, primary key, secondary indexes, and more.

import { Table } from 'dynamodb-toolbox/table'

const PokeTable = new Table({
...
})
info

The configuration provided to the Table constructor must match your resources. But DynamoDB-Toolbox does NOT hold the responsibility of actually deploying them. This should be done by other means, like the AWS CLI, Terraform or Cloudformation.

Constructor

Table takes a single parameter of type object and accepts the following properties:

documentClient

As mentioned in the Getting Started section, DynamoDB-Tooblox is an abstraction layer over the Document Client, but it does not replace it. A DocumentClient instance is explicitly needed for commands to interact with DynamoDB.

You can provide it in the Table constructor:

// From peer dependencies
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'

const dynamoDBClient = new DynamoDBClient()

const documentClient = DynamoDBDocumentClient.from(
dynamoDBClient,
{
marshallOptions: {
// Specify your client options as usual
removeUndefinedValues: true,
convertEmptyValues: false
...
}
}
)

const PokeTable = new Table({
documentClient,
...
})

You can also set it later in the code (but beware that commands fail if no client has been provided):

const PokeTable = new Table(...)

// Later in the code
const documentClient = ...
PokeTable.documentClient = documentClient

name

A string (or function returning a string) that matches the name of your DynamoDB table:

Examples
const PokeTable = new Table({
name: "poke-table",
...
});

You can also provide it through command options – which is useful for multitenant apps – but beware that commands fail if no table name has been provided:

const PokeTable = new Table({
// Omit `name` property
documentClient,
...
})

// Scan tenant table
const { Items } = await PokeTable.build(ScanCommand)
.options({ tableName: tenantTableName })
.send()

partitionKey

(required)

The partition key attribute name and type of your DynamoDB table:

Examples
const MyTable = new Table({
...,
partitionKey: {
name: "pokemonId",
type: "string",
}
})

sortKey

If present, the sort key attribute name and type of your DynamoDB table:

const MyTable = new Table({
...,
sortKey: {
name: "level",
type: "number",
}
})

indexes

An object that lists the secondary indexes of your DynamoDB Table.

Secondary indexes are represented as key-value pairs, keys being the index names, and values containing:

  • The type of the secondary index ("local" or "global")
  • For global secondary indexes, the partitionKey of the index (similar to the main partitionKey)
  • The sortKey of the index (similar to the main sortKey)
Examples
const MyTable = new Table({
...,
indexes: {
byTrainerId: {
type: 'global',
partitionKey: { name: 'trainerId', type: 'string' }
}
}
})
caution

When whitelisted, the projected attributes of a secondary index MUST include the Table's entity attribute for automatic parsing of the returned data.

entityAttributeSavedAs

DynamoDB-Toolbox tags your data via an internal and hidden entity attribute. Any write command automatically sets its value to the corresponding Entity name.

To allow for appropriate formatting when fetching multiple items of the same Table in a single operation (like Queries or Scans), the key of this attribute must be the same accross all of its items, so it must be set at the Table level.

Its default value is _et, but it can be renamed through the entityAttributeSavedAs argument:

const MyTable = new Table({
...
// 👇 defaults to '_et'
entityAttributeSavedAs: '__entity__',
});
caution

☝️ This property cannot be updated once your Table has its first item (at least not without a data migration first), so choose wisely!