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