relic supports two ways to perform computation and define conditions within queries. You can use standard Clojure functions for arbitrary, complex logic, or use the relic expression DSL for simple, inline computations.
When to use Clojure functions
Use Clojure functions when you need complex logic that is difficult to express in the DSL.
Critical Requirement: Functions used as expressions must be referentially transparent (pure) to avoid glitches in the reactive system.
When to use relic expressions
Use the relic expression DSL for:
- Simple inline computations.
- Better ergonomics when working with rows (keywords are automatically substituted for lookups).
- Avoiding Clojure's quote/unquote template syntax.
- Utilizing built-in features like sub-queries, nil-safe function application, and special conditional forms.
Where expressions can be used
Expressions can be applied in the following query clauses:
[:where ...] conditions[:extend ...] extensions[:select ...] projections[:expand ...] expansions[:join ...] and [:left-join ...] clauses[:agg ...] (certain aggregates like sum accept expressions as arguments)[:hash ...] and [:btree ...] indexed expressions
;; Example of using a Clojure function
(defn my-pred? [{:keys [foo]}] (= foo 42))
[[:from :A] [:where my-pred?]]
;; Example of using a relic expression
[[:from :A] [= :foo 42]]