Pager
Zero-dependency pagination for Ecto queries with automatic page boundary handling and flexible parameter types.
Features
- Clean API for paginating Ecto queries
- Handles string and integer page parameters seamlessly
- Automatic first page redirect when requested page is out of bounds
- Efficient count queries using subqueries
- Zero dependencies beyond Ecto
- Comprehensive pagination metadata
Installation
Add pager to your dependencies in mix.exs:
def deps do
[
{:pager, "~> 0.4.0"}
]
endUsage
import Ecto.Query
alias YourApp.Repo
alias YourApp.Post
# Basic query pagination
query = from(p in Post)
result = Pager.page(query, Repo, 1, 20)
# Handle Phoenix params directly
def index(conn, %{"page" => page, "per_page" => per_page}) do
query = from(p in Post, order_by: [desc: :inserted_at])
pages = Pager.page(query, Repo, page, per_page)
render(conn, "index.html", posts: pages.list)
endReturn Structure
%{
has_next: true, # More pages available
has_prev: false, # Previous pages available
prev_page: 0, # Previous page number
page: 1, # Current page
next_page: 2, # Next page number
first: 1, # First item index
last: 20, # Last item index
count: 50, # Total items
list: [%Post{}, ...] # Page items
}Documentation
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/page.
Contributing
- Fork it
-
Create your feature branch (
git checkout -b my-feature) -
Commit your changes (
git commit -am 'Add feature') -
Push to the branch (
git push origin my-feature) - Create a Pull Request
Development Setup
-
Create repo for tests:
MIX_ENV=test mix ecto.create MIX_ENV=test mix ecto.migrate mix test