kubukoz / slick-effect

Making Slick more functional with cats-effect.

GitHub

slick-effect

License

Compatibility tools for Slick + cats-effect. Released for Scala 2.12 and 2.11.

Usage

Add the dependency. SBT:

"com.kubukoz" %% "slick-effect" % "0.1.0"

Ammonite:

$ivy.`com.kubukoz::slick-effect:0.1.0`

Coursier:

com.kubukoz::slick-effect:0.1.0

Instances

Import the instances:

import slickeffect.implicits._

//an implicit EC is needed for cpu-bound work on DBIOs (map, flatMap)
import scala.concurrent.ExecutionContext.Implicits.global

//the instances will be in implicit scope
scala> Async[slick.dbio.DBIO]
res0: Async[slick.dbio.package.DBIO] = slickeffect.DBIOAsync@434c179e

Transactor (from 0.3.0-M2 onwards)

You can use slick-effect to run your DBIOs. Add a dependency on the transactor module:

"com.kubukoz" %% "slick-effect-transactor" % "0.3.0-M2"

Create a transactor:

val transactorResource: Resource[IO, Transactor[IO]]
  .fromDatabase[IO](IO(Database.forURL("jdbc:h2:mem:"))) //or .fromDatabaseConfig
  .map(_.configure(config.transactionally)) //or any DBIO ~> DBIO
  .use(_.transact(action))


val result: DBIO[Int] = ???

transactorResource.use { tx =>
  tx.transact(result): IO[Int]
}