Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: Add stream_without_transaction #4526

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions lib/ecto/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,46 @@ defmodule Ecto.Repo do
end
end

def stream_without_transaction(queryable, opts \\ []) do
limit = Keyword.get(opts, :limit, 500)

base_query = queryable |> limit(^limit)

state = case Keyword.get(opts, :next, nil) do
nil -> %{offset: 0}
next -> %{next: next, query: base_query}
end

all_ops = Keyword.drop(opts, [:limit, :next])

Stream.resource(
fn -> state end,
fn state ->
{data, state} = case state do
%{next: next, query: query} ->
data = all(query, all_ops)

query = case data do
[] -> nil
_ -> next.(base_query, data)
end

{data, %{next: next, query: query}}

%{offset: o} ->
data = base_query |> offset(^o) |> all(all_ops)
{data, %{offset: o + limit}}
end

case data do
[] -> {:halt, :done}
_ -> {data, state}
end
end,
fn _ -> :ok end
)
end

def all(queryable, opts \\ []) do
repo = get_dynamic_repo()

Expand Down
Loading