Paginator v0.3.0 Paginator behaviour View Source
Defines a paginator.
This module adds a
paginate/3 function to your
Ecto.Repo so that you can
paginate through results using opaque cursors.
defmodule MyApp.Repo do use Ecto.Repo, otp_app: :my_app use Paginator end
Paginator can take any options accepted by
paginate/3. This is useful when
you want to enforce some options globally across your project.
defmodule MyApp.Repo do use Ecto.Repo, otp_app: :my_app use Paginator, limit: 10, # sets the default limit to 10 maximum_limit: 100, # sets the maximum limit to 100 include_total_count: true # include total count by default end
Note that these values can be still be overriden when
paginate/3 is called.
Link to this section Summary
Link to this section Callbacks
Fetches all the results matching the query within the cursors.
:after- Fetch the records after this cursor.
:before- Fetch the records before this cursor.
:cursor_fields- The fields used to determine the cursor. In most cases, this should be the same fields as the ones used for sorting in the query.
:include_total_count- Set this to true to return the total number of records matching the query. Note that this number will be capped by
:total_count_limit. Defaults to
:limit- Limits the number of records returned per page. Note that this number will be capped by
:maximum_limit. Defaults to
:maximum_limit- Sets a maximum cap for
:limit. This option can be useful when
:limitis set dynamically (e.g from a URL param set by a user) but you still want to enfore a maximum. Defaults to
:sort_direction- The direction used for sorting. Defaults to
:total_count_limit- Running count queries on tables with a large number of records is expensive so it is capped by default. Can be set to
:infinityin order to count all the records. Defaults to
This will be passed directly to
Ecto.Repo.all/2, as such any option supported
by this function can be used here.
query = from(p in Post, order_by: [asc: p.inserted_at, asc: p.id], select: p) Repo.paginate(query, cursor_fields: [:inserted_at, :id], limit: 50)