BoxTurtle.Schema.Pagination (BoxTurtle v0.1.0)

Copy Markdown View Source

Limit/offset pagination for Ecto queries.

BoxTurtle.Schema.Query applies it for the :page option, so you rarely call it directly. It also works on any Ecto query. A page is either a page number, using the default size, or a keyword list:

import Ecto.Query
alias BoxTurtle.Schema.Pagination

query = from(p in MyApp.Blog.Post, order_by: [desc: p.inserted_at, desc: p.id])

# Second page of 40 rows (the default size): LIMIT 40 OFFSET 40
query |> Pagination.page(2) |> MyApp.Repo.all()

# Rows 11-20: LIMIT 10 OFFSET 10
query |> Pagination.page(number: 2, size: 10) |> MyApp.Repo.all()

Page numbers below 1 are treated as 1. Give the query an order_by ending in a unique column, so rows don't move between pages.

Summary

Types

A page number, or [number: pos_integer(), size: pos_integer()].

Functions

The page size used when none is configured: 40.

Applies limit/offset for the given page to the query. A nil page leaves the query untouched.

Types

page_spec()

@type page_spec() :: nil | pos_integer() | keyword()

A page number, or [number: pos_integer(), size: pos_integer()].

Functions

default_page_size()

@spec default_page_size() :: pos_integer()

The page size used when none is configured: 40.

page(query, page, default_size \\ 40)

Applies limit/offset for the given page to the query. A nil page leaves the query untouched.