Skip to main content
Version: v2

CDKTableV2

note

CDKTableV2 requires the aws-cdk-lib and constructs dependencies to be installed first (they usually already are in AWS CDK apps):

npm install aws-cdk-lib constructs

Derives TableV2 props from a Table, so that your Table is the single source of truth for keys and indexes:

import { TableV2, Billing } from 'aws-cdk-lib/aws-dynamodb'
import { CDKTableV2 } from 'dynamodb-toolbox/table/actions/cdk'

import { PokeTable } from '../src/tables'

new TableV2(this, 'PokeTable', {
...PokeTable.build(CDKTableV2).props(),
billing: Billing.onDemand()
})

Methods​

props(...)​

(options?: CDKTableV2Options) => CDKTableV2Props

Returns the partitionKey, sortKey, globalSecondaryIndexes, localSecondaryIndexes and (optionally) tableName props of the Table:

TableTableV2 props
partitionKeypartitionKey
sortKeysortKey (omitted if the Table has none)
global indexesglobalSecondaryIndexes (omitted if none)
local indexeslocalSecondaryIndexes (omitted if none)
nametableName (see options)
'string' / 'number' / 'binary' keysAttributeType.STRING / AttributeType.NUMBER / AttributeType.BINARY

Indexes are emitted in their declaration order, with the index key as indexName. Global indexes with several partitionKeys / sortKeys are emitted as partitionKeys / sortKeys, while single keys (and single-element arrays) are emitted as partitionKey / sortKey.

Options​

OptionTypeDefaultDescription
tableNameboolean-By default, tableName is only emitted if the Table name is a string.

If true, the name is always emitted (and resolved if it is a function). Throws a table.missingTableName error if the Table has no name.

If false, it is never emitted.
Examples
const props = PokeTable.build(CDKTableV2).props()
info
  • Global indexes with multi-attribute keys (more than one partitionKeys or sortKeys) require aws-cdk-lib v2.226.0 or later.
  • Index projections are not set, so they default to ALL.
  • The Table meta is not mapped (TableV2 has no title or description prop).
  • The Table definition is not validated: invalid definitions (like a local index on a table without sort key) are passed as-is, and reported by CDK or CloudFormation at synth or deploy time.