Last active
January 15, 2020 11:15
-
-
Save teamon/c5dc163ce7243cb5088ec72b0e132623 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Recruitee.Repo do | |
use Ecto.Repo, ot_app: :recruitee | |
import Ecto.Query | |
@doc """ | |
Stream query results | |
Example: | |
iex> Candidate | |
...> |> where([c], c.foo > 4) | |
...> |> Repo.stream(chunk_size: 300) | |
""" | |
def stream(query, opts \\ []) do | |
chunk_size = Keyword.get(opts, :chunk_size, 1000) | |
Stream.resource( | |
# start with id=0 | |
fn -> {query, 0} end, | |
# get at most `chunk_size` items | |
fn {query, last_id} -> | |
list = query | |
|> where([e], e.id > ^last_id) | |
|> limit(^chunk_size) | |
|> order_by([e], e.id) | |
|> all | |
case List.last(list) do | |
%{id: id} -> {list, {query, id}} | |
nil -> {:halt, {query, last_id}} | |
end | |
end, | |
fn _ -> [] end | |
) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@teamon is https://gist.github.com/teamon/c5dc163ce7243cb5088ec72b0e132623/8ec1b76d2b37acee788d7446062b195e1df4ed21#file-repo-ex-L2 a type with
ot_app
?