doobie

Travis CI Join the chat at https://gitter.im/tpolecat/doobie Maven Central Javadocs

doobie is a pure functional JDBC layer for Scala and Cats. It is not an ORM, nor is it a relational algebra; it simply provides a functional way to construct programs (and higher-level libraries) that use JDBC. For common use cases doobie provides a minimal but expressive high-level API:

import doobie._
import doobie.implicits._
import cats.effect.IO
import scala.concurrent.ExecutionContext

implicit val cs = IO.contextShift(ExecutionContext.global)

val xa = Transactor.fromDriverManager[IO](
  "org.postgresql.Driver", "jdbc:postgresql:world", "postgres", ""
)

case class Country(code: String, name: String, population: Long)

def find(n: String): ConnectionIO[Option[Country]] =
  sql"select code, name, population from country where name = $n".query[Country].option

And then …

scala> find("France").transact(xa).unsafeRunSync
res4: Option[Country] = Some(Country(FRA,France,59225700))

doobie is a Typelevel project. This means we embrace pure, typeful, functional programming, and provide a safe and friendly environment for teaching, learning, and contributing as described in the Typelevel Code of Conduct.

Quick Start

The current version is 0.6.0 for Scala 2.11/12 with

To use doobie you need to add the following to your build.sbt. If you’re not using the Postgres or H2 add-ons you’ll also need to provide a JDBC driver for the database you’re using.

scalacOptions += "-Ypartial-unification" // 2.11.9+

libraryDependencies ++= Seq(

  // Start with this one
  "org.tpolecat" %% "doobie-core"      % "0.6.0",

  // And add any of these as needed
  "org.tpolecat" %% "doobie-h2"        % "0.6.0",          // H2 driver 1.4.197 + type mappings.
  "org.tpolecat" %% "doobie-hikari"    % "0.6.0",          // HikariCP transactor.
  "org.tpolecat" %% "doobie-postgres"  % "0.6.0",          // Postgres driver 42.2.5 + type mappings.
  "org.tpolecat" %% "doobie-specs2"    % "0.6.0" % "test", // Specs2 support for typechecking statements.
  "org.tpolecat" %% "doobie-scalatest" % "0.6.0" % "test"  // ScalaTest support for typechecking statements.

)

See the documentation for more information on these add-ons.

Note that doobie is pre-1.0 software and is still undergoing active development. New versions are not binary compatible with prior versions, although in most cases user code will be source compatible. Starting with the 0.5.x we’re trying to be a bit more careful about versioning.

Documentation and Support

  • Behold the sparkly documentation ← start here
  • The Scaladoc will be handy once you get your feet wet.
  • See the changelog for an overview of changes in this and previous versions.
  • The Gitter Channel is a great place to chat!
  • There is a Scala Exercises module, courtesy of our friends at 47 Degrees!
  • There is also the source. Check out the examples too.
  • If you have comments or run into trouble, please file an issue.

Presentations, Blog Posts, etc.

Listed newest first. If you have given a presentation or have written a blog post that includes doobie, let me know and I’ll add it to this list.

Testing

If you want to build and run the tests for yourself, you’ll need a local postgresql database. Tests are run as the default postgres user, which should have no password for access in the local environment. You can see the before_script section of the .travis.yml file for an up-to-date list of steps for preparing the test database.