AccessPattern
The AccessPattern
utility allows you to quickly build QueryCommands
on the entity Table
:
import { AccessPattern } from 'dynamodb-toolbox/entity/actions/accessPattern'
const highLevelPokemons = PokemonEntity.build(AccessPattern)
// Define the expected query input schema
.schema(map({ trainerId: string(), level: number() }))
// Declare the query pattern
.pattern(({ trainerId, level }) => ({
index: 'byLevel',
partition: trainerId,
range: { gte: level }
}))
// Optional: provide additional options
.options({ maxPages: 3 })
const { Items } = await highLevelPokemons
.query({ trainerId, level: 70 })
.send()
Remember that schemas can be composed or derived from existing ones:
const queryPokemonsByLevel = PokemonEntity
.build(AccessPattern)
.schema(pokemonSchema.pick('trainerId', 'level'))
For multiple Entities
access patterns, see the dedicated AccessPattern
Table Action.
Methodsβ
.schema(...)
β
(required)
Defines the input schema for the pattern parameters (see the Schema Section for more details):
import { string } from 'dynamodb-toolbox/schema/string'
const stringAccessPattern = PokemonEntity
.build(AccessPattern)
.schema(string())
.pattern(...)
β
(required)
Defines how pattern inputs (parsed by the schema
) are translated into a query
, i.e. an object containing:
partition
: The partition key to queryindex (optional)
: The name of a secondary index to queryrange (optional)
: If the table or index has a sort key, an additional Range or Equality Condition
const stringAccessPattern = PokemonEntity
.build(AccessPattern)
.schema(string())
.pattern(str => ({ partition: str }))
It is advised to provide schema
first as it constrains the pattern
type.
.options(...)
β
Provides additional options to the resulting QueryCommands
(see QueryCommands
for more details):
const projectedPattern = PokemonEntity
.build(AccessPattern)
.options({ attributes: ['name', 'trainerId'] })
.meta(...)
β
Adds metadata to the AccessPattern
.
The meta
object can include a title
and description
, both of which must be strings. Additional fields can be of any type:
const pokemonsByLevelPattern = PokemonEntity.build(AccessPattern)
.meta({
title: 'Trainer PokΓ©mons by Level',
description:
'Returns the pokemons of a trainer (`trainerId`) above a given level (`minLevel`)',
other: { field: 'of any type' }
})
Metadata is especially useful when building MCPToolkit
Actions.
.query(...)
β
Produces a QueryCommand
from valid pattern inputs:
const queryCommand = highLevelPokemons
.query({ trainerId, level: 70 })
const { Items } = await queryCommand
// Optional: Refine queryCommand
.options({ consistent: true })
.send()