I have a ConnectionIO[Option[Int]]
and map over the Option
to produce a ConnectionIO[Option[String]]
with a query the Some[Int]
otherwise keep the Nones. I was able to do this with a
forcomprehension and a
match`:
def findWidgetByOwner(name: String): ConnectionIO[Option[String]] = for {
opt <- sql"SELECT owner_id FROM owners WHERE name = $name".query[Int].option
widget <- opt match {
case None => None.pure[ConnectionIO]
case Some(id) => sql"SELECT widget_name FROM widgets WHERE owner_id = $id".query[String].option
}
} yield widget
I know i'm getting tripped up by the ConnectionIO
container, but I can't find a cleaner mapping approach that to transform ConnectionIO[Option[Int]]
to ConnectionIO[Option[String]]
.