Batch Operations

Many operations against Dynamo can be performed in batches. Scanamo has support for putting, getting and deleting in batches

import com.gu.scanamo._
import com.gu.scanamo.syntax._

import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
 
val client = LocalDynamoDB.client()
import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType._
LocalDynamoDB.createTable(client)("lemmings")('role -> S)

case class Lemming(role: String, number: Long)
val lemmingsTable = Table[Lemming]("lemmings")
// lemmingsTable: com.gu.scanamo.Table[Lemming] = Table(lemmings)

val ops = for {
  _ <- lemmingsTable.putAll(Set(
    Lemming("Walker", 99), Lemming("Blocker", 42), Lemming("Builder", 180)
  ))
  bLemmings <- lemmingsTable.getAll('role -> Set("Blocker", "Builder"))
  _ <- lemmingsTable.deleteAll('role -> Set("Walker", "Blocker"))
  survivors <- lemmingsTable.scan()
} yield (bLemmings, survivors)
// ops: cats.free.Free[com.gu.scanamo.ops.ScanamoOpsA,(Set[Either[com.gu.scanamo.error.DynamoReadError,Lemming]], List[Either[com.gu.scanamo.error.DynamoReadError,Lemming]])] = Free(...)

val (bLemmings, survivors) = Scanamo.exec(client)(ops)
// bLemmings: Set[Either[com.gu.scanamo.error.DynamoReadError,Lemming]] = Set(Right(Lemming(Builder,180)), Right(Lemming(Blocker,42)))
// survivors: List[Either[com.gu.scanamo.error.DynamoReadError,Lemming]] = List(Right(Lemming(Builder,180)))

import cats.syntax.either._
// import cats.syntax.either._

bLemmings.flatMap(_.toOption)
// res1: scala.collection.immutable.Set[Lemming] = Set(Lemming(Builder,180), Lemming(Blocker,42))

survivors.flatMap(_.toOption)
// res2: List[Lemming] = List(Lemming(Builder,180))