Lasf
Lasf

Reputation: 2582

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.

Upvotes: 9

Views: 1835

Answers (1)

Alvaro Carrasco
Alvaro Carrasco

Reputation: 6182

You can do that with .sequence from cats:

import doobie.implicits._
import cats.implicits._

...

val res = progList.sequence // ConnectionIO[List[Unit]]

Upvotes: 17

Related Questions