PaginationEx.Core (PaginationEx v0.1.0)
Core pagination functionality for Ecto queries.
This module provides the main pagination functionality for Ecto queries, including:
- Paginating query results with customizable page size
- Calculating total entries and pages
- Grouping results for bulk retrieval
- Handling various query types (simple, grouped, distinct)
The pagination result is a struct containing entries, metadata about the pagination, and the original query.
Summary
Functions
Retrieves all entries in groups of specified size.
Creates a new paginated result for the given query.
Types
@type t() :: %PaginationEx.Core{ entries: [any()], page_number: pos_integer(), pages: non_neg_integer(), per_page: pos_integer(), query: Ecto.Query.t(), total_entries: integer() }
Functions
@spec in_groups(Ecto.Queryable.t(), map()) :: [any()]
Retrieves all entries in groups of specified size.
This function fetches all records matching the query in batches, combining them into a single list. Useful for retrieving large datasets efficiently without loading everything at once.
Parameters
query- The Ecto query to paginateparams- Map of pagination parameters (optional)"per_group"- Number of items per group (defaults to config or 1000)"total"- Optional pre-calculated total count
Returns
- List of all entries matching the query
Examples
iex> PaginationEx.Core.in_groups(Post, %{"per_group" => "100"})
[%Post{}, %Post{}, ...]
@spec new(Ecto.Queryable.t(), map(), keyword()) :: t()
Creates a new paginated result for the given query.
This function returns a pagination struct containing the entries for the requested page, along with metadata about the pagination such as total entries, page count, etc.
Parameters
query- The Ecto query to paginateparams- Map of pagination parameters"page"- Page number to fetch (defaults to 1)"per_page"- Number of items per page (defaults to 30)"total"- Optional pre-calculated total count
opts- Options passed toRepo.all/2(optional)
Returns
- A
PaginationEx.Corestruct containing:entries- The paginated query resultstotal_entries- Total count of matching recordspage_number- Current page numberper_page- Number of items per pagepages- Total number of pagesquery- The original query
Examples
iex> PaginationEx.Core.new(Post, %{"page" => "2", "per_page" => "10"})
%PaginationEx.Core{
entries: [%Post{}, %Post{}, ...],
total_entries: 59,
page_number: 2,
per_page: 10,
pages: 6,
query: #Ecto.Query<...>
}