ecto-cursor-based-stream
ecto-cursor-based-stream copied to clipboard
Elixir library that allows for cursor-based streaming of Ecto records, that does not require database transaction.
EctoCursorBasedStream
Cursor-based streaming of Ecto records, that does not require database transaction.
Gives you a cursor_based_stream/2 function that mimics Ecto.Repo.stream/2 interface.
Advantages in comparison to the standard Ecto.Repo.stream/2:
- streaming can be stopped and continued at any point (by passing
opts[:after_cursor]), - works with tables that have milions of records.
Only limitation is that you have to supply a cursor column (by passing opts[:cursor_field], defaults to :id). Such a column:
- must have unique values,
- should have a database index. (So that sorting by it, and returning a number of rows larger than
xis a performant operation.)
Usage
- Add
ecto_cursor_based_streamto your list of dependencies inmix.exs:
def deps do
[
{:ecto_cursor_based_stream, "~> 1.0.2"}
]
end
- Add
use EctoCursorBasedStreamto the module that usesEcto.Repo:
defmodule MyRepo do
use Ecto.Repo
use EctoCursorBasedStream
end
- Stream the rows using
cursor_based_stream/2:
Post
|> MyRepo.cursor_based_stream(max_rows: 100)
|> Stream.each(...)
|> Stream.run()
Useful links
Contributing
Running tests
Run the following after cloning the repo:
mix deps.get
docker-compose up -d
mix test