https://github.com/or-equals/pager
A simple pagination library for Elixir
https://github.com/or-equals/pager
ecto elixir pagination
Last synced: 4 months ago
JSON representation
A simple pagination library for Elixir
- Host: GitHub
- URL: https://github.com/or-equals/pager
- Owner: or-equals
- Created: 2021-11-09T17:58:42.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-02-11T18:55:14.000Z (over 1 year ago)
- Last Synced: 2025-10-21T15:06:29.309Z (8 months ago)
- Topics: ecto, elixir, pagination
- Language: Elixir
- Homepage: https://hexdocs.pm/pager/Pager.html
- Size: 19.5 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Pager
Zero-dependency pagination for Ecto queries with automatic page boundary handling and flexible parameter types. [View the documentation](https://hexdocs.pm/pager/Pager.html)
## 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`:
```elixir
def deps do
[
{:pager, "~> 1.0.0"}
]
end
```
## Usage
```elixir
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)
end
```
### Return Structure
```elixir
%{
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
}
```
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-feature`)
3. Commit your changes (`git commit -am 'Add feature'`)
4. Push to the branch (`git push origin my-feature`)
5. Create a Pull Request
### Development Setup
1. Create repo for tests:
```bash
MIX_ENV=test mix ecto.create
MIX_ENV=test mix ecto.migrate
mix test
```
### Generating Documentation
```bash
mix docs
open doc/index.html
```