Skip to content

Commit

Permalink
Document conditional operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Wills committed Feb 7, 2017
1 parent 9612ddd commit efde186
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/main/resources/microsite/data/menu.yml
Expand Up @@ -21,6 +21,9 @@ options:
- title: Batch Operations
url: batch-operations.html

- title: Conditional Operations
url: conditional-operations.html

- title: Using Indexes
url: using-indexes.html

Expand Down
2 changes: 1 addition & 1 deletion src/main/tut/asynchronous.md
@@ -1,7 +1,7 @@
---
layout: docs
title: Asynchronous Requests
position: 5
position: 6
---

## Non-blocking requests
Expand Down
47 changes: 47 additions & 0 deletions src/main/tut/conditional-operations.md
@@ -0,0 +1,47 @@
---
layout: docs
title: Conditional Operations
position: 3
---

## Conditional Operations

Modifying operations ([Put](operations.html#put-and-get), [Delete](operations.html#delete),
[Update](operations.html#update)) can be performed conditionally, so that they
only have an effect if some state of the DynamoDB table is true at the time of
execution.

```tut:silent
import com.gu.scanamo._
import com.gu.scanamo.syntax._
import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType._
val client = LocalDynamoDB.client()
case class Gremlin(number: Int, name: String, wet: Boolean, friendly: Boolean)
```
```tut:book
val gremlinsTable = Table[Gremlin]("gremlins")
LocalDynamoDB.withTable(client)("gremlins")('number -> N) {
val ops = for {
_ <- gremlinsTable.putAll(
Set(Gremlin(1, "Gizmo", false, true), Gremlin(2, "George", true, false)))
// Only `put` Gremlins if not already one with the same number
_ <- gremlinsTable.given(not(attributeExists('number)))
.put(Gremlin(2, "Stripe", false, true))
_ <- gremlinsTable.given(not(attributeExists('number)))
.put(Gremlin(3, "Greta", true, true))
allGremlins <- gremlinsTable.scan()
_ <- gremlinsTable.given('wet -> true)
.delete('number -> 1)
_ <- gremlinsTable.given('wet -> true)
.delete('number -> 2)
_ <- gremlinsTable.given('wet -> true)
.update('number -> 1, set('friendly -> false))
_ <- gremlinsTable.given('wet -> true)
.update('number -> 3, set('friendly -> false))
remainingGremlins <- gremlinsTable.scan()
} yield (allGremlins, remainingGremlins)
Scanamo.exec(client)(ops)
}
```

More examples can be found in the [Table ScalaDoc](/scanamo/latest/api/com/gu/scanamo/Table.html#given[T](condition:T)(implicitevidence$2:com.gu.scanamo.query.ConditionExpression[T]):com.gu.scanamo.query.ConditionalOperation[V,T]).
2 changes: 1 addition & 1 deletion src/main/tut/dynamo-format.md
@@ -1,7 +1,7 @@
---
layout: docs
title: DynamoFormat
position: 4
position: 5
---

## DynamoFormat
Expand Down
4 changes: 2 additions & 2 deletions src/main/tut/operations.md
Expand Up @@ -17,8 +17,8 @@ Scanamo supports all the DynamoDB operations that interact with individual items
* [Query](#query) for retrieving all elements with a given hash-key and a range key that matches
some criteria

Scanamo also supports [batched operations](batch-operations.html) and queries against
[secondary indexes](using-indexes.html).
Scanamo also supports [batched operations](batch-operations.html), [conditional operations](conditional-operations.html)
and queries against [secondary indexes](using-indexes.html).

### Put and Get

Expand Down
2 changes: 1 addition & 1 deletion src/main/tut/using-indexes.md
@@ -1,7 +1,7 @@
---
layout: docs
title: Using Indexes
position: 3
position: 4
---

## Using Indexes
Expand Down

0 comments on commit efde186

Please sign in to comment.