Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 920
Lasf Avatar asked Jul 29 '18 13:07

Lasf


1 Answers

You can do that with .sequence from cats:

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

...

val res = progList.sequence // ConnectionIO[List[Unit]]
like image 183
Alvaro Carrasco Avatar answered Nov 03 '22 00:11

Alvaro Carrasco