<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Kaspars Rancans</title>
    <description>The latest articles on Forem by Kaspars Rancans (@rkaspars).</description>
    <link>https://forem.com/rkaspars</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3531905%2Fb1a863eb-5f87-4b51-8346-ce856c1d8ff0.jpg</url>
      <title>Forem: Kaspars Rancans</title>
      <link>https://forem.com/rkaspars</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rkaspars"/>
    <language>en</language>
    <item>
      <title>Moving the "Brain" to the Database: Architecting a Deterministic, Multi-Tenant OS for UK Trades</title>
      <dc:creator>Kaspars Rancans</dc:creator>
      <pubDate>Sun, 22 Feb 2026 20:01:18 +0000</pubDate>
      <link>https://forem.com/rkaspars/moving-the-brain-to-the-database-architecting-a-deterministic-multi-tenant-os-for-uk-trades-50d8</link>
      <guid>https://forem.com/rkaspars/moving-the-brain-to-the-database-architecting-a-deterministic-multi-tenant-os-for-uk-trades-50d8</guid>
      <description>&lt;p&gt;&lt;strong&gt;The Goal: "Admin-Zero"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Most SaaS tools for tradespeople are just CRUD wrappers with a pretty UI. When building ForgeDash, we threw out the "client-side logic" playbook. We needed a Financial Black Box—a system where the tech disappears, the math is bulletproof, and compliance is a background process.&lt;/p&gt;

&lt;p&gt;Here is a breakdown of the "Database-First" architecture we engineered to solve the complexity of UK trade compliance.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;1. Deterministic Finance: The "Dumb Frontend" Strategy&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In fintech, trusting the client with money is a liability. We isolated 100% of the financial, tax, and business logic inside PostgreSQL RPCs.&lt;/p&gt;

&lt;p&gt;The Problem:&lt;br&gt;
 UK tax law is a labyrinth of thresholds, Class 4 NICs, and allowance tapers.&lt;br&gt;
The Solution: We implemented a Deterministic Tax Engine directly in PL/pgSQL (fn_calc_uk_tax_25_26). It natively handles edge cases like the Personal Allowance Taper (reducing PA by £1 for every £2 earned &amp;gt;£100k) and auto-calculates multi-plan Student Loans (Plans 1, 2, 4, and Postgrad).&lt;/p&gt;

&lt;p&gt;The Result: &lt;br&gt;
Whether a user is on a spotty 4G connection or a desktop, their "Safe-to-Spend" (StP) figure is mathematically identical because it is calculated at the source of truth.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;2. The "Ledger Orchestrator" Pattern&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Compliance shouldn't be a manual task. We built a centralized trigger function (fn_orchestrate_ledger_update) attached directly to the ledger tables.&lt;/p&gt;

&lt;p&gt;Autonomous Recalculation: &lt;/p&gt;

&lt;p&gt;Every INSERT, UPDATE, or DELETE on a transaction automatically triggers a top-down refresh of the Organization’s financial "Pots" (Tax, VAT, NI).&lt;br&gt;
The VAT Sentinel: The orchestrator continuously monitors rolling 12-month turnover. As soon as a business approaches the £90,000 UK VAT threshold, the system proactively alerts the user before they breach compliance.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;3. Immutable Audit Trails (Cryptographic Chaining)&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To ensure "professional-grade" trust, we implemented blockchain-style chaining for the audit logs using pgcrypto.&lt;/p&gt;

&lt;p&gt;Tamper-Evidence: &lt;br&gt;
Every record in the audit_logs table is mathematically linked to the previous entry.&lt;/p&gt;

&lt;p&gt;The Seal: &lt;br&gt;
A BEFORE INSERT trigger generates a SHA-256 hash of the current payload concatenated with the previous_hash. If a single byte of financial history is manually altered by a DBA or a bug, the chain breaks, providing a verifiable corruption record for accountants.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;4. "Vertical Molding" &amp;amp; Dynamic Schema Injection&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Mechanics, plumbers, and electricians use different jargon. Instead of managing multiple codebases, we built a Modular Vertical Framework.&lt;br&gt;
JSONB Domain Injection: Industry-specific terminology (e.g., "VRM" vs. "Asset ID") is stored in a vertical_registry. A custom hook, useMoldedUI, fetches this manifest to inject the correct semantics and UI tokens (accent colors, border radii) at runtime.&lt;/p&gt;

&lt;p&gt;Feature Slots: &lt;br&gt;
We use a "Slot Pattern" to load vertical-specific modules—like a 2D Vehicle Damage Walkaround for mechanics—while keeping the core application logic industry-agnostic.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;5. The "Greasy Finger" UX Standard&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We engineered for the workshop floor, not the boardroom.&lt;br&gt;
Touch Targets: We enforce a strict 48px minimum for all interactive elements to accommodate gloved hands.&lt;/p&gt;

&lt;p&gt;Visual Precision: &lt;br&gt;
We use Tabular Numerals (tabular-nums) for all financial data. This prevents the "layout jitter" common in React apps when numbers update in real-time.&lt;/p&gt;

&lt;p&gt;Privacy: &lt;br&gt;
All PII is encrypted with AES-256 at rest within the database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Stack&lt;/strong&gt;&lt;br&gt;
Frontend: React 19, Tailwind CSS v4, Vite.&lt;br&gt;
Backend: Supabase / PostgreSQL (Heavy PL/pgSQL &amp;amp; Row Level Security).&lt;br&gt;
AI Integration: n8n + Gemini 3.0 (Multimodal RAG for OCR receipt scanning).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ForgeDash is Compliance-as-Code. It is a high-performance co-pilot designed to make the modern trade business "Admin-Zero."&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;(Open to feedback on the PL/pgSQL approach—let me know your thoughts in the comments!)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>ai</category>
      <category>devops</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
