Skip to main content

BatchGetCommand

Groups one or several BatchGetRequest from the Table entities to execute a BatchGetItem operation.

BatchGetItem operations can affect multiple tables, so BatchGetCommands do not have a .send(...) method. Instead, they should be performed via the dedicated execute function:

import {
BatchGetCommand,
execute
} from 'dynamodb-toolbox/table/actions/batchGet'
import { BatchGetRequest } from 'dynamodb-toolbox/entity/actions/batchGet'

const pokeCmd = PokeTable.build(BatchGetCommand).requests(
PokemonEntity.build(BatchGetRequest).key(pikachuKey),
PokemonEntity.build(BatchGetRequest).key(charizardKey)
)

const params = pokeCmd.params()

const otherCmd = OtherTable.build(BatchGetCommand).requests(
TrainerEntity.build(BatchGetRequest).key(ashKey)
)

const { Responses } = await execute(pokeCmd, otherCmd)

// 🙌 Correctly typed!
const [[pikachu, charizard], [ash]] = Responses
note

Note that batch operations are more efficient than running their equivalent commands in parallel, but do not reduce costs.

Request

.requests(...)

(required)

The BatchGetRequests to execute.

Note that the requests can be provided as tuples or arrays (the output is typed accordingly):

Examples
const command = PokeTable.build(BatchGetCommand).requests(
PokemonEntity.build(BatchGetRequest).key(pikachuKey),
PokemonEntity.build(BatchGetRequest).key(charizardKey),
...
)

.options(...)

Provides additional table-level options:

const command = PokeTable.build(BatchGetCommand).options({
consistent: true,
...
})

You can use the BatchGetCommandOptions type to explicitly type an object as a BatchGetCommand options object:

import type { BatchGetCommandOptions } from 'dynamodb-toolbox/table/actions/batchGet'

const batchGetOptions: BatchGetCommandOptions<
// 👇 Optional entities
[typeof PokemonEntity, typeof TrainerEntity]
> = {
consistent: true,
...
}

const command = PokeTable.build(BatchGetCommand)
.requests(...)
.options(batchGetOptions)
info

It is advised to provide requests first as they constrain the options type.

Available options (see the DynamoDB documentation for more details):

OptionTypeDefaultDescription
consistentbooleanfalseBy default, read operations are eventually consistent (which improves performances and reduces costs).

Set to true to use strongly consistent reads.
attributesstring[]-To specify a list of attributes to retrieve (improves performances but does not reduce costs).

Requires requests. Paths must be common to all requested entities.

See the PathParser action for more details on how to write attribute paths.
tableNamestring-Overrides the Table name. Mostly useful for multitenancy.
Examples
const command = PokeTable.build(BatchGetCommand)
.requests(...)
.options({
consistent: true
})

Execution

import { execute } from 'dynamodb-toolbox/table/actions/batchGet'

await execute(...batchGetCommands)
warning

Only one BatchGetCommand per Table is supported.

Note that the commands can be provided as tuples or arrays (the output is typed accordingly):

Examples
await execute(
PokeTable.build(BatchGetCommand).requests(...),
OtherTable.build(BatchGetCommand).requests(...),
...
)

Options

The execute function accepts an additional object as a first argument for operation-level options, as well as DocumentClient options such as abortSignal:

await execute(options, ...batchGetCommands)

Available options (see the DynamoDB documentation for more details):

OptionTypeDefaultDescription
capacityCapacityOption"NONE"Determines the level of detail about provisioned or on-demand throughput consumption that is returned in the response.

Possible values are "NONE", "TOTAL" and "INDEXES".
documentClientDocumentClient-By default, the documentClient attached to the Table of the first BatchGetCommand is used to execute the operation.

Use this option to override this behavior.
maxAttemptsinteger ≥ 11A "meta" option provided by DynamoDB-Toolbox to retry failed requests in a single promise.

Note that Infinity is a valid (albeit dangerous) option.
Examples
const { ConsumedCapacity } = await execute(
{ capacity: 'TOTAL' },
...batchGetCommands
)

Response

The data is returned using the same response syntax as the DynamoDB BatchGetItem API, except for the Responses property.

Instead of a map of arrays indexed by table name, DynamoDB-Toolbox returns an array of arrays, where each sub-array contains the items of a BatchGetCommand. Both commands and items are returned in the same order they were provided, and items are formatted by their respective entities.

note

The official documentation states that "DynamoDB does not return items in any particular order", but DynamoDB-Toolbox handles the re-ordering for you 😘

This format is also better for type inference, as DynamoDB-Toolbox does not statically know your table names.