Slab.Query (Slab v1.0.0)

Copy Markdown View Source

Builds and runs Ecto queries for Slab.table/1 query mode.

Query support is compiled in only when Ecto is available (it is an optional dependency). Without Ecto, fetch/4 raises at runtime with instructions.

Everything URL-driven is whitelisted and cast before it touches a query: sort fields come from <:col sortable> declarations, and cursor values are cast with Ecto.Type.cast/2 against the schema's field types — invalid input is ignored, never interpolated.

Summary

Functions

Applies WHERE conditions from the filter URL params.

Applies order_by from the sort and sort_direction params.

Counts the records matching the current filters, ignoring sorting and pagination. Backs the "Showing X to Y of Z entries" summary and the numbered page links in page mode.

Returns the values of an Ecto.Enum field, or nil when the field is not an enum. Backs automatic select options in the Filters tab.

Runs the queryable through the repo, applying whitelisted sorting and pagination from params.

Returns the Ecto.Schema module behind a queryable — the module itself, or the source schema of an %Ecto.Query{}. Returns nil when there is no schema to reflect on.

Functions

apply_filters(queryable, params, filterable_cols, schema)

Applies WHERE conditions from the filter URL params.

Only whitelisted columns are ever filtered. Each filter is one of:

  • a custom 2-arity function from <:col filter={...}> — called with (queryable, raw_value) and free to add joins or any Ecto condition
  • a bare value (filter[name]=ada) — strings match with a case-insensitive contains, other types cast to an equality check
  • a list of values (filter[role][]=a&filter[role][]=b, from multi-select inputs) — becomes a field IN (...) condition
  • an operator map (filter[age][gte]=21) — supported operators are eq, neq, gt, gte, lt, lte, and contains

Declarative values are cast with Ecto.Type.cast/2 against the schema's field type; anything that fails to cast is ignored, never interpolated.

apply_sort(queryable, params, sortable_fields)

Applies order_by from the sort and sort_direction params.

Only fields in sortable_fields (a list of atoms, derived from <:col sortable> declarations) are ever compiled into the query — anything else in the URL is ignored.

count(queryable, repo, params, filterable_cols)

Counts the records matching the current filters, ignoring sorting and pagination. Backs the "Showing X to Y of Z entries" summary and the numbered page links in page mode.

enum_values(schema, field)

Returns the values of an Ecto.Enum field, or nil when the field is not an enum. Backs automatic select options in the Filters tab.

fetch(queryable, repo, params, opts)

Runs the queryable through the repo, applying whitelisted sorting and pagination from params.

Returns {records, has_next?}. Pagination fetches one extra record to detect whether a next page exists, avoiding a count query.

Options

  • :sortable_fields - atoms whitelisted for ORDER BY
  • :filterable_cols - columns whitelisted for WHERE, as maps of %{field: atom, filter: fun | nil}
  • :paginate - nil, :page, or :cursor
  • :per_page - default page size
  • :max_per_page - upper clamp for the URL per_page param

schema_module(schema)

Returns the Ecto.Schema module behind a queryable — the module itself, or the source schema of an %Ecto.Query{}. Returns nil when there is no schema to reflect on.