Tutorial: Building with CleaveDB
Live computed feeds
In many real-time applications, incoming raw data must be mathematically transformed, normalized, or unit-converted before display. Rather than writing client-side math functions in every frontend application, CleaveDB pairs ENRICH with LISTEN to create live computed feeds.
Virtual enrichment on stream arrival
When an ENRICH rule is active on a bucket, any incoming POUR or CHANGE mutation evaluates the formula on the fly and broadcasts the computed fields alongside raw data:
-- 1. Declare dynamic temperature conversion formula
ENRICH sensors WITH fahrenheit AS (celsius * 9/5) + 32
-- 2. Open real-time stream subscription
LISTEN TO sensorsThe virtual field fahrenheit does not consume disk space in the primary B-tree, but is dynamically calculated by the query interpreter during broadcast dispatch.
Publishing raw data
When an IoT sensor or backend service publishes raw telemetry:
POUR INTO sensors "s1" {"celsius": 20}Subscribed clients instantly receive the fully computed payload:
{
"event": "POUR",
"bucket": "sensors",
"id": "s1",
"data": {
"celsius": 20,
"fahrenheit": 68
},
"timestamp": 1728562500
}The frontend directly renders 68°F without executing math routines or risking rounding discrepancies across different browser platforms.
Real-time business formulas
You can compute complex multi-field metrics such as profit margins, tax rates, or risk scores:
-- Compute profit margin on sales events
ENRICH sales WITH margin AS price - cost, tax AS price * 0.12
-- Stream to accounting dashboards
LISTEN TO salesEvery sale logged to the database immediately pushes margin and tax to active administrative dashboards.
