Skip to content

Adapter: Batch builders

Eugene Lazutkin edited this page Apr 23, 2026 · 1 revision

Adapter: Batch builders

See also (AWS JS SDK v3): BatchWriteCommand · TransactWriteCommand · BatchGetCommand. Vocabulary: Concepts.

The Adapter exposes make*() builders that return discriminated descriptors {action, params} (or {action, adapter, params} for makeGet). Compose with applyBatch / applyTransaction from Batch and transactions to issue one DynamoDB call covering many ops.

Builder Returns Notes
makeGet(key, fields?, params?) {action: 'get', adapter, params} For use with getBatch / getTransaction
makeCheck(key, params?) {action: 'check', params} Condition-only; for use with applyTransaction
makePost(item, options?) {action: 'put', params} Adds attribute_not_exists
makePut(item, options?) {action: 'put', params} options.force skips existence check
makePatch(key, patch, options?) {action: 'patch', params} Builds UpdateExpression
makeDelete(key, options?) {action: 'delete', params}

All builders run the relevant hooks (prepare, validateItem, updateInput) and pass the result through cleanParams. makePost / makePut / makePatch / makeDelete accept {returnFailedItem: true} to surface the conflicting item on a failed conditional check — see Adapter: CRUD methods § returnFailedItem.

Example: composite transaction

import {applyTransaction} from 'dynamodb-toolkit/batch';

// Atomically: post a child planet AND increment a parent's count
const child = await adapter.makePost({name: 'Mustafar', parent: 'Sol'});
const parentBump = await adapter.makePatch(
  {name: 'Sol'},
  {},
  {arrayOps: [{op: 'add', path: 'children', value: 1}]}
);

await applyTransaction(adapter.client, child, parentBump);

Example: batch read across two tables

import {getBatch} from 'dynamodb-toolkit/batch';

const planetsAdapter = new Adapter({client, table: 'planets', keyFields: ['name']});
const moonsAdapter = new Adapter({client, table: 'moons', keyFields: ['planet', 'name']});

const results = await getBatch(
  client,
  await planetsAdapter.makeGet({name: 'Endor'}),
  await moonsAdapter.makeGet({planet: 'Endor', name: 'Sanctuary'})
);
// → [{table: 'planets', item: {...}}, {table: 'moons', item: {...}}]

When to use builders vs direct CRUD

  • Single op? Call adapter.put(...) / adapter.patch(...) directly. Auto-upgrade and dispatch are handled.
  • Multiple ops in one call? Build descriptors with make*(), then dispatch with applyBatch / applyTransaction / getBatch / getTransaction.
  • Custom consistency checks? Use makeCheck(key, {ConditionExpression: ...}) and bundle with the main op via applyTransaction.

Clone this wiki locally