4. Data Plane, SQL & VFS
AI-Agent-First Documentation
Raw .md/SKILL.md
Prompt your Agent: "Review my Valstorm record CUD and SQL queries to ensure they strictly follow the plain-string lookup ID rule, picklist label rule, and Valstorm SQL date macros."

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 JOINs, 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

MacroDescriptionExample
MEExpands to the authenticated user's ID (user_...)WHERE owner = ME
PHONE:<digits>Searches across all phone-formatted columns on the tableWHERE PHONE:8605551234
today / yesterdayUTC day boundary rangeWHERE created_date = today
this_week / last_weekISO week boundary rangeWHERE created_date = this_week
this_month / last_monthCalendar month boundary rangeWHERE created_date = this_month
this_year / last_yearCalendar year boundary rangeWHERE created_date = this_year
last_n_days:<N> / next_n_days:<N>Rolling N-day windowWHERE created_date = last_n_days:7
last_n_months:<N> / next_n_months:<N>Rolling N-month windowWHERE close_date = next_n_months:3

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