# Data Model Rules, SQL Engine & VFS Hybrid Search Every developer and AI agent building on Valstorm (Cloud or COSS) **must** follow these core data model invariants. --- ## 1. Critical Data Model Rules (Zero-Exception Invariants) ### Rule 1: Relationship Lookups Store Plain String IDs Only Never write dictionary lookups (e.g. `{"id": "user_123", "name": "Jared"}`) when creating or updating records. - **Single Lookup (`format: "lookup"`):** Store the plain Base62 string ID (e.g. `"owner": "user_qBulInDu18OcylWP"`, `"account": "acc_9x8y7z"`). - **Lookup List (`format: "lookup_list"`):** Store a JSON array of plain string IDs (e.g. `"contacts": ["con_123", "con_456"]`, `"vaults": ["vaul_abc"]`). - Display name hydration happens dynamically at read/query time (`hydrate=True`), never in persistent storage. ### Rule 2: Picklists Store Human-Readable Tag Labels (Not Tag IDs) Fields with `format: "picklist"` (such as `status`, `priority`, `type`, `lead_source`, `utm_source`) must be populated with the **string label/value** (e.g. `"status": "Active"`, `"priority": "High"`), **never** a `tag_...` record ID. ### Rule 3: Native UTC Timestamps & Base62 Prefixes - Every object has a deterministic 3–4 character Base62 ID prefix (e.g. `con_` for `contact`, `tick_` for `ticket`, `file_` for `file`, `vaul_` for `vault`, `aich_` for `ai_chat`). - Custom objects and fields created by non-system orgs automatically receive the `__c` suffix. --- ## 2. Valstorm ANSI SQL Engine & Dynamic Macros (`POST /v1/query/sql`) Open Valstorm compiles Valstorm SQL directly to parameterized PostgreSQL AST via `sqlglot`, supporting native `JOIN`s, aggregations, and dynamic macros: ```sql SELECT d.id, d.name, d.amount, c.name AS contact_name FROM deal d JOIN contact c ON d.primary_contact = c.id WHERE d.owner = ME AND d.created_date = last_n_days:30 ORDER BY d.amount DESC LIMIT 50 ``` ### Supported SQL Macros | Macro | Description | Example | | :--- | :--- | :--- | | `ME` | Expands to the authenticated user's ID (`user_...`) | `WHERE owner = ME` | | `PHONE:` | Searches across all phone-formatted columns on the table | `WHERE PHONE:8605551234` | | `today` / `yesterday` | UTC day boundary range | `WHERE created_date = today` | | `this_week` / `last_week` | ISO week boundary range | `WHERE created_date = this_week` | | `this_month` / `last_month` | Calendar month boundary range | `WHERE created_date = this_month` | | `this_year` / `last_year` | Calendar year boundary range | `WHERE created_date = this_year` | | `last_n_days:` / `next_n_days:` | Rolling N-day window | `WHERE created_date = last_n_days:7` | | `last_n_months:` / `next_n_months:` | Rolling N-month window | `WHERE close_date = next_n_months:3` | --- ## 3. Virtual File Service (VFS) & `pgvector` Hybrid RRF Search Open Valstorm stores hierarchical Knowledge Vaults (`vault`) and versioned files (`file`, `file_version`) backed by S3/Spaces and indexes document chunks into PostgreSQL `file_chunk` using: 1. **Dense Vector Search (`pgvector`):** 768-dimension HNSW cosine distance (`embedding <=> $2::vector(768)`). 2. **Lexical Full-Text Search (`tsvector`):** PostgreSQL GIN index (`websearch_to_tsquery('english', $1)`). 3. **Reciprocal Rank Fusion (RRF):** Blends lexical rank and semantic vector rank (`1 / (60 + rank_lex) + 1 / (60 + rank_vec)`) in a single SQL CTE (`GET /v1/vfs/search?query=...`).