Skip to content

Introduction ​

TypeRule is a programming language for business software. You describe your business once — the data, the money, the rules, the processes, who may do what — and TypeRule gives you the database, the HTTP API, a web app to work in, and a complete, verifiable history of every value.

Its motto is i conti quadrano: the books balance. TypeRule is built so that they always do.

Who it is for ​

  • Developers building ERPs, back offices and line-of-business apps: orders, invoices, stock, purchasing, production, accounting, approvals. The kind of software where a wrong cent, a lost update or an unexplained number is a real bug.
  • Teams that are tired of writing the same layers again and again: tables, migrations, ORM classes, validation, REST endpoints, permission checks, audit logs, admin screens.
  • People who need to trust the numbers: accountants, auditors, managers. A TypeRule model reads almost like a specification, and every value can explain where it comes from.

The mental model ​

In most stacks, the code is a program that manipulates a database. In TypeRule, the code is the model, and everything else is derived from it:

You writeTypeRule gives you
entity, fields, relationstables with foreign keys, UNIQUE and CHECK constraints (SQLite or PostgreSQL)
formulas (total = subtotal + vat)values that are always up to date, also stored as columns for outside readers
check, warn, where, required when, uniquerules enforced at every commit, from every entry point
flow and actionstate machines and atomic operations, exposed as API calls and buttons
frozen whendocuments that stop changing once they are confirmed or posted
ledger and post toappend-only registers (stock, receivables, general ledger) with automatic reversals
rolepermissions applied to every row and field, in the API and in the web client
test, fixturefast, deterministic tests that run in memory or on a real database
migration, changesafe schema evolution and audited, approved bulk data changes

There is no "database layer" and no "business layer" to keep in sync: they are the same text.

A taste ​

typerule
entity Order:
    customer: Customer
    lines: [OrderLine]
    state: enum(Draft, Confirmed, Shipped, Cancelled) default Draft

    subtotal = lines.sum(l => l.amount)
    vat = vat_summary.sum(v => v.vat)
    total = subtotal + vat + shipping

    frozen when state is not Draft except notes, cancel_reason

    flow state:
        Draft -> Confirmed                  by confirm
        Confirmed -> Shipped                by ship
        Draft | Confirmed -> Cancelled      by cancel

    action confirm:
        require lines.count() > 0, "An order needs at least one line"
        number = next_number(current.company, "SO")
        confirmed_at = now()


entity OrderLine:
    order: Order parent
    product: Product
    qty: Qty<product.uom> where > 0
    price: Price<EUR, product.uom> default product.price
    discount: Percent where 0%..50% default order.customer.discount

    amount = (qty * price * (1 - discount)).round()

A few things to notice — they are what makes TypeRule different:

  • Money, prices and quantities are types. qty * price multiplies a Qty<kg> by a Price<EUR, kg> and gives money. Adding euros to dollars, or kilograms to pieces, does not compile. Money is exact (no floating point), and you must say where you round.
  • = is a formula, default is a snapshot. amount always follows the line; price default product.price copies the list price when the line is created, so a later price change does not rewrite old orders.
  • A confirmed order is a fact. frozen when makes it read-only and makes its formulas see the world as it was when it was frozen: tomorrow's VAT rate does not change yesterday's order.
  • The state machine is declared, not coded. confirm can only run in Draft; everything an action does happens in one transaction, or not at all.

The compiler checks names, types, units, currencies, optional values, formula cycles and state machines before anything runs:

text
error: cannot add Money<EUR> and Money<USD>
   ┌─ tests/types.test.tr:34:17
   │
34 │     let total = 7.50 EUR + 2.00 USD
   │                 ^^^^^^^^^^^^^^^^^^^

Compared to "framework + ORM + SQL" ​

Framework + ORM + SQLTypeRule
SchemaSQL migrations, ORM classes, API serializers, form definitions — kept in sync by handone model; tables, API and UI are derived
Derived valuescomputed in code, cached, recomputed by jobs, sometimes staleformulas, always current, materialized at every commit
Money and unitsdecimals or floats, currency in a separate column, conversions by conventionMoney<EUR>, Qty<kg>, Price<EUR, kg> checked by the compiler; exact arithmetic
Validationin the controller, in the form, in a trigger — often in only one of themcheck, where, unique, required when: enforced on every write, from any entry point
Status changesif order.status == ... scattered in the codeflow + action: only the declared transitions exist
Auditan audit table someone remembers to write toevery value has a history and a cause chain (why(record)), in a hash-chained log
Permissionsmiddleware and query filters per endpointrole rules on rows and fields, applied by the engine everywhere
Concurrencylast write wins, unless you add lockingoptimistic concurrency: a commit on data changed by others is refused with who, what and when
Schema changesa migration script that hopes the data is cleanmigration blocks with dry run on the real data, provisional values and verification

TypeRule is not a low-code tool: it is a typed language with a compiler, a formatter, a language server, tests and code review in git. It just happens to know what an invoice is.

How a project runs ​

text
 .tr files ──▶ typerule check ──▶ typerule test ──▶ typerule db init / migrate ──▶ typerule serve
 (the model)    names, types,     in memory or     tables, history,            HTTP API +
                units, cycles     on SQLite        signed commits              web client

The source of truth in the database is the history: one entry per field per transaction, each commit chained to the previous one by a hash. The ordinary tables (order, order_line…) are projections of that history, written in the same transaction, so any SQL tool can read them — and typerule db verify can prove they were not tampered with.

Where to go next ​

  • Installation: build the typerule command and set up your editor.
  • Quick start: a running app in five minutes.
  • Tutorial: build a small shop step by step — customers, products, orders, invoices, stock, permissions, a database and an API.

TypeRule: the books balance.