Batch Operations

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

import org.scanamo._
import org.scanamo.syntax._
import org.scanamo.generic.auto._
import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType._
 
val client = LocalDynamoDB.syncClient()
val scanamo = Scanamo(client)

LocalDynamoDB.createTable(client)("lemmings")("role" -> S)

case class Lemming(role: String, number: Long)
val lemmingsTable = Table[Lemming]("lemmings")
// lemmingsTable: Table[Lemming] = Table("lemmings")
val ops = for {
  _ <- lemmingsTable.putAll(Set(
    Lemming("Walker", 99), Lemming("Blocker", 42), Lemming("Builder", 180)
  ))
  bLemmings <- lemmingsTable.getAll("role" in Set("Blocker", "Builder"))
  _ <- lemmingsTable.deleteAll("role" in Set("Walker", "Blocker"))
  survivors <- lemmingsTable.scan()
} yield (bLemmings, survivors)
// ops: cats.free.Free[org.scanamo.ops.ScanamoOpsA, (Set[Either[DynamoReadError, Lemming]], List[Either[DynamoReadError, Lemming]])] = FlatMapped(
//   FlatMapped(
//     Suspend(
//       BatchWrite(
//         BatchWriteItemRequest(RequestItems={lemmings=[WriteRequest(PutRequest=PutRequest(Item={number=AttributeValue(N=99), role=AttributeValue(S=Walker)})), WriteRequest(PutRequest=PutRequest(Item={number=AttributeValue(N=42), role=AttributeValue(S=Blocker)})), WriteRequest(PutRequest=PutRequest(Item={number=AttributeValue(N=180), role=AttributeValue(S=Builder)}))]})
//       )
//     ),
//     org.scanamo.ScanamoFree$$$Lambda$11144/930290667@1d5f95bd
//   ),
//   <function1>
// )
val (bLemmings, survivors) = scanamo.exec(ops)
// bLemmings: Set[Either[DynamoReadError, Lemming]] = Set(
//   Right(Lemming("Builder", 180L)),
//   Right(Lemming("Blocker", 42L))
// )
// survivors: List[Either[DynamoReadError, Lemming]] = List(
//   Right(Lemming("Builder", 180L))
// )
bLemmings.flatMap(_.toOption)
// res1: Set[Lemming] = Set(Lemming("Builder", 180L), Lemming("Blocker", 42L))
survivors.flatMap(_.toOption)
// res2: List[Lemming] = List(Lemming("Builder", 180L))