Depot.Query
Depot.Query: composable, lazy query builder for in-memory data.
Builds a query value from a Depot.Schema that accumulates operations (where, select, order_by, limit, offset). The query is lazy — no filtering happens until execute/2 is called with a list of rows.
Usage:
let q = Depot.Query.from(schema()) |> Depot.Query.where_(fn row -> record_get(row, "age") > Some(18)) |> Depot.Query.order_asc("name") |> Depot.Query.limit(10)
let results = Depot.Query.execute(q, all_rows)
Multiple where_ calls are AND-combined. Order of operations in execute:
- Filter (where predicates, AND-combined)
- Sort (order_by fields)
- Offset (drop N rows)
- Limit (take N rows)
- Project (select mapping)
Types
Functions
Wrap a Depot.Schema into a fresh query with no operations.
let q = Depot.Query.from(schema())
Add a where predicate. Multiple calls are AND-combined.
The predicate receives a row (record) and must return a Bool.
q |> Depot.Query.where_(fn row -> record_get(row, "active") == Some(true))
Set the select projection. Replaces any previous select.
The projection receives a row (record) and returns a new value.
q |> Depot.Query.select(fn row -> { name = record_get(row, "name"), email = record_get(row, "email") } )
Add an order_by clause with explicit direction ("asc" or "desc").
Multiple calls accumulate: earlier calls have higher priority.
q |> Depot.Query.order_by("name", "asc") |> Depot.Query.order_by("age", "desc")
Add an ascending order_by clause.
q |> Depot.Query.order_asc("name")
Add a descending order_by clause.
q |> Depot.Query.order_desc("name")
Set a limit on the number of results. Replaces any previous limit.
q |> Depot.Query.limit(10)
Set an offset (skip N rows). Replaces any previous offset.
q |> Depot.Query.offset(20)
Returns the schema associated with this query.
Depot.Query.schema(q)
Execute a query against a list of rows (records).
Applies operations in order:
- where predicates (AND-combined filter)
- order_by (stable insertion sort)
- offset (drop N rows)
- limit (take N rows)
- select projection (map)
let results = Depot.Query.execute(query, rows)
Count the results of a query without materializing them. Only applies where predicates (ignores order_by, limit, offset, select).
let n = Depot.Query.count(query, rows)
Returns true if the query would return any results.
Depot.Query.exists(query, rows)
Returns the first result of a query, or None.
Depot.Query.first(query, rows)