# Multi-Model Requests

## General

### Creation

Multi-model requests are created with the `new`  keyword.

{% hint style="info" %}
You can provide a DynamoDB client instance when creating a new multi model request:`new BatchGetRequest(Person, myDynamoDBClient)`&#x20;
{% endhint %}

### 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

{% embed url="<https://shiftcode.github.io/dynamo-easy/modules/multi_model_requests_transact_get.html>" %}
technical api doc
{% endembed %}

{% embed url="<https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchGetItem.html>" %}
<https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API\\_BatchGetItem.html>
{% endembed %}

{% code title="batch-get-request.example.ts" %}

```typescript
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)
  })
```

{% endcode %}

## BatchWrite

{% embed url="<https://shiftcode.github.io/dynamo-easy/modules/multi_model_requests_batch_write.html>" %}
technical api doc
{% endembed %}

{% embed url="<https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html>" %}
<https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API\\_BatchWriteItem.html>
{% endembed %}

{% code title="batch-write-request.example.ts" %}

```typescript
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)
  })
```

{% endcode %}

## TransactGet

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

{% embed url="<https://shiftcode.github.io/dynamo-easy/modules/multi_model_requests_transact_get.html>" %}
technical api doc
{% endembed %}

{% embed url="<https://goo.gl/qktiQb>" %}
<https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API\\_TransactGetItems.html>
{% endembed %}

{% code title="transact-get-request.example.ts" %}

```typescript
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
  })
```

{% endcode %}

## TransactWrite

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

{% embed url="<https://goo.gl/8NHwFd>" %}
technical api doc
{% endembed %}

{% embed url="<https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_TransactWriteItems.html>" %}
<https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API\\_TransactWriteItems.html>
{% endembed %}

{% code title="transact-write-request.example.ts" %}

```typescript
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'))

```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://shiftcode.gitbook.io/dynamo-easy/api/multi-model-requests.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
