alexdesousa / ayesql

Library for using raw SQL in Elixir
MIT License
135 stars 13 forks source link

Query stream support #23

Closed visciang closed 2 years ago

visciang commented 3 years ago

Hi, @alexdesousa, I've played a bit to extend the query execution mode to support the postgrex / ecto stream mode. Hope it make sense for the library and its design.

I thought about how to introduce it in the current interface to the queries, being clean an backward compatible.

I think a new options could be used:

# Current query behaviour, result collected in memory and returned as a list to the caller
MyQueries.get_user([id: id], run?: true)

# Stream mode, the `stream_fun` function is called for each row in the stream,
# the whole execution happens in a transaction (ref Postgrex.stream and Ecto.Repo.stream)
MyQueries.get_user([id: id], run?: true, stream_fun: &IO.inspect/1)

Feel free to change / refactor / adjust if you think this feature can be included in the library.

Thank you

alexdesousa commented 2 years ago

Hey @visciang! Thank you for the PR and sorry I actually review it so late :grimacing:

I was thinking that this Stream functionality could be moved to a new runner implementation. I want to keep current runners as simple as possible to avoid adding more complexity to them.

I'll close this PR for now, but feel free to reopen it if you want to discuss this further :slightly_smiling_face:

visciang commented 2 years ago

@alexdesousa sure, make sense to have this logic in the runner