LiveFilter.QueryBuilder (LiveFilter v0.1.8)

Copy Markdown View Source

Thin query builder that converts LiveFilter structs to PgRest AST maps and delegates to PgRest.Filter.apply_all/2.

Summary

Functions

Applies a list of Filter structs to an Ecto query.

Applies pagination (limit/offset) to an Ecto query.

Applies a raw PostgREST param map to an Ecto query without filter config.

Counts total records for a query (for pagination).

Functions

apply(query, filters_or_params, opts \\ [])

@spec apply(Ecto.Queryable.t(), [LiveFilter.Filter.t()] | map(), keyword()) ::
  Ecto.Query.t()

Applies a list of Filter structs to an Ecto query.

Converts each Filter to a PgRest-compatible AST map, splits date_range compound filters, optionally casts via PgRest.TypeCaster, and delegates to PgRest.Filter.apply_all/2.

Options

  • :schema - Ecto schema module for type casting via PgRest.TypeCaster.cast_filters/2
  • :allowed_fields - list of atoms restricting which fields can be filtered
  • :config - filter config list (required when passing a param map instead of filters)

apply_pagination(query, pagination)

@spec apply_pagination(Ecto.Queryable.t(), LiveFilter.Pagination.t()) ::
  Ecto.Query.t()

Applies pagination (limit/offset) to an Ecto query.

Example

query
|> LiveFilter.QueryBuilder.apply(filters, schema: Task)
|> LiveFilter.QueryBuilder.apply_pagination(pagination)
|> Repo.all()

apply_raw(query, params, opts \\ [])

@spec apply_raw(Ecto.Queryable.t(), map(), keyword()) :: Ecto.Query.t()

Applies a raw PostgREST param map to an Ecto query without filter config.

Parses each param via PgRest.Parser.parse_operator_value/1 and delegates to PgRest.Filter.apply_all/2.

Options

  • :schema - Ecto schema module for type casting
  • :allowed_fields - list of atoms restricting which fields can be filtered

count(query, repo)

@spec count(Ecto.Queryable.t(), module()) :: non_neg_integer()

Counts total records for a query (for pagination).

Strips select, order_by, preload, limit, and offset to get an accurate count.

Example

base_query = Task |> LiveFilter.QueryBuilder.apply(filters, schema: Task)
total_count = LiveFilter.QueryBuilder.count(base_query, Repo)