SQLite

SQLite is an in-process database that is becoming more popular for web applications due to the simplicity of running it. It's appropriate for applications that don't need to expand beyond a single web server, which covers many applications due to the speed of modern computers. Krop provides SQLite integration via the sqlite-jdbc and Magnum projects.

Using SQLite

Krop's integration lives in a separate artifact. To use it you'll need to add the following dependency to your build.sbt.

libraryDependencies += "org.creativescala" %% "krop-sqlite" % "0.9.1"

Creating a Database Connection

Connecting to a database is trivial, as SQLite only requires the name of the file that stores the database. If the file doesn't already exist it will be created. The code below shows how to connect to a database. This uses a default configuration that has been tuned to the needs of a typical web application.

import cats.effect.{IO, Resource}
import krop.sqlite.{Sqlite, Transactor}

val dbFile = "./database.sqlite3"
val db: Resource[IO, Transactor] = Sqlite.create(dbFile)

If we wanted a custom configuration we could set one ourselves. If the example below we use an empty configuration, but we can call methods on the object returned by empty to customise the configuration.

Sqlite.create(dbFile, Sqlite.config.empty)

The value returned by Sqlite.create is a Cats Effect Resource containing a Transactor. This means that nothing actually happens until we use the Resource, with code like the following.

db.use { xa =>
  // Use the Transactor, xa, here
}

The Transactor is a Magnum type to work with databases.

Copyright © Noel Welsh. Built with 💖