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:
Table | TableV2 props |
|---|---|
partitionKey | partitionKey |
sortKey | sortKey (omitted if the Table has none) |
global indexes | globalSecondaryIndexes (omitted if none) |
local indexes | localSecondaryIndexes (omitted if none) |
name | tableName (see options) |
'string' / 'number' / 'binary' keys | AttributeType.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β
| Option | Type | Default | Description |
|---|---|---|---|
tableName | boolean | - | 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
- Usage
- Resolved name
- No name
const props = PokeTable.build(CDKTableV2).props()
const PokeTable = new Table({
name: () => process.env.POKE_TABLE_NAME,
...
})
const props = PokeTable.build(CDKTableV2).props({
tableName: true
})
// π Let CloudFormation generate the table name
const props = PokeTable.build(CDKTableV2).props({
tableName: false
})
info
- Global indexes with multi-attribute keys (more than one
partitionKeysorsortKeys) requireaws-cdk-libv2.226.0 or later. - Index projections are not set, so they default to
ALL. - The
Tablemetais not mapped (TableV2has no title or description prop). - The
Tabledefinition is not validated: invalid definitions (like alocalindex on a table without sort key) are passed as-is, and reported by CDK or CloudFormation at synth or deploy time.