+ def fetch_paginated(query, %{"total" => true} = params, :offset, table_binding) do
+ total =
+ query
+ |> Ecto.Query.exclude(:left_join)
+ |> Repo.aggregate(:count, :id)
+
+ %{
+ total: total,
+ items: fetch_paginated(query, Map.drop(params, ["total"]), :offset, table_binding)
+ }
+ end
+
+ def fetch_paginated(query, params, :offset, table_binding) do
+ options = cast_params(params)
+
+ query
+ |> paginate(options, :offset, table_binding)
+ |> Repo.all()
+ end
+
+ @spec paginate(Ecto.Query.t(), map(), type(), atom() | nil) :: [Ecto.Schema.t()]
+ def paginate(query, options, method \\ :keyset, table_binding \\ nil)
+
+ def paginate(query, options, :keyset, table_binding) do
+ query
+ |> restrict(:min_id, options, table_binding)
+ |> restrict(:since_id, options, table_binding)
+ |> restrict(:max_id, options, table_binding)
+ |> restrict(:order, options, table_binding)
+ |> restrict(:limit, options, table_binding)
+ end
+
+ def paginate(query, options, :offset, table_binding) do