Alembic v2.3.0 Alembic.Pagination behaviour

Pagination by fixed-size pages

Summary

Types

t()
  • first - the first Page.t
  • last - the last Page.t
  • next - the next Page.t
  • previous - the previous Page.t
  • total_size - the total number of all resources that can be paged

Callbacks

Converts the module’s type to a t or nil if there is no pagination information

Types

t :: %Alembic.Pagination{first: Alembic.Pagination.Page.t, last: Alembic.Pagination.Page.t, next: Alembic.Pagination.Page.t, previous: Alembic.Pagination.Page.t, total_size: non_neg_integer}
  • first - the first Page.t
  • last - the last Page.t
  • next - the next Page.t
  • previous - the previous Page.t
  • total_size - the total number of all resources that can be paged.

Functions

reduce_field_key_to_links(pagination, u_r_i, arg, acc)
to_links(arg1, u_r_i)

Specs

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"
}

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

Callbacks

to_pagination(any)

Specs

to_pagination(any) :: t | nil

Converts the module’s type to a t or nil if there is no pagination information.