Using Indexes

Scanamo supports scanning and querying global secondary indexes. In the following example, we create and use a table called transport with a hash key of mode and range key of line and a global secondary called colour-index with only a hash key on the colour attribute:

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

case class Transport(mode: String, line: String, colour: String)
val transport = Table[Transport]("transport")
val colourIndex = transport.index("colour-index")

val client = LocalDynamoDB.client()
import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType._
LocalDynamoDB.withTableWithSecondaryIndex(client)("transport", "colour-index")('mode -> S, 'line -> S)('colour -> S) {
  val operations = for {
    _ <- transport.putAll(Set(
      Transport("Underground", "Circle", "Yellow"),
      Transport("Underground", "Metropolitan", "Maroon"),
      Transport("Underground", "Central", "Red")))
    maroonLine <- colourIndex.query('colour -> "Maroon")
  } yield maroonLine.toList
  Scanamo.exec(client)(operations)
}
// res0: List[Either[com.gu.scanamo.error.DynamoReadError,Transport]] = List(Right(Transport(Underground,Metropolitan,Maroon)))