zio / zio-dynamodb   0.2.13

Apache License 2.0 Website GitHub

Simple, type-safe, and efficient access to DynamoDB

Scala versions: 3.x 2.13 2.12

ZIO DynamoDB

Simple, type-safe, and efficient access to DynamoDB

Development CI Badge Sonatype Releases Sonatype Snapshots javadoc ZIO DynamoDB

Introduction

ZIO DynamoDB is a library that is used for type-safe, efficient, and boilerplate free access to AWS's DynamoDB service. It provides a type-safe API for many query types and the number of type-safe APIs is expanding. ZIO DynamoDB will automatically batch queries and execute unbatchable queries in parallel.

Under the hood we use the excellent ZIO AWS library for type-safe DynamoDB access, and the awesome ZIO Schema library for schema derived codecs (see here for documentation on how to customise these through annotations).

Installation

To use ZIO DynamoDB, we need to add the following line to our build.sbt file:

libraryDependencies ++= Seq(
  "dev.zio" %% "zio-dynamodb" % "1.0.0-RC2"
)

Cats Effect Interop

To use the new Cats Effect 3 interop module, we need to also add the following line to our build.sbt file:

libraryDependencies ++= Seq(
  "dev.zio" %% "zio-dynamodb-ce" % "1.0.0-RC2"
)

For CE interop examples please see examples sbt module.

Read/write DynamoDB JSON

AWS tools like the CLI and Console read/write a special JSON representation of dynamoDB items. The new experimental optional zio-dynamodb-json module provides a way to read/write this form of JSON. To use this module, we need to also add the following line to our build.sbt file:

libraryDependencies ++= Seq(
  "dev.zio" %% "zio-dynamodb-json" % <SNAPSHOT-VERSION>
)

For examples please see examples sbt module.

Example

For examples please see examples sbt module. Below is Main.scala from that module:

import zio.aws.core.config
import zio.aws.{ dynamodb, netty }
import zio.dynamodb.DynamoDBQuery.{ get, put }
import zio.dynamodb.{ DynamoDBExecutor }
import zio.schema.{ DeriveSchema, Schema }
import zio.ZIOAppDefault
import zio.dynamodb.ProjectionExpression

object Main extends ZIOAppDefault {

  final case class Person(id: Int, firstName: String)
  object Person {
    implicit lazy val schema: Schema.CaseClass2[Int, String, Person] = DeriveSchema.gen[Person]

    val (id, firstName) = ProjectionExpression.accessors[Person]
  }
  val examplePerson = Person(1, "avi")

  private val program = for {
    _      <- put("personTable", examplePerson).execute
    person <- get("personTable")(Person.id.partitionKey === 1).execute
    _      <- zio.Console.printLine(s"hello $person")
  } yield ()

  override def run =
    program.provide(
      netty.NettyHttpClient.default,
      config.AwsConfig.default, // uses real AWS dynamodb
      dynamodb.DynamoDb.live,
      DynamoDBExecutor.live
    )
}

For examples on how to use the DynamoDBLocal in memory database please see the integration tests and DynamoDBLocalMain . Note before you run these you must first run the DynamoDBLocal docker container using the provided docker-compose file:

docker-compose -f docker/docker-compose.yml up -d

Don't forget to shut down the container after you have finished

docker-compose -f docker/docker-compose.yml down

Resources

  • Introducing ZIO DynamoDB by Avinder Bahra & Adam Johnson - DynamoDB powers many cloud-scale applications, with its robust horizontal scalability and uptime. Yet, interacting with the Java SDK is error-prone and tedious. In this presentation, Avinder Bahra presents ZIO DynamoDB, a new library by Avi and Adam Johnson designed to make interacting with DynamoDB easy, type-safe, testable, and productive.
  • Introducing The ZIO DynamoDB Type-Safe API by Avinder Bahra - Last year, Adam Johnson and Avinder released ZIO DynamoDB, a new Scala library that significantly reduces boilerplate when compared to working directly with AWS client libraries. However, there was still work to be done to improve type safety. In this talk, Avinder introduces a new type-safe API that can prevent many errors at compile time while remaining user-friendly.

Documentation

Learn more on the ZIO DynamoDB homepage!

Contributing

For the general guidelines, see ZIO contributor's guide.

Code of Conduct

See the Code of Conduct

Support

Come chat with us on Badge-Discord.

License

License