What is Zio
ZIO is a library for Scala programming language that provides a pure, composable, and type-safe approach to error handling and asynchronous programming. ZIO provides a lot of tools for developers to write applications in a clean, concise, and functional manner. Zlayer is a module in ZIO that provides abstractions for building and composing modular applications.
In this blog, we will be exploring the ZioDynamoDB wrapper.
What is DynamoDB
DynamoDB uses a NoSQL database model, which is nonrelational, allowing documents, graphs, and columnar among its data models. A user stores data in DynamoDB tables, then interact with it via GET and PUT queries, which are read and write operations, respectively. DynamoDB supports basic CRUD operations and conditional operations. Each DynamoDB query is executed by a primary key identified by the user, which uniquely identifies each item.
How To Use Zio DynamoDB wrapper
- Add the Required Library Dependency
- Create a Connection
- Create a function to Insert DynamoDb Record
Add the Library Dependency:
libraryDependencies ++= Seq(
"dev.zio" %% "zio-dynamodb" % "0.2.0-RC2"
)
Create a Connection:
So in Zio, we can create an Async in Zio and pick the configurations from the application.conf file or we can use the default configuration in the system.
//Create an Async Connection
val client = DynamoDbAsyncClient
.builder()
.region(Region.US_EAST_1)
.build()
The client here is an Async Client and It will get the AWS credentials from the application.conf file.
//Pick the Default Configuration
Inputfunction.provide(
netty.NettyHttpClient.default,
config.AwsConfig.default,
dynamodb.DynamoDb.live,
DynamoDBExecutor.live
)
Here the .provide function will provide a Layer to the specific function to which we apply the function on. Here the InputFunction will get the layer with DynamoDBExecutor.live which will access the DynamoDb service which in turn requires the layer dynamodb.DynamoDb.live layer to get the AWS config, which accesses the config.AwsConfig.default to get the default AWS configuration from the system which requires an HTTP client and here we have netty.NettyHttpClient.default.
Function To Insert Dynamo Db Record
case class TransactionDetails(userDetails: String, status: String)
private object TransactionDetails {
implicit lazy val schema: Schema[TransactionDetails] = DeriveSchema.gen[TransactionDetails]
}
private def InputFunction(transactionDetails: TransactionDetails): ZIO[DynamoDBExecutor, Throwable, Unit] = {
val input: TransactionDetails = TransactionDetails(transactionDetails.userDetails, transactionDetails.status)
for {
_ <- put("newTable", input).execute
bookingDetailsSuccess <- get[TransactionDetails]("newTable", PrimaryKey("status" -> "success")).execute
bookingDetailsFail <- get[TransactionDetails]("newTable", PrimaryKey("status" -> "fail")).execute
_ <- zio.Console.printLine(s"Inputted Data $bookingDetailsSuccess")
_ <- zio.Console.printLine(s"Inputted Data $bookingDetailsFail")
} yield ()
}
Conclusion:
The Zio DynamoDB Library makes It much easier to persist data with fewer lines of code than in Java or Scala. To read more about What is a Dynamo DB you can access this link here:
DynamoDB Core Concepts DynamoDB 5 Minute Read
Also If you wish to learn more about ZIO DynamoDB you can access the youtube video here: