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 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:
- Usage
- Options
const { Items } = await pokeTableRepository.scan()
const { Items } = await pokeTableRepository.scan({
consistent: true,
limit: 10
})
query(...)
β
(query: QUERY, options?: OPTIONS) => QueryResponse<TABLE, QUERY, ENTITIES, OPTIONS>
Performs a Query Operation. See QueryCommand
for more details:
- Usage
- Index
- Options
// Get 'ashKetchum' pokemons
const { Items } = await pokeTableRepository.query({
partition: 'ashKetchum'
})
// Get 'ashKetchum1' pokemons with a level β₯ 50
const { Items } = await pokeTableRepository.query({
partition: 'ashKetchum1',
index: 'byTrainerId',
range: { gte: 50 }
})
// Consistently get less than 10 'ashKetchum' pokemons
const { Items } = await pokeTableRepository.query(
{ partition: 'ashKetchum' },
{ consistent: true, limit: 10 }
)
deletePartition(...)
β
(query: QUERY, options?: OPTIONS) => DeletePartitionResponse<TABLE, QUERY, OPTIONS>
DeletePartitionCommand
is exposed as a quality of life improvement, but is NOT an official DynamoDB operation (eventhough we wish it was).
Performs one or more Query operations on the Table
, and then runs BatchWriteCommands
to batch delete the returned items. Automatically iterates through query pages if needed. See DeletePartitionCommand
for more details:
- Usage
- Index
- Options
// Delete 'ashKetchum' pokemons
await pokeTableRepository.deletePartition({
partition: 'ashKetchum'
})
// Delete 'ashKetchum1' pokemons with a level β₯ 50
await pokeTableRepository.deletePartition({
partition: 'ashKetchum1',
index: 'byTrainerId',
range: { gte: 50 }
})
// Consistently delete less than 10 'ashKetchum' pokemons
await pokeTableRepository.deletePartition(
{ partition: 'ashKetchum' },
{ consistent: true, limit: 10 }
)
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:
- Usage
- Ent. Repository
- Options
const batchGetCommand = pokeTableRepository.batchGet(
PokemonEntity.build(BatchGetRequest).key(pikachuKey),
TrainerEntity.build(BatchGetRequest).key(ashKetchumKey)
)
const batchGetCommand = pokeTableRepository.batchGet(
pokemonRepository.batchGet(pikachuKey),
trainerRepository.batchGet(ashKetchumKey)
)
const batchGetCommand = pokeTableRepository.batchGet(
{ consistent: true },
pokemonRepository.batchGet(pikachuKey),
trainerRepository.batchGet(ashKetchumKey)
)
executeBatchGet(...)
β
static (opt?: OPTIONS, ...cmd: COMMANDS) => ExecuteBatchGetResponses<COMMANDS>
The BatchGetCommand
executor exposed as a static method:
- Usage
- Options
const { Responses } = await TableRepository.executeBatchGet(
// Only one `BatchGetCommand` per table is supported
pokeTableRepository.batchGet(...),
otherTableRepository.batchGet(...),
)
const { Responses } = await TableRepository.executeBatchGet(
{ maxAttempts: Infinity },
// 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:
- Usage
- Ent. Repository
- Options
const batchWriteCommand = pokeTableRepository.batchWrite(
PokemonEntity.build(BatchPutRequest).item(pikachu),
TrainerEntity.build(BatchPutRequest).item(ashKetchum)
)
const batchWriteCommand = pokeTableRepository.batchWrite(
pokemonRepository.batchPut(pikachu),
trainerRepository.batchPut(ashKetchum)
)
const batchWriteCommand = pokeTableRepository.batchWrite(
{ tableName: `tenant-${tenantId}-pokemons` },
pokemonRepository.batchPut(pikachu),
trainerRepository.batchPut(ashKetchum)
)
executeBatchWrite(...)
β
static (opt?: OPTIONS, ...cmd: COMMANDS) => BatchWriteCommandOutput
The BatchWriteCommand
executor exposed as a static method:
- Usage
- Options
await TableRepository.executeBatchWrite(
// Only one `BatchWriteCommand` per table is supported
pokeTableRepository.batchWrite(...),
otherTableRepository.batchWrite(...),
)
await TableRepository.executeBatchWrite(
{ maxAttempts: Infinity },
// Only one `BatchWriteCommand` per table is supported
pokeTableRepository.batchWrite(...),
otherTableRepository.batchWrite(...),
)
Utilsβ
accessPattern(...)
β
(sch: SCHEMA, ptrn: Pattern<SCHEMA, QUERY>, opt?: OPTIONS) => AccessPattern<TABLE, SCHEMA, QUERY, OPTIONS>
Creates an AccessPattern
. See AccessPattern
for more details:
const accessPattern = pokeTableRepository.accessPattern(
map({ trainerId: string() }),
({ trainerId }) => ({ partition: trainerId }),
{ attributes: ['trainerId'] }
)
const { Items } = await accessPattern
.query({ trainerId: '123' })
.send()
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