Skip to content

Instantly share code, notes, and snippets.

@teamon
Last active January 15, 2020 11:15

Revisions

  1. teamon revised this gist Jun 27, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion repo.ex
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    defmodule Recruitee.Repo do
    use Ecto.Repo, ot_app: :recruitee
    use Ecto.Repo, otp_app: :recruitee
    import Ecto.Query

    @doc """
  2. teamon revised this gist Dec 26, 2016. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions repo.ex
    Original 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}}
  3. teamon created this gist Dec 26, 2016.
    38 changes: 38 additions & 0 deletions repo.ex
    Original 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