Alembic v4.0.0 Alembic.Pagination behaviour View Source

Pagination by fixed-size pages

Link to this section 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

Link to this section Types

Link to this type t() View Source
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 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.

Link to this section Functions

Link to this function reduce_field_key_to_links(pagination, base_uri, arg, acc) View Source
Link to this function to_links(pagination, base_uri) View Source
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

Link to this section Callbacks

Link to this callback to_pagination(any) View Source
to_pagination(any()) :: t() | nil

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