LiveFilter.QueryBuilder (LiveFilter v0.3.0)

Copy Markdown View Source

Thin query builder that converts LiveFilter structs to PgRest filter AST nodes (PgRest.AST.Condition) and applies them with PgRest.Ecto.Filter.where/3.

Summary

Types

A filter that could not be applied, with the reason.

Functions

Applies a list of Filter structs to an Ecto query.

Like apply/3, but raises the first PgRest.Error instead of skipping the filter.

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

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

Like apply_raw/3, but raises the first PgRest.Error instead of skipping the param.

Applies a LiveFilter.Sort to an Ecto query as order_by, mapping each entry via its query_field and nulls placement.

Like apply/3, and also returns the skipped filters with their PgRest.Error, in filter order, e.g. to flash which URL filters were ignored.

Counts total records for a query (for pagination).

Types

rejection()

@type rejection() :: {LiveFilter.Filter.t(), PgRest.Error.t()}

A filter that could not be applied, with the reason.

Functions

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

Applies a list of Filter structs to an Ecto query.

Converts each Filter to PgRest.AST.Conditions (a date range becomes a gte and an lte) and applies them with PgRest.Ecto.Filter.where/3.

A filter that cannot be applied is skipped: with :schema, a value its field type rejects (22P02, e.g. ?qty=gt.lots typed into the URL) or a field that is not a schema field (42703); in a param map, a value the PostgREST grammar rejects (PGRST100, e.g. ?enabled=is.maybe). The other filters still apply, so a hand-edited URL never crashes the LiveView. Use apply_with_errors/3 to report the skipped filters, or apply!/3 to raise.

Options

  • :schema - Ecto schema module; values are cast to its field types
  • :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!(query, filters_or_params, opts \\ [])

Like apply/3, but raises the first PgRest.Error instead of skipping the filter.

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.Queryable.t()

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

Parses each param with PgRest.Grammar.Filter.parse_condition/2 (params that are not op.value filters are skipped) and applies them with PgRest.Ecto.Filter.where/3. Values are cast to the query's schema fields; a schemaless source needs :allowed_fields (ArgumentError otherwise).

As in apply/3, a param whose field is not filterable or whose value cannot be cast is skipped; apply_raw!/3 raises instead.

Options

  • :schema - Ecto schema module for type casting (defaults to the query's schema)
  • :allowed_fields - list of atoms restricting which fields can be filtered

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

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

Like apply_raw/3, but raises the first PgRest.Error instead of skipping the param.

apply_sort(query, sort, opts \\ [])

@spec apply_sort(Ecto.Queryable.t(), LiveFilter.Sort.t(), keyword()) :: Ecto.Query.t()

Applies a LiveFilter.Sort to an Ecto query as order_by, mapping each entry via its query_field and nulls placement.

A deterministic tiebreaker column is appended so paginated, low-cardinality sorts stay stable across pages (the classic "rows skipped/repeated across pages" bug). Configure with opts[:tiebreak] (default :id, false to disable). For expression-based sorts, skip this and order on the parsed %LiveFilter.Sort{} yourself.

Example

query
|> LiveFilter.QueryBuilder.apply(filters, schema: Link)
|> LiveFilter.QueryBuilder.apply_sort(sort)        # tiebreak: :id
|> LiveFilter.QueryBuilder.apply_pagination(pagination)

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

@spec apply_with_errors(
  Ecto.Queryable.t(),
  [LiveFilter.Filter.t()] | map(),
  keyword()
) ::
  {Ecto.Queryable.t(), [rejection()]}

Like apply/3, and also returns the skipped filters with their PgRest.Error, in filter order, e.g. to flash which URL filters were ignored.

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)