Skip to main content

TableRepository

A utility action that exposes all table actions as methods. Using it leads to heavier bundles (as it necessarily imports all of their code) but provides a more concise syntax:

import { TableRepository } from 'dynamodb-toolbox/table/actions/repository'

const pokeTableRepository = PokeTable.build(TableRepository)

// 👇 Sends a `ScanCommand`
await pokeTableRepository.scan()
note

Note that Spies can still be used in cunjunction with Repositories as commands are still sent under the hood.

Specifying entities

You can provide a list of entities to the repository. Those are then provided to all underlying actions:

const pokeTableRepository = PokeTable.build(TableRepository)
.entities(PokemonEntity, TrainerEntity, ...)

// 👇 Typed as (Pokemon | Trainer | ...)[]
const { Items } = await pokeTableRepository.scan()

Methods

scan(...)

(options?: OPTIONS) => ScanResponse<TABLE, ENTITIES, OPTIONS>

Performs a Scan Operation. See ScanCommand for more details:

Examples
const { Items } = await pokeTableRepository.scan()

query(...)

(query: QUERY, options?: OPTIONS) => QueryResponse<TABLE, QUERY, ENTITIES, OPTIONS>

Performs a Query Operation. See QueryCommand for more details:

Examples
// Get 'ashKetchum' pokemons
const { Items } = await pokeTableRepository.query({
partition: 'ashKetchum'
})

deletePartition(...)

(query: QUERY, options?: OPTIONS) => DeletePartitionResponse<TABLE, QUERY, OPTIONS>

warning

DeletePartitionCommand is exposed as a quality of life improvement, but is NOT an official DynamoDB operation (eventhough we wish it was).

Performs a paginated Query Operation on a Table and run subsequent BatchWriteCommands to batch delete returned items. See DeletePartitionCommand for more details:

Examples
// Delete 'ashKetchum' pokemons
await pokeTableRepository.deletePartition({
partition: 'ashKetchum'
})

Batch Gets

batchGet(...)

(opt?: OPTIONS, ...req: REQUESTS) => BatchGetCommand<TABLE, ENTITIES, REQUESTS, OPTIONS>

Groups one or several BatchGetRequest from the Table entities to execute a BatchGetItem operation. Additional options can be provided as a first argument. See BatchGet for more details:

Examples
const batchGetCommand = pokeTableRepository.batchGet(
PokemonEntity.build(BatchGetRequest).key(pikachuKey),
TrainerEntity.build(BatchGetRequest).key(ashKetchumKey)
)

executeBatchGet(...)

static (opt?: OPTIONS, ...cmd: COMMANDS) => ExecuteBatchGetResponses<COMMANDS>

The BatchGetCommand executor exposed as a static method:

Examples
const { Responses } = await TableRepository.executeBatchGet(
// Only one `BatchGetCommand` per table is supported
pokeTableRepository.batchGet(...),
otherTableRepository.batchGet(...),
)

Batch Writes

batchWrite(...)

(opt?: OPTIONS, ...req: REQUESTS) => BatchWriteCommand<TABLE, ENTITIES, REQUESTS>

Groups one or several BatchPutRequest and BatchDeleteRequest from the Table entities to execute a BatchWriteItem operation. Additional options can be provided as a first argument. See BatchWrite for more details:

Examples
const batchWriteCommand = pokeTableRepository.batchWrite(
PokemonEntity.build(BatchPutRequest).item(pikachu),
TrainerEntity.build(BatchPutRequest).item(ashKetchum)
)

executeBatchWrite(...)

static (opt?: OPTIONS, ...cmd: COMMANDS) => BatchWriteCommandOutput

The BatchWriteCommand executor exposed as a static method:

Examples
await TableRepository.executeBatchWrite(
// Only one `BatchWriteCommand` per table is supported
pokeTableRepository.batchWrite(...),
otherTableRepository.batchWrite(...),
)

Utils

parsePrimaryKey(...)

(input: unknown) => PrimaryKey<TABLE>

Parses a Primary Key. See ParsePrimaryKey for more details:

const primaryKey = pokeTableRepository.parsePrimaryKey({
partitionKey: 'pikachu',
sortKey: 42,
foo: 'bar'
})
// ✅ => { partitionKey: 'pikachu', sortKey: 42 }

pokeTableRepository.parsePrimaryKey({ invalid: 'input' })
// ❌ Throws an error