ecto_paging v0.8.4 Ecto.Paging

This module provides a easy way to apply cursor-based pagination to your Ecto Queries.

Usage:

  1. Add macro to your repo

    defmodule MyRepo do use Ecto.Repo, otp_app: :my_app use Ecto.Paging.Repo # This string adds paginate/2 method. end

  2. Paginate!

    query = from p in Ecto.Paging.Schema

    query |> Ecto.Paging.TestRepo.paginate(%Ecto.Paging{limit: 150}) |> Ecto.Paging.TestRepo.all

Limitations:

  • Right now it works only with schemas that have :inserted_at field with auto-generated value.
  • You need to be careful with order-by’s in your queries, since this feature is not tested yet.
  • It doesn’t construct paginate struct with has_more and size counts (TODO: add this helpers).
  • When both starting_after and ending_before is set, only starting_after is used.

Link to this section Summary

Functions

This struct defines pagination rules. It can be used in your response API

Convert map into Ecto.Paging struct

Build a %Ecto.Paging{} struct to fetch next page results based on previous Ecto.Repo.all result and previous paging struct

Apply pagination to a Ecto.Query. It can accept either Ecto.Paging struct or map that can be converted to it via from_map/1

Convert Ecto.Paging struct into map and drop all nil values and cursors property if it’s empty

Link to this section Types

Link to this type t()
t() :: %{limit: number, cursors: Ecto.Paging.Cursors.t, has_more: number, size: number}

Link to this section Functions

Link to this function %Ecto.Paging{} (struct)

This struct defines pagination rules. It can be used in your response API.

Link to this function find_where_order(query, chronological_field, timestamp, atom)
Link to this function from_map(paging)

Convert map into Ecto.Paging struct.

Link to this function get_next_paging(query_result, paging)

Build a %Ecto.Paging{} struct to fetch next page results based on previous Ecto.Repo.all result and previous paging struct.

Link to this function paginate(query, paging, opts)

Apply pagination to a Ecto.Query. It can accept either Ecto.Paging struct or map that can be converted to it via from_map/1.

Convert Ecto.Paging struct into map and drop all nil values and cursors property if it’s empty.