Create a gist now

Instantly share code, notes, and snippets.

@teamon /repo.ex
Last active Dec 29, 2016

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