cquill/query/builder

Types

A QueryModifier is a function that transforms a query. This is the foundation for composable query building.

pub type QueryModifier(s) =
  fn(ast.Query(s)) -> ast.Query(s)

A Scope is a named, reusable query modifier. This is similar to Ecto’s query scopes.

pub type Scope(s) {
  Scope(name: String, modifier: fn(ast.Query(s)) -> ast.Query(s))
}

Constructors

Values

pub fn active() -> fn(ast.Query(s)) -> ast.Query(s)

Create an “active” filter. Assumes an active boolean field.

pub fn and_then(
  first: fn(ast.Query(s)) -> ast.Query(s),
  second: fn(ast.Query(s)) -> ast.Query(s),
) -> fn(ast.Query(s)) -> ast.Query(s)

Compose two modifiers into a single modifier.

pub fn apply_scope(
  query: ast.Query(s),
  scope: Scope(s),
) -> ast.Query(s)

Apply a scope to a query.

pub fn apply_scopes(
  query: ast.Query(s),
  scopes: List(Scope(s)),
) -> ast.Query(s)

Apply multiple scopes to a query.

pub fn by_user(user_id: Int) -> fn(ast.Query(s)) -> ast.Query(s)

Create a modifier for “by user” filtering. Common pattern for multi-tenant queries.

pub fn clone(q: ast.Query(s)) -> ast.Query(s)

Clone a query, optionally resetting certain parts.

pub fn clone_without_conditions(q: ast.Query(s)) -> ast.Query(s)

Clone a query but clear all WHERE conditions.

pub fn clone_without_order(q: ast.Query(s)) -> ast.Query(s)

Clone a query but clear all ORDER BY.

pub fn clone_without_pagination(q: ast.Query(s)) -> ast.Query(s)

Clone a query but clear pagination.

pub fn compose(
  modifiers: List(fn(ast.Query(s)) -> ast.Query(s)),
) -> fn(ast.Query(s)) -> ast.Query(s)

Compose multiple modifiers into a single modifier. Modifiers are applied left to right.

Example

let active_admins = builder.compose([
  builder.filter(query.eq_bool("active", True)),
  builder.filter(query.eq_string("role", "admin")),
  builder.with_limit(10),
])
user_query |> active_admins
pub fn conditions_equivalent(
  query_a: ast.Query(a),
  query_b: ast.Query(b),
) -> Bool

Check if two queries have equivalent conditions (order-independent).

pub fn count_conditions_deep(q: ast.Query(s)) -> Int

Count total conditions including nested AND/OR.

pub fn filter(
  condition: ast.Condition,
) -> fn(ast.Query(s)) -> ast.Query(s)

Create a filter modifier from a condition. Returns a function that adds the condition to any query.

Example

let active_filter = builder.filter(query.eq_bool("active", True))
user_query |> active_filter
pub fn filter_eq_bool(
  field: String,
  value: Bool,
) -> fn(ast.Query(s)) -> ast.Query(s)

Create a filter that requires a field to equal a specific boolean.

pub fn filter_eq_int(
  field: String,
  value: Int,
) -> fn(ast.Query(s)) -> ast.Query(s)

Create a filter that requires a field to equal a specific integer.

pub fn filter_eq_string(
  field: String,
  value: String,
) -> fn(ast.Query(s)) -> ast.Query(s)

Create a filter that requires a field to equal a specific string.

pub fn filter_gt_int(
  field: String,
  value: Int,
) -> fn(ast.Query(s)) -> ast.Query(s)

Create a filter for values greater than a threshold.

pub fn filter_not_null(
  field: String,
) -> fn(ast.Query(s)) -> ast.Query(s)

Create a filter for non-null values.

pub fn filter_null(
  field: String,
) -> fn(ast.Query(s)) -> ast.Query(s)

Create a filter for null values.

pub fn get_condition_fields(q: ast.Query(s)) -> List(String)

Extract all field names referenced in conditions.

pub fn identity() -> fn(ast.Query(s)) -> ast.Query(s)

Create an identity modifier (does nothing).

pub fn merge_conditions(
  target: ast.Query(s),
  source: ast.Query(a),
) -> ast.Query(s)

Merge conditions from source query into target query. Preserves target’s source, select, and other settings.

pub fn merge_order_bys(
  target: ast.Query(s),
  source: ast.Query(a),
) -> ast.Query(s)

Merge order_bys from source query into target query.

pub fn merge_pagination(
  target: ast.Query(s),
  source: ast.Query(a),
) -> ast.Query(s)

Merge pagination (limit/offset) from source to target. Only applies if target doesn’t already have pagination.

pub fn not_deleted() -> fn(ast.Query(s)) -> ast.Query(s)

Create a “soft delete” filter that excludes soft-deleted records. Assumes a deleted_at field that is NULL for non-deleted records.

pub fn oldest_first() -> fn(ast.Query(s)) -> ast.Query(s)

Create an “oldest first” ordering modifier. Assumes a created_at timestamp field.

pub fn order_asc(
  field: String,
) -> fn(ast.Query(s)) -> ast.Query(s)

Create a modifier that adds ORDER BY ASC.

pub fn order_desc(
  field: String,
) -> fn(ast.Query(s)) -> ast.Query(s)

Create a modifier that adds ORDER BY DESC.

pub fn published() -> fn(ast.Query(s)) -> ast.Query(s)

Create a “published” filter for content models. Assumes a published boolean field.

pub fn recent_first() -> fn(ast.Query(s)) -> ast.Query(s)

Create a “recent first” ordering modifier. Assumes a created_at timestamp field.

pub fn scope(
  name: String,
  modifier: fn(ast.Query(s)) -> ast.Query(s),
) -> Scope(s)

Create a named scope.

pub fn when(
  condition: Bool,
  modifier: fn(ast.Query(s)) -> ast.Query(s),
) -> fn(ast.Query(s)) -> ast.Query(s)

Conditionally apply a modifier. If the condition is true, apply the modifier; otherwise, return unchanged.

pub fn when_some(
  maybe_value: option.Option(a),
  modifier_factory: fn(a) -> fn(ast.Query(s)) -> ast.Query(s),
) -> fn(ast.Query(s)) -> ast.Query(s)

Apply a modifier based on an optional value. If the value is Some, apply the modifier factory with that value.

pub fn with_distinct() -> fn(ast.Query(s)) -> ast.Query(s)

Create a modifier that makes the query distinct.

pub fn with_limit(count: Int) -> fn(ast.Query(s)) -> ast.Query(s)

Create a modifier that sets the limit.

pub fn with_offset(
  count: Int,
) -> fn(ast.Query(s)) -> ast.Query(s)

Create a modifier that sets the offset.

pub fn with_pagination(
  page: Int,
  per_page: Int,
) -> fn(ast.Query(s)) -> ast.Query(s)

Create a modifier that sets pagination.

pub fn with_select(
  fields: List(String),
) -> fn(ast.Query(s)) -> ast.Query(s)

Create a modifier that sets specific fields to select.

Search Document