> For the complete documentation index, see [llms.txt](https://shiftcode.gitbook.io/dynamo-easy/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://shiftcode.gitbook.io/dynamo-easy/api/expressions-conditions-update.md).

# Expressions (conditions, update)

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

## Condition

{% embed url="<https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ConditionExpressions.html>" %}
<https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ConditionExpressions.html>
{% endembed %}

Condition Expressions are used for two purposes:&#x20;

* pre condition for write operations (delete, put, update) `onlyIf()`,`onlyIfAttribute()`
* filter condition for read operations (query, scan ) `where()`,`whereAttribute()`

`onlyIfAttribute()` and `whereAttribute()` provide a chainable way to add your conditions, all conditions are combined with `and` operator:

```typescript
scanReq
    .whereAttribute('myProp').eq('foo')
    .whereAttribute('otherProp').eq('bar')
```

to either use `not` or `or` operator you need to use the `onlyIf()` resp. `where()`methods:

{% code title="nor-not-and-condition.snippet.ts" %}

```typescript
import { and, attribute, DynamoStore, not, or } from '@shiftcoders/dynamo-easy'
import { Person } from '../models'

const personStore = new DynamoStore(Person)
personStore.delete('vogelsw')
  .onlyIf(
    or(
      and(
        attribute('myProp').eq('foo'),
        attribute('otherProp').eq('bar')
      ),
      not(
        attribute('otherProp').eq('foo bar')
      ),
    )
  )
```

{% endcode %}

For better typings the `attribute2` function is provided (only useful for top level properties)

{% code title="attribute2-condition.snippet.ts" %}

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

const personStore = new DynamoStore(Person)

personStore.delete('volgelsw')
  .onlyIf(
    attribute2(Person, 'yearOfBirth').eq(1958),
    // attributes2 only accepts a propertyName from Person
    // eq only accepts a value of type Person.yearOfBirth (number in this case)
  )
```

{% endcode %}

{% hint style="info" %}
The same methods also exists for all transact operations (used in `TransactWriteRequest`)
{% endhint %}

## Update

{% embed url="<https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html>" %}
<https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html>
{% endembed %}

As for condition expressions, there are two ways to define the update expressions for an update request:

the chainable way:

```typescript
updateReq
    .update('name').set('Werner Vogels')
```

the not-that-chainy way:

{% code title="update-expression.snippet.ts" %}

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

const personStore = new DynamoStore(Person)
personStore.update('vogelsw')
  .operations(
    update('name').set('Werner Vogels'),
    update('yearOfBirth').set(1984),
  )
```

{% endcode %}

> *the `operations` method primarily exists for more flexibility*

There also exists the `update2(ModelClazz, propertyName)` function as equivalent to `attribute2()` for better typing.

{% hint style="info" %}
The same methods also exists for `TransactUpdate` (used in `TransactWriteRequest`)
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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/expressions-conditions-update.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.
