Multi-Model Requests

There are different requests you can use to read from or write to different tables.

General

Creation

Multi-model requests are created with the new keyword.

You can provide a DynamoDB client instance when creating a new multi model request:new BatchGetRequest(Person, myDynamoDBClient)

Params

Every request instance contains the public params property where all params for the request are written to. You might want to use it for debugging purpose but are also able to write on it (eg. non-covered features).

Consumed Capacity

You might want to receive the consumed capacity which can be achieved by applying the returnConsumedCapacity('INDEXES' | 'TOTAL') method and the usage of execFullResponse() instead of exec().

Execution

All requests provide at least two execution methods:

  • exec() returns a promise which will resolve to the requested items or void if no items were requested

  • execFullResponse() returns a promise which will resolve to the full response DynamoDB returns, but response.Items are still mapped for you.

BatchGet

batch-get-request.example.ts
import { BatchGetRequest, BatchGetResponse } from '@shiftcoders/dynamo-easy'
import { AnotherModel, Person } from '../models'

const keysToFetch: Array<Partial<Person>> = [{ id: 'vogelsw' }]
const otherKeysToFetch: Array<Partial<AnotherModel>> = [{ propA: 'Foo', propB: 'Bar' }]

new BatchGetRequest()
  .forModel(Person, keysToFetch)
  .forModel(AnotherModel, otherKeysToFetch)
  .exec()
  .then((result: BatchGetResponse) => {
    console.log('items fetched from example table', result.tableNameOfExampleModel)
    console.log('items fetched from another table', result.tableNameOfAnotherModel)
  })

BatchWrite

batch-write-request.example.ts
import { BatchWriteRequest } from '@shiftcoders/dynamo-easy'
import { AnotherModel, Person } from '../models'

const keysToDelete: Array<Partial<Person>> = [{ id: 'vogelsw' }]
const otherKeysToDelete: Array<Partial<AnotherModel>> = [{ propA: 'Foo', propB: 'Bar' }]
const objectsToPut: AnotherModel[] = [
  { propA: 'foo', propB: 'bar', updated: new Date() },
  { propA: 'foo2', propB: 'bar2', updated: new Date() },
]

new BatchWriteRequest()
  .returnConsumedCapacity('TOTAL')
  .delete(Person, keysToDelete)
  .delete(AnotherModel, otherKeysToDelete)
  .put(AnotherModel, objectsToPut)
  .execFullResponse()
  .then(resp => {
    console.log(resp.ConsumedCapacity)
  })

TransactGet

Execute transactional read operations by providing one or multiple models + keys.

transact-get-request.example.ts
import { TransactGetRequest } from '@shiftcoders/dynamo-easy'
import { AnotherModel, Person } from '../models'

new TransactGetRequest()
  .returnConsumedCapacity('TOTAL')
  .forModel(Person, { id: 'vogelsw' })
  .forModel(AnotherModel, { propA: 'Foo', propB: 'Bar' })
  .exec()
  .then(result => {
    console.log(result[0]) // Person item
    console.log(result[1]) // AnotherModel item
  })

TransactWrite

Execute transactional write operations by providing one or multiple transact items (TransactConditionCheck, TransactDelete, TransactPut, TransactUpdate).

transact-write-request.example.ts
import {
  attribute,
  TransactConditionCheck,
  TransactDelete,
  TransactPut,
  TransactUpdate,
  TransactWriteRequest,
} from '@shiftcoders/dynamo-easy'
import { AnotherModel, Person } from '../models'

const objectToPut: AnotherModel = { propA: 'Foo', propB: 'Bar', updated: new Date() }

new TransactWriteRequest()
  .transact(
    new TransactConditionCheck(Person, 'vogelsw').onlyIf(attribute('yearOfBirth').gte(1958)),
    new TransactDelete(AnotherModel, 'Foo', 'Bar'),
    new TransactPut(AnotherModel, objectToPut),
    new TransactUpdate(Person, 'vogelsw').updateAttribute('yearOfBirth').set(1984),
  )
  .exec()
  .then(() => console.log('done'))

Last updated