Doobie and DB access composition within 1 transaction

前端 未结 1 1447
遇见更好的自我
遇见更好的自我 2020-12-24 08:44

Doobie book says that it\'s a good practice to return ConnectionIO from your repository layer. It gives an ability to chain calls and perform them in one transaction. Nice

1条回答
  •  一生所求
    2020-12-24 09:20

    ConnectionIO in doobie has a cats.effect.Async instance, which, among other things, allows you do turn any cats.effect.IO into ConnectionIO by means of liftIO method:

    import doobie.free.connection._
    import cats.effect.{IO, Async}
    val catsIO: IO[String] = ???
    val cio: ConnectionIO[String] = Async[ConnectionIO].liftIO(catsIO)
    

    For monix.eval.Task, your best bet is using Task#toIO and performing the same trick, but you'd need a monix Scheduler in scope.

    0 讨论(0)
提交回复
热议问题