Ecto.Paging
This module provides a easy way to apply cursor-based pagination to your Ecto Queries.
Usage:
Add macro to your repo
defmodule MyRepo do
use Ecto.Repo, otp_app: :my_app use Ecto.Pagging.Repo # This string adds `paginate/2` method.
end
Paginate!
query = from p in Ecto.Paging.Schema
query |> Ecto.Paging.TestRepo.paginate(%Ecto.Paging{limit: 150}) |> Ecto.Paging.TestRepo.all
Limitations:
- Right now it works only with schemas that have
:inserted_at
field with auto-generated value. - You need to be careful with order-by’s in your queries, since this feature is not tested yet.
- It doesn’t construct
paginate
struct withhas_more
andsize
counts (TODO: add this helpers). - When both
starting_after
andending_before
is set, onlystarting_after
is used.
Installation
Add
ecto_paging
to your list of dependencies inmix.exs
:def deps do [{:ecto_paging, "~> 0.2.0"}] end
Ensure
ecto_paging
is started before your application:def application do [applications: [:ecto_paging]] end