March Docs

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:

  1. Filter (where predicates, AND-combined)
  2. Sort (order_by fields)
  3. Offset (drop N rows)
  4. Limit (take N rows)
  5. Project (select mapping)

Types

typeQueryQuery = Query(a, List(b), c, List((String, String)), d, e)#

Functions

fnfromfrom(schema)#

Wrap a Depot.Schema into a fresh query with no operations.

let q = Depot.Query.from(schema())

fnwhere_where_(query, predicate)#

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))

fnselectselect(query, projection)#

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") } )

fnorder_byorder_by(query, field, direction)#

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")

fnorder_ascorder_asc(query, field)#

Add an ascending order_by clause.

q |> Depot.Query.order_asc("name")

fnorder_descorder_desc(query, field)#

Add a descending order_by clause.

q |> Depot.Query.order_desc("name")

fnlimitlimit(query, n)#

Set a limit on the number of results. Replaces any previous limit.

q |> Depot.Query.limit(10)

fnoffsetoffset(query, n)#

Set an offset (skip N rows). Replaces any previous offset.

q |> Depot.Query.offset(20)

fnschemaschema(query)#

Returns the schema associated with this query.

Depot.Query.schema(q)

fnexecuteexecute(query, rows)#

Execute a query against a list of rows (records).

Applies operations in order:

  1. where predicates (AND-combined filter)
  2. order_by (stable insertion sort)
  3. offset (drop N rows)
  4. limit (take N rows)
  5. select projection (map)

let results = Depot.Query.execute(query, rows)

fncountcount(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)

fnexistsexists(query, rows)#

Returns true if the query would return any results.

Depot.Query.exists(query, rows)

fnfirstfirst(query, rows)#

Returns the first result of a query, or None.

Depot.Query.first(query, rows)