ScanCommand
Performs a Scan Operation on a Table
:
import { ScanCommand } from 'dynamodb-toolbox/table/actions/scan'
const scanCommand = PokeTable.build(ScanCommand)
const params = scanCommand.params()
const { Items } = await scanCommand.send()
Request
.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(ScanCommand)
.entities(PokemonEntity, TrainerEntity)
.send()
.options(...)
Provides additional options:
const { Items } = await PokeTable.build(ScanCommand)
.options({
consistent: true,
limit: 10
...
})
.send()
You can use the ScanOptions
type to explicitly type an object as a ScanCommand
options object:
import type { ScanOptions } from 'dynamodb-toolbox/table/actions/scan'
const scanOptions: ScanOptions<
typeof PokeTable,
// 👇 Optional entities
[typeof PokemonEntity, typeof TrainerEntity]
> = {
consistent: true,
limit: 10,
...
}
const { Items } = await PokeTable.build(ScanCommand)
.options(scanOptions)
.send()
It is advised to provide entities
first as it constrains 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).
|
index | string | - | The name of a secondary index to scan.
| |
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 for 1 page.
|
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 | |
Parallelism | segment | integer ≥ 0 | - | Identifies an individual segment to be scanned by an application worker (zero-based).
|
totalSegment | integer ≥ 1 | - | Represents the total number of segments into which the Scan operation is divided.
|
- Strongly consistent
- On index
- Multitenant
- Aborted
const { Items } = await PokeTable.build(ScanCommand)
.options({
consistent: true
})
.send()
const { Items } = await PokeTable.build(ScanCommand)
.options({
index: 'my-index'
})
.send()
const { Items } = await PokeTable.build(ScanCommand)
.options({
tableName: `tenant-${tenantId}-pokemons`
})
.send()
const abortController = new AbortController()
const abortSignal = abortController.signal
const { Items } = await PokeTable.build(ScanCommand).send({
abortSignal
})
// 👇 Aborts the command
abortController.abort()
- Paginated
- All DB
- All Pokemons
let lastEvaluatedKey: Record<string, unknown> | undefined
const command = PokeTable.build(ScanCommand)
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(ScanCommand)
// Retrieve all items from the table (beware of RAM issues!)
.options({
maxPages: Infinity
})
.send()
const { Items } = await PokeTable.build(ScanCommand)
.entities(PokemonEntity)
// Retrieve all pokemons from the table (beware of RAM issues!)
.options({
maxPages: Infinity
})
.send()
- Filters
- Filter
- Attributes
- Count
const { Items } = await PokeTable.build(ScanCommand)
.entities(PokemonEntity, TrainerEntity)
.options({
filters: {
POKEMONS: { attr: 'pokeType', eq: 'fire' },
TRAINERS: { attr: 'age', gt: 18 }
}
})
.send()
const { Items } = await PokeTable.build(ScanCommand)
.options({
filter: { attr: 'pokeType', eq: 'fire' }
})
.send()
const { Items } = await PokeTable.build(ScanCommand)
.entities(PokemonEntity)
.options({
attributes: ['name', 'type']
})
.send()
const { Count } = await PokeTable.build(ScanCommand)
.options({
select: 'COUNT'
})
.send()
- Segment
- All DB (3 threads)
const { Items } = await PokeTable.build(ScanCommand)
.options({
segment: 1,
totalSegment: 3
})
.send()
const opts = { totalSegment: 3, maxPages: Infinity }
const [
{ Items: segment1 = [] },
{ Items: segment2 = [] },
{ Items: segment3 = [] }
] = await Promise.all([
PokeTable.build(ScanCommand)
.options({ segment: 0, ...opts })
.send(),
PokeTable.build(ScanCommand)
.options({ segment: 1, ...opts })
.send(),
PokeTable.build(ScanCommand)
.options({ segment: 2, ...opts })
.send()
])
const allItems = [...segment1, ...segment2, ...segment3]
Response
The data is returned using the same response syntax as the DynamoDB Scan API.
If entities
have been provided, the response Items
are formatted by their respective entities.
You can use the ScanResponse
type to explicitly type an object as a ScanCommand
response object:
import type { ScanResponse } from 'dynamodb-toolbox/table/actions/scan'
const scanResponse: ScanResponse<
typeof PokeTable,
// 👇 Optional entities
[typeof PokemonEntity],
// 👇 Optional options
{ attributes: ['name', 'type'] }
> = { Items: ... }