Scala CockroachDB/Postgres Client with Skunk

Functional Ops

(λx.x)eranga
Effectz.AI
3 min readDec 13, 2021

--

Background

Skunk is purely functional, non-blocking Postgres library for Scala which designed with cats, cats-effect, scodec, and fs2 libraries. Skunk built with native Postgres API without using JDBC. So it has really powerful features set. Since Skunk built with native Postgres APIs, it would be an ideal library to build GraalVM like native compiler-based applications(you can get rid of lot of compiler issues and ignore reflection configs with Skunk on GraalVM).

In my previous posts(post 1, post 2) I have discussed about using doobie which is another purely functional database library for Scala. In this post I’m gonna discuss about using Skunk to build CockroachDB/Postgres client applications. Since CockroackDB is compatible with Postgres API, we can use Skunk to write CockroachDB client applications. All the source codes which related to this post available in gitlab. Please clone the repo and continue the post. Also please watch this nice video by Rob Norris(the creator of Skunk), he talked about pure functional database programming without JDBC, which is Skunk.

Deploy CockroachDB

In this scenario I’m using CockroachDB to run the Skunk application. Since CockroachDB compatible with Postgres API, you can use same application with Postgres as well. I have deployed CockroachDB single node cluster with Docker. Read more information about deploying CockroachDB cluster from my previous post. Following is the docker-compose deployment.

In here I have setup the cluster in insecure mode. So it does not required SSL/Password authentication to interact with the cluster. To work compatible with Postgres we need to do few configurations in the cluster now. The configurations can be done via connecting to the CockroachDB docker container.

Database Setup

I have a database name rahasak and table named toggles. The toggles table has different types of columns uuid, varchar, text, timestamp. The table schema is related to a feature toggle service that I have built(read about it if you are interested). Following is the way to create the database and table schema via connecting to the CockroachDB Sql API inside the Docker container.

Skunk Dependency

Following is the build.sbt with the Skunk dependencies. Skunk using cats-effects, fs2 libraries behind the scene.

Skunk Queries

There is a case class mapping for database table toggles. ToggleRepo defines the abstract CURD functions for toggles tables. These functions wrapped with cats-effects IO monad.

ToggleReoImpl implements the CURD operations of the toggles table via Skunk library. It used various query operations of Skunk(queries, commands, encoders, decoders) to implement the CURD functions. The functionalities of these operations are described in the comments.

Run Queries

To run the Queries defined in the repository, I need to get the Skunk Postgres Session(in our case CockroachDB Postgres API sessions). Once have the session, I can un-warp the dependency components of the IO monad and run the query functions which defined in the ToggleRepoImpl.

In here I have called createToggle, updateToggle, deleteToggle, getToggles, getServiceToggles functions defined in the ToggleRepoImpl. Following is the output of the queries.

Reference

  1. https://tpolecat.github.io/skunk/index.html
  2. https://medium.com/rahasak/run-scala-applications-with-graalvm-and-docker-a1e67701e935
  3. https://medium.com/rahasak/doobie-and-cats-effects-d01230be5c38
  4. https://medium.com/rahasak/multi-region-cockroachdb-cluster-on-kubernetes-32c95263d802
  5. https://medium.com/rahasak/hacking-with-http4s-doobie-and-cats-effects-4fc54068ea10

--

--