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 | |
This comment has been minimized.
This comment has been minimized.
Yes, thanks :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
@teamon is https://gist.github.com/teamon/c5dc163ce7243cb5088ec72b0e132623/8ec1b76d2b37acee788d7446062b195e1df4ed21#file-repo-ex-L2 a type with
ot_app
?