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
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