Created
September 19, 2016 16:49
-
-
Save rawman/190bf8038fbab3653920361a97404990 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
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