Skip to content

Instantly share code, notes, and snippets.

@rawman
Created September 19, 2016 16:49
Show Gist options
  • Save rawman/190bf8038fbab3653920361a97404990 to your computer and use it in GitHub Desktop.
Save rawman/190bf8038fbab3653920361a97404990 to your computer and use it in GitHub Desktop.
def page(query, from, limit) do
from m in query,
where: m.id > ^from,
limit: ^limit
end
def to_stream(query, repo, page_size \\ 100) do
Stream.resource(
fn -> 0 end,
fn previous_id ->
case get_page(from, page_size, repo) do
[] -> {:halt, []}
items -> {items, List.last(items).id}
end
end,
fn page -> page end
)
end
defp get_page(from, limit, repo) do
query
|> page(from, limit)
|> repo.all
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment