Special Alpakka Slick Flow streaming elements off of a SELECT


(Max Calderoni) #1

After reading about the Alpakka’s Slick adapter, i was not able to figure out how to stream elements off of a SELECT in a Flow.
It is easy to do with a Source:

Slick.source(sql"SELECT ID, NAME FROM USERS".as[User])

which will emit one User element at a time.
Not so easy to me with a Flow that takes some element, issues a SELECT query internally and then emits the result of that SELECT one by one.
(For example, say the input element to the Flow is an Int, 100, and the SELECT is something like ‘SELECT * FROM USERS LIMIT 100’)

The closest Flow operation i see in Alpakka resembling what i need is Slick.flowWithPassThrough, but that seems to be forcing me to

Slick.flowWithPassThrough[Int, Seq[User]]

and not something like

Slick.flowWithPassThrough[Int, User]

with User and not Seq[User].

Of course, i can add a downstream stage that flattens the sequence resulting from the SELECT stage upstream, but i was trying to avoid that.
Did i miss something obvious in the documentation or API, or do i have to write a special GraphStage (Flow)?


(Martynas Mickevičius) #2

I am glad that you found Alpakka Slick connector interesting.

It looks like you could use the flatMapConcat operator here.

val flow: Flow[Int, User, NotUsed] =
  Flow[Int].flatMapConcat(limit =>
    Slick.source(sql"SELECT ID, NAME FROM USERS, LIMIT $limit".as[User]))