Search code examples
scalascala-catsdoobie

Doobie transact over a list of ConnectionIO programs


Let's say I have a list of Doobie programs (all with Unit type parameters, fwiw):

val progList: List[ConnectionIO[Unit]] = prog1 :: prog2 :: ... :: Nil

Is there any way I can run them in one transaction? A for-comprehension won't work here, because I only know the precise composition of the program list at runtime. Essentially, I suppose I need to fold them together, I guess.

I suppose this question applies to Free Monads in Cats in general, so I'll tag Cats as well. Thanks.


Solution

  • You can do that with .sequence from cats:

    import doobie.implicits._
    import cats.implicits._
    
    ...
    
    val res = progList.sequence // ConnectionIO[List[Unit]]