Skip to main content

Transactions

TransactGet

DynamoDB-Toolbox exposes the GetTransaction actions to perform TransactGetItems operations.

TransactWrite

DynamoDB-Toolbox exposes the following actions to perform TransactWriteItems operations:

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

import { execute } from 'dynamodb-toolbox/entity/actions/transactWrite'

const put = PokemonEntity.build(PutTransaction).item(...)
const update = PokemonEntity.build(UpdateTransaction).item(...)
const del = PokemonEntity.build(DeleteTransaction).key(...)
const check = PokemonEntity.build(ConditionCheck).key(...).condition(...)

await execute(put, update, del, check, ...otherTransactions)
caution

Only one transaction per item is supported. For instance, you cannot run a ConditionCheck and an UpdateTransaction on the same item: You can, however, condition the UpdateTransaction itself.

Options

The execute function accepts an additional object as a first argument for operation-level options:

await execute(options, ...writeTransactions)

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".
metricsMetricsOption"NONE"Determines whether item collection metrics are returned.

Possible values are "NONE" and "SIZE".
clientRequestTokenstring-Providing a clientRequestToken makes the execution idempotent, meaning that multiple identical calls have the same effect as one single call.
documentClientDocumentClient-By default, the documentClient attached to the Table of the first WriteTransaction is used to execute the operation.

Use this option to override this behavior.
Examples
const { ConsumedCapacity } = await execute(
{ capacity: 'TOTAL' },
...writeTransactions
)

Response

The data is returned using the same response syntax as the DynamoDB TransactWriteItems API, with an additional ToolboxItems property, which allows you to retrieve the items generated by DynamoDB-Toolbox in PutTransactions and UpdateTransactions:

const { ToolboxItems } = await execute(
putTransaction,
deleteTransaction,
conditionCheck,
updateTransaction
)

const [
putPokemon,
// 👇 Both undefined
_,
__,
updatedPokemon
] = ToolboxItems

// 👇 Great for auto-generated attributes
const createdTimestamp = putPokemon.created
const modifiedTimestamp = updatedPokemon.modified