QueryCommand
Performs a Query Operation on a Table
:
import { QueryCommand } from 'dynamodb-toolbox/table/actions/query'
const queryCommand = PokeTable.build(QueryCommand)
const params = queryCommand.params()
const { Items } = await queryCommand.send()
Request
.query(...)
(required)
The partition to query, with optional index and range condition:
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
// Get 'ashKetchum' pokemons
await PokeTable.build(QueryCommand)
.query({ partition: 'ashKetchum' })
.send()
You can use the Query
type to explicitly type an object as a QueryCommand
query object:
import type { Query } from 'dynamodb-toolbox/table/actions/query'
// Get 'ashKetchum1' pokemons with a level ≥ 50
const query: Query<typeof PokeTable> = {
index: 'byTrainerId',
partition: 'ashKetchum1',
range: { gte: 50 }
}
const { Items } = await PokeTable.build(QueryCommand)
.query(query)
.send()
.entities(...)
Provides a list of entities to filter the returned items (via the internal entity
attribute). Also formats them and types the response:
// 👇 Typed as (Pokemon | Trainer)[]
const { Items } = await PokeTable.build(QueryCommand)
.query(query)
.entities(PokemonEntity, TrainerEntity)
.send()
Returned items are also tagged with their respective entity names through the $entity
symbol:
import { $entity } from 'dynamodb-toolbox/table/actions/query'
const { Items = [] } = await queryCommand.send()
for (const item of Items) {
switch (item[$entity]) {
case "pokemon":
// 🙌 Typed as Pokemon
...
case "trainer":
// 🙌 Typed as Trainer
...
}
}
.options(...)
Provides additional options:
const { Items } = await PokeTable.build(QueryCommand)
.options({
consistent: true,
limit: 10
...
})
.send()
You can use the QueryOptions
type to explicitly type an object as a QueryCommand
options object:
import type { QueryOptions } from 'dynamodb-toolbox/table/actions/query'
const queryOptions: QueryOptions<
typeof PokeTable,
// 👇 Optional entities
[typeof PokemonEntity, typeof TrainerEntity]
> = {
consistent: true,
limit: 10,
...
}
const { Items } = await PokeTable.build(QueryCommand)
.query(query)
.entities(PokemonEntity, TrainerEntity)
.options(queryOptions)
.send()
It is advised to provide entities
and query
first as they constrain the options
type.
Available options (see the DynamoDB documentation for more details):
Cat. | Option | Type | Default | Description |
---|---|---|---|---|
General | consistent | boolean | false | By default, read operations are eventually consistent (which improves performances and reduces costs).
|
reverse | boolean | false | Specifies the order for index traversal.
| |
capacity | CapacityOption | "NONE" | Determines the level of detail about provisioned or on-demand throughput consumption that is returned in the response.
| |
tableName | string | - | Overrides the | |
Pagination | limit | integer ≥ 1 | - | The maximum number of items to evaluate
|
exclusiveStartKey | Key | - | The primary key of the first item that this operation evaluates. Use the LastEvaluatedKey from the previous operation. | |
maxPages | integer ≥ 1 | 1 | A "meta" option provided by DynamoDB-Toolbox to send multiple requests in a single promise.
| |
Filters | select | SelectOption | - | The strategy for returned attributes. You can retrieve all attributes, specific attributes, the count of matching items, or in the case of an index, some or all of the projected attributes.
|
filters | Record<string, Condition> | - | For each entity name, a condition that must be satisfied in order for evaluated items of this entity to be returned (improves performances but does not reduce costs).
| |
filter | Condition | - | An untyped condition that must be satisfied in order for evaluated items to be returned (improves performances but does not reduce costs).
| |
attributes | string[] | - | To specify a list of attributes to retrieve (improves performances but does not reduce costs).
| |
entityAttrFilter | boolean | true | By default, specifying |
- Strongly consistent
- On index
- Reversed
- Multitenant
const { Items } = await PokeTable.build(QueryCommand)
.query({ partition: 'ashKetchum' })
.entities(PokemonEntity)
.options({ consistent: true })
.send()
const { Items } = await PokeTable.build(QueryCommand)
.query({
index: 'byTrainerId',
partition: 'ashKetchum',
range: { gte: 50 }
})
.entities(PokemonEntity)
.send()
const { Items } = await PokeTable.build(QueryCommand)
.query({ partition: 'ashKetchum' })
.entities(PokemonEntity)
.options({ reverse: true })
.send()
const { Items } = await PokeTable.build(QueryCommand)
.query({ partition: 'ashKetchum' })
.entities(PokemonEntity)
.options({ tableName: `tenant-${tenantId}-pokemons` })
.send()
- Paginated
- All Partition
let lastEvaluatedKey: Record<string, unknown> | undefined
const command = PokeTable.build(QueryCommand).query({
partition: 'ashKetchum'
})
do {
const page = await command
.options({ exclusiveStartKey: lastEvaluatedKey })
.send()
// ...do something with page.Items here...
lastEvaluatedKey = page.LastEvaluatedKey
} while (lastEvaluatedKey !== undefined)
const { Items } = await PokeTable.build(QueryCommand)
.query({ partition: 'ashKetchum' })
// Retrieve all items from the partition (beware of RAM issues!)
.options({ maxPages: Infinity })
.send()
- Filters
- Filter
- Attributes
- Count
- Aborted
const { Items } = await PokeTable.build(QueryCommand)
.query({ partition: 'ashKetchum' })
.entities(PokemonEntity, TrainerEntity)
.options({
filters: {
POKEMONS: { attr: 'pokeType', eq: 'fire' },
TRAINERS: { attr: 'age', gt: 18 }
}
})
.send()
const { Items } = await PokeTable.build(QueryCommand)
.query({ partition: 'ashKetchum' })
.options({
filters: { attr: 'pokeType', eq: 'fire' }
})
.send()
const { Items } = await PokeTable.build(QueryCommand)
.query({ partition: 'ashKetchum' })
.entities(PokemonEntity)
.options({ attributes: ['name', 'type'] })
.send()
const { Count } = await PokeTable.build(QueryCommand)
.query({ partition: 'ashKetchum' })
.options({
select: 'COUNT'
})
.send()
const abortController = new AbortController()
const abortSignal = abortController.signal
const { Items } = await PokeTable.build(QueryCommand)
.query({ partition: 'ashKetchum' })
.send({ abortSignal })
// 👇 Aborts the command
abortController.abort()
Response
The data is returned using the same response syntax as the DynamoDB Query API.
If entities
have been provided, the response Items
are formatted by their respective entities.
You can use the QueryResponse
type to explicitly type an object as a QueryCommand
response object:
import type { QueryResponse } from 'dynamodb-toolbox/table/actions/query'
const queryResponse: QueryResponse<
typeof PokeTable,
// 👇 Query
{ partition: 'ashKetchum' },
// 👇 Optional entities
[typeof PokemonEntity],
// 👇 Optional options
{ attributes: ['name', 'type'] }
> = { Items: ... }