Alembic v4.0.0 Alembic.Pagination behaviour View Source
Pagination by fixed-size pages
Link to this section Summary
Types
first
- the firstPage.t
last
- the lastPage.t
next
- the nextPage.t
previous
- the previousPage.t
total_size
- the total number of all resources that can be paged
Functions
Converts t
back to the named Links.t
Callbacks
Converts the module’s type to a t
or nil
if there is no pagination information
Link to this section Types
t() :: %Alembic.Pagination{ first: Alembic.Pagination.Page.t(), last: Alembic.Pagination.Page.t(), next: Alembic.Pagination.Page.t() | nil, previous: Alembic.Pagination.Page.t() | nil, total_size: non_neg_integer() }
first
- the firstPage.t
last
- the lastPage.t
next
- the nextPage.t
previous
- the previousPage.t
total_size
- the total number of all resources that can be paged.
Link to this section Functions
to_links(nil, URI.t()) :: nil
to_links(t(), URI.t()) :: Alembic.Links.t()
Converts t
back to the named Links.t
Single Page
When there is only one page, there will be a "first"
and "last"
link pointing to the same page, but no
“next” or “prev” links.
iex> Alembic.Pagination.to_links(
...> %Alembic.Pagination{
...> first: %Alembic.Pagination.Page{
...> number: 1,
...> size: 10
...> },
...> last: %Alembic.Pagination.Page{
...> number: 1,
...> size: 10
...> }
...> },
...> URI.parse("https://example.com/api/v1/users")
...> )
%{
"first" => "https://example.com/api/v1/users?page%5Bnumber%5D=1&page%5Bsize%5D=10",
"last" => "https://example.com/api/v1/users?page%5Bnumber%5D=1&page%5Bsize%5D=10"
}
Multiple Pages
When there are multiple pages, every page will have a "first"
and "last"
link pointing to the respective,
different pages.
On the first page, the "next"
link will be set, but not the "prev"
link.
iex> Alembic.Pagination.to_links(
...> %Alembic.Pagination{
...> first: %Alembic.Pagination.Page{
...> number: 1,
...> size: 10
...> },
...> last: %Alembic.Pagination.Page{
...> number: 3,
...> size: 10
...> },
...> next: %Alembic.Pagination.Page{
...> number: 2,
...> size: 10
...> }
...> },
...> URI.parse("https://example.com/api/v1/users")
...> )
%{
"first" => "https://example.com/api/v1/users?page%5Bnumber%5D=1&page%5Bsize%5D=10",
"last" => "https://example.com/api/v1/users?page%5Bnumber%5D=3&page%5Bsize%5D=10",
"next" => "https://example.com/api/v1/users?page%5Bnumber%5D=2&page%5Bsize%5D=10"
}
On any middle page, both the "next"
and "prev"
links will be set.
iex> Alembic.Pagination.to_links(
...> %Alembic.Pagination{
...> first: %Alembic.Pagination.Page{
...> number: 1,
...> size: 10
...> },
...> last: %Alembic.Pagination.Page{
...> number: 3,
...> size: 10
...> },
...> next: %Alembic.Pagination.Page{
...> number: 3,
...> size: 10
...> },
...> previous: %Alembic.Pagination.Page{
...> number: 1,
...> size: 10
...> }
...> },
...> URI.parse("https://example.com/api/v1/users")
...> )
%{
"first" => "https://example.com/api/v1/users?page%5Bnumber%5D=1&page%5Bsize%5D=10",
"last" => "https://example.com/api/v1/users?page%5Bnumber%5D=3&page%5Bsize%5D=10",
"next" => "https://example.com/api/v1/users?page%5Bnumber%5D=3&page%5Bsize%5D=10",
"prev" => "https://example.com/api/v1/users?page%5Bnumber%5D=1&page%5Bsize%5D=10"
}
On the last page, the "prev"
link will be set, but not the "next"
link.
iex> Alembic.Pagination.to_links(
...> %Alembic.Pagination{
...> first: %Alembic.Pagination.Page{
...> number: 1,
...> size: 10
...> },
...> last: %Alembic.Pagination.Page{
...> number: 3,
...> size: 10
...> },
...> previous: %Alembic.Pagination.Page{
...> number: 2,
...> size: 10
...> }
...> },
...> URI.parse("https://example.com/api/v1/users")
...> )
%{
"first" => "https://example.com/api/v1/users?page%5Bnumber%5D=1&page%5Bsize%5D=10",
"last" => "https://example.com/api/v1/users?page%5Bnumber%5D=3&page%5Bsize%5D=10",
"prev" => "https://example.com/api/v1/users?page%5Bnumber%5D=2&page%5Bsize%5D=10"
}
No Links
If there are no links, then nil
will be returned
iex> Alembic.Pagination.to_links(nil, URI.parse("https://example.com/api/v1/users"))
nil
Link to this section Callbacks
Converts the module’s type to a t
or nil
if there is no pagination information.