Last active
January 15, 2020 11:15
Revisions
-
teamon revised this gist
Jun 27, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ defmodule Recruitee.Repo do use Ecto.Repo, otp_app: :recruitee import Ecto.Query @doc """ -
teamon revised this gist
Dec 26, 2016 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -12,6 +12,7 @@ defmodule Recruitee.Repo do """ def stream(query, opts \\ []) do chunk_size = Keyword.get(opts, :chunk_size, 1000) on_chunk = Keyword.get(opts, :on_chunk) Stream.resource( # start with id=0 @@ -24,6 +25,8 @@ defmodule Recruitee.Repo do |> limit(^chunk_size) |> order_by([e], e.id) |> all if on_chunk, do: on_chunk.(list) case List.last(list) do %{id: id} -> {list, {query, id}} -
teamon created this gist
Dec 26, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ 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