# Model Requests

## General

### Creation

All model requests are created by using the corresponding method on a DynamoStore instance. A new request instance for the related model/table will be returned.

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

## Get

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

{% code title="get.snippet.ts" %}

```typescript
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'

new DynamoStore(Person)
  .get('wernerv')        // returns an instance of GetRequest
  .consistentRead(true)  // sets params.ConsistentRead = true
  .exec()                // returns a Promise<Person|null>
  .then(obj => console.log(obj))

```

{% endcode %}

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

## Put

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

{% code title="put.snippet.ts" %}

```typescript
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'

const objectToPut: Person = {
  id: 'vogelsw',
  name: 'Werner Hans Peter Vogels',
  yearOfBirth: 1958,
} // object literal or new Person(...)

new DynamoStore(Person)
  .put(objectToPut)
  .ifNotExists()
  .exec()
  .then(() => console.log('done'))
```

{% endcode %}

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

## Update

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

{% tabs %}
{% tab title="Simple" %}
{% code title="update-simple.snippet.ts" %}

```typescript
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { AnotherModel } from '../models'

const oneHourAgo = new Date(Date.now() - 1000 * 60 * 60)

new DynamoStore(AnotherModel)
  .update('myPartitionKey', 'mySortKey')
  .updateAttribute('propC').set('newValue')
  .updateAttribute('updated').set(new Date())
  .onlyIfAttribute('updated').lt(oneHourAgo)
  .exec()
  .then(() => console.log('done'))
```

{% endcode %}
{% endtab %}

{% tab title="Complex" %}
{% code title="update-complex.snippet.ts" %}

```typescript
import { attribute, DynamoStore, or, update } from '@shiftcoders/dynamo-easy'
import { AnotherModel } from '../models'

const index = 3
const oneHourAgo = new Date(Date.now() - 1000 * 60 * 60)

new DynamoStore(AnotherModel)
  .update('myPartitionKey', 'mySortKey')
  .operations(
    update(`myNestedList[${index}].propertyX`).set('value'),
    update('updated').set(new Date()),
  )
  .onlyIf(
    or(
      attribute('id').attributeNotExists(), // item not existing
      attribute('updated').lt(oneHourAgo), // or was not updated in the last hour
    ),
  )
  .returnValues('ALL_OLD')
  .exec()
  .then(oldVal => console.log('old value was:', oldVal))
```

{% endcode %}
{% endtab %}
{% endtabs %}

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

## Delete

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

{% code title="delete.snippet.ts" %}

```typescript
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'

new DynamoStore(Person)
  .delete('vogelsw')
  .onlyIfAttribute('yearOfBirth').lte(1958)
  .exec()
  .then(() => console.log('done'))
```

{% endcode %}

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

## Query

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

{% code title="query.snippet.ts" %}

```typescript
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { AnotherModel } from '../models'

new DynamoStore(AnotherModel)
  .query()
  .wherePartitionKey('2018-01')
  .whereSortKey().beginsWith('a')
  .execSingle()
  .then(r => console.log('first found item:', r))
```

{% endcode %}

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

## Scan

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

{% code title="scan.snippet.ts" %}

```typescript
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'

new DynamoStore(Person)
  .scan()
  .whereAttribute('yearOfBirth').equals(1958)
  .execFetchAll()
  .then(res => console.log('ALL items with yearOfBirth == 1958', res))
```

{% endcode %}

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

## BatchGet

This is a special implementation from BatchGetRequest which only allows to read from a single table.

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

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

```typescript
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'

new DynamoStore(Person)
  .batchGet([{ id: 'a' }, { id: 'b' }])
  .exec()
  .then(res => console.log('fetched items:', res))
```

{% endcode %}

{% 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 %}

## BatchWrite

This is a special implementation from BatchWriteRequest which only allows to write to a single table

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

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

```typescript
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'

new DynamoStore(Person)
  .batchWrite()
  .delete([{ id: 'a' }, { id: 'b' }])
  .put([{ id: 'vogelsw', name: 'Werner Hans Peter Vogels', yearOfBirth: 1958 }])
  .exec()
  .then(() => console.log('item a, b deleted; werner vogels added'))
```

{% endcode %}

{% 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 %}

## TransactGet

This is a special implementation from TransactGetRequest which only allows to read from a single table.

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

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

```typescript
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'

new DynamoStore(Person)
  .transactGet([{ id: 'a' }, { id: 'b' }])
  .exec()
  .then(() => console.log('transactionally read a and b'))
```

{% endcode %}

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


---

# 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/dynamo-store/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.
