Skip to content

Instantly share code, notes, and snippets.

@TheKojuEffect
Last active November 23, 2022 18:28
Show Gist options
  • Save TheKojuEffect/6b7ac38929ce7675c05e26440b4ce797 to your computer and use it in GitHub Desktop.
Save TheKojuEffect/6b7ac38929ce7675c05e26440b4ce797 to your computer and use it in GitHub Desktop.
Configuring Scala 3, ZIO 2, and Doobie
//> 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)
}
@kastoestoramadus
Copy link

kastoestoramadus commented Nov 4, 2022

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.*

@kastoestoramadus
Copy link

kastoestoramadus commented Nov 4, 2022

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

@TheKojuEffect
Copy link
Author

TheKojuEffect commented Nov 4, 2022

@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