View Source Mobilizon.Storage.Page (Mobilizon v5.0.0-beta.1)

Module for pagination of queries.

Summary

Functions

Returns a Page struct for a query.

Stream chunks of results from the given queryable.

Add limit and offset to the query.

Types

@type t(structure) :: %Mobilizon.Storage.Page{elements: [structure], total: integer()}

Functions

Link to this function

build_page(query, page, limit, field \\ :id)

View Source
@spec build_page(Ecto.Queryable.t(), integer() | nil, integer() | nil, atom()) ::
  t(any())

Returns a Page struct for a query.

field is use to define the field that will be used for the count aggregate, which should be the same as the field used for order_by See https://stackoverflow.com/q/12693089/10204399

Link to this function

chunk(queryable, chunk_size)

View Source
@spec chunk(Ecto.Queryable.t(), integer()) :: Stream.t()

Stream chunks of results from the given queryable.

Unlike Repo.stream, this function does not keep a long running transaction open. Hence, consistency is not guarenteed in the presence of rows being deleted or sort criteria changing.

Example

Ecto.Query.from(u in Users, order_by: [asc: :created_at]) |> Repo.chunk(100) |> Stream.map(&process_batch_of_users) |> Stream.run()

Source

https://elixirforum.com/t/what-is-the-best-approach-for-fetching-large-amount-of-records-from-postgresql-with-ecto/3766/8

Link to this function

paginate(query, page \\ 1, size \\ 10)

View Source
@spec paginate(Ecto.Queryable.t() | struct(), integer() | nil, integer() | nil) ::
  Ecto.Query.t()

Add limit and offset to the query.