CockroachDB Scala Client with Slick

Functional Ops

(λx.x)eranga
Effectz.AI
3 min readApr 23, 2022

--

Background

In my previous post I have discussed about building CockroachDB client application with Skunk library which is purely functional, non-blocking Postgres library for Scala. In this post I’m gonna discuss about building CockroachDB client application Slick library. Slick is a modern database query and access library for Scala. It allows you to work with stored data almost as if you were using Scala collections while at the same time giving you full control over when a database access happens and which data is transferred. You can write your database queries in Scala instead of SQL, thus profiting from the static checking, compile-time safety and compositionality of Scala.

Use case

I have been designing highly scalable, lightweight blockchain storage Librumchain. Following figure shows how Librumchain works with CockroachDB storage. I have used Scala, Golang and Rust to implement the Librumchain. CockroachDB has been use as the data storage. Basically CockroachDB stores transactions, blocks, accounts, token balances details of the blockchain. In this post I’m gonna discuss about how I have used CockroachDB to handle the blockchain storage functions in Scala application. To keep the things simple I’m using minimum database table structures. All the source codes which related to this post available in gitlab. Please clone the repo and continue the post.

Deploy CockroachDB

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 I 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 named kerapoththa. Following is the way to create the database via connecting to the CockroachDB Sql API inside the Docker container.

Sbt Dependency

Following is the build.sbt. I have used several Slick related dependencies here, 1) slick-hikaricp(Slick connection pooling library), 2) slick-pg(Slick Postgres extension), 3) postgresql(Postgres driver library).

Database Config

Following is the CockroachDB related configs. These configurations defined in storage.conf file and loaded into StorageConf trait. Slick data source created with HikariCP connection pooling library HikariDataSource.

Slick Database Models

As mentioned previously, I have used four main tables in this blockchain scenario, 1) accounts, 2) balances, 3) blocks, 4) transactions. The blocks table contains list of transactions(as a Postgres array type field). To define arrays with Slick I have to create a new Postgres profile with array support using slick-pg library.

Slick Queries

Following are the Slick queries which related to model classes. To keep things simple I have used KerapoththaRepo and extend it with AccountTable, BalanceTable, TransactionTable and BlockTable traits. To properly handle dependencies you could use self typed annotations(cake pattern) or monads.

Run Queries

Following is the way to execute the Slick queries defined in the KerapoththaRepo. In here first I have created the CockroachDB database tables executing init() function and then execute the queries.

Reference

  1. https://medium.com/rahasak/scala-cockroachdb-postgres-client-with-skunk-622423a29840
  2. https://stackoverflow.com/questions/57658382/how-to-map-mapstring-string-to-string-string-in-slick-3-2-0
  3. https://dzone.com/articles/using-cockroach-with-scala-an-introduction
  4. https://blog.knoldus.com/getting-started-cockroach-with-scala-an-introduction/
  5. https://plippe.github.io/blog/2020/06/01/playing-with-scala-slick.html

--

--