Last active
November 23, 2022 18:28
-
-
Save TheKojuEffect/6b7ac38929ce7675c05e26440b4ce797 to your computer and use it in GitHub Desktop.
Configuring Scala 3, ZIO 2, and Doobie
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//> using scala "3.2.0" | |
//> using lib "dev.zio::zio:2.0.2" | |
//> using lib "dev.zio::zio-interop-cats:3.3.0" | |
//> using lib "org.tpolecat::doobie-core:1.0.0-RC2" | |
//> using lib "org.tpolecat::doobie-hikari:1.0.0-RC2" | |
import zio.* | |
import zio.interop.catz.* | |
import doobie.util.transactor.Transactor | |
import doobie.hikari.HikariTransactor | |
import cats.* | |
import cats.data.* | |
import cats.effect.* | |
import cats.implicits.* | |
import doobie.* | |
import doobie.implicits.* | |
case class DatabaseConfig(driver: String, url: String, username: String, password: String) | |
object ZioDoobieConfig { | |
given zioRuntime: zio.Runtime[Any] = zio.Runtime.default | |
given dispatcher: cats.effect.std.Dispatcher[zio.Task] = zio.Unsafe.unsafe { | |
unsafe => | |
{ | |
given Unsafe = unsafe | |
zioRuntime.unsafe | |
.run( | |
cats.effect.std | |
.Dispatcher[zio.Task] | |
.allocated, | |
) | |
.getOrThrowFiberFailure()(unsafe) | |
._1 | |
} | |
} | |
def transactor: ZIO[DatabaseConfig & Scope, Throwable, Transactor[Task]] = | |
for | |
ec <- ZIO.executor.map(_.asExecutionContext) | |
config <- ZIO.service[DatabaseConfig] | |
xa <- HikariTransactor | |
.newHikariTransactor[Task]( | |
config.driver, | |
config.url, | |
config.username, | |
config.password, | |
ec, | |
) | |
.toScopedZIO | |
yield xa | |
val liveTransactor: ZLayer[DatabaseConfig, Throwable, Transactor[Task]] = | |
ZLayer.scoped(transactor) | |
} |
What about:
/* missing */
[error] | summon[zio.Runtime[zio.clock.package.Clock & zio.interop.CBlocking]]
in lines: 30 and 49 ?
for me worked adding: import zio.interop.catz.implicits.*
Versioning of interop lib is unintuitive.
I had more problems with versions: 13.0.0.1
and 22.0.0.0
.
Going to 3.3.0 helped
@kastoestoramadus It could be because of the version incompatibility.
Checkout the new version scheme. https://github.com/zio/interop-cats/releases/tag/v13.0.0.0
3.3.0
is the latest version for ZIO 2 and Cats Effect 3.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
References:
https://zio.dev/guides/interop/with-cats-effect/#using-zio-with-doobie
https://medium.com/@wiemzin/zio-with-http4s-and-doobie-952fba51d089