Memory
# SurrealDB Real-Time and Calculation Capabilities ## LIVE SELECT (Real-time Subscriptions) Push changes to connected clients as they commit. No polling needed. ```surql LIVE SELECT * FROM document WHERE source_type = "upload"; LIVE SELECT * FROM notification WHERE read = false; ``` - Returns UUID handle for the subscription - Use KILL $handle to cancel - Works via WebSocket SDKs - Can filter with WHERE clauses ## DEFINE EVENT (Database Triggers) Automatic reactions to data changes, executed at the database level. ```surql DEFINE EVENT document_ingested ON document WHEN $event = "CREATE" THEN { CREATE notification SET kind = "document_ingested", message = "New doc: " + $after.title; }; ``` - Fires on CREATE, UPDATE, DELETE - Can execute any SurrealQL (CREATE, UPDATE, DELETE) - Can call http::post() for outbound webhooks - Need --allow-net flag for HTTP calls - Avoid recursive events (causes computation depth error) ## Table Views (Materialized Aggregates) Auto-updating computed tables with predictable IDs. ```surql DEFINE TABLE doc_stats AS SELECT source_type, count() AS total, math::mean(trust) AS avg FROM document GROUP BY source_type; ``` - Auto-update when source data changes - Predictable IDs for fast lookup: `doc_stats:["upload"]` - Use DROP keyword if you only want the view, not the base table ## math:: Functions (Spreadsheet Calculations) Full calculation engine for computed fields and aggregates: - math::sum(), math::mean(), math::min(), math::max() - math::round(), math::floor(), math::ceil() - math::abs(), math::sqrt(), math::pow() - math::median(), math::percentile() - Works in SELECT, WHERE, ORDER BY clauses ## Event-Driven Curation Pattern 1. Events fire on document/entity/relationship changes 2. Events write to notification table 3. Agent subscribes via LIVE SELECT on notifications 4. Agent processes notifications (re-link, consolidate, elaborate) 5. Agent marks notifications as read ## Spreadsheet-like Features for Documents - Table Views for aggregated stats (by source, by collection, by date) - math:: functions for trust calculations, importance scores - Record range queries for temporal data (ULID-based IDs) - Complex record IDs for efficient time-series queries
Tags: surrealdb, live-queries, events, table-views, math, real-time, calculations, project:intelligentrag, kind:semantic — Source: claude — 2026-07-16 19:15:48 UTC