Appearance
Entities
Entities are the data of the application: each one becomes a table in the database, a resource of the API and a sheet in the web client. This page covers every declaration that defines data: entity, record, trait, extend, enum, fields and their modifiers, relations, temporary entities, seed, const, functions and events.
| Declaration | What it is |
|---|---|
entity Name: | Records with an identity, stored and versioned (every change is kept). |
record Name: | A composite value without identity (an address), stored inside the record that holds it. |
trait Name: | Fields shared by several entities, and a type that can refer to any of them. |
extend Name: | Adds members to an entity declared in another module. |
enum Name: A, B | A closed set of values, which may carry data. |
ledger Name: | An append-only journal fed by post to. See Ledgers. |
temporary entity Name: | A staging table that exists only while a migration runs. |
seed: | Reference data created with the database. |
const NAME: T = value | A named constant. |
fn name(…) -> T: | A pure function. |
event Name(field: T, …) | A message for the outside world, sent with emit. |
entity
typerule
entity Customer:
name: Text unique
email: Email?
credit_limit: Money<EUR> default 1_000.00 EUR
orders: [Order]
open_total = orders.where(o => o.state is Confirmed).sum(o => o.total)
check open_total <= credit_limit, "Credit limit exceeded"An entity's body is a list of members:
| Member | Page |
|---|---|
| fields and formulas | below |
check, warn, unique (…), frozen when …, live … | Rules |
retain 10 years after date, expire 2 days after created_at, history 1 year, history last 10, history none, deletable, trash 30 days | Retention and erasure |
flow field: and action name: | Flows and actions |
post to Ledger: | Ledgers |
fn name(…): | methods |
load from "file.csv" | temporary entities |
entity Name is Trait1, Trait2: makes the entity implement traits.
Implicit fields
Every entity has these fields without declaring them:
| Field | Type | Value |
|---|---|---|
id | Text | the record's identifier |
created_at | DateTime | when it was created (the clock of the transaction) |
created_by | User? | who created it; none for the system |
updated_at | DateTime | when it was last written (the clock of that transaction); created_at until then |
updated_by | User? | who last wrote it; none for the system |
version | Int | the number of values written to it so far |
warnings | [Text] | the messages of its failed warn rules (and of its lines') |
The whole history of every field, with the cause of each change, is kept as well: see why() in Expressions and the history endpoint of the HTTP API.
The history is not a value in expressions: rec.history, rec.as_of(…) and rec.explain() are compile errors that say where to find it (why(rec) for the cause, the history endpoint of the API for the past values).
Fields
| Form | Meaning |
|---|---|
name: T | A stored field. Required: a record cannot be saved without a value. |
name: T? | An optional stored field: it may be none. |
name: T default expr | A stored field with an initial value, computed once when the record is created. It can be changed afterwards. |
name = expr | A formula: always up to date, read-only, its type inferred. |
name: T = expr | A formula with a declared type, checked against the expression. |
name default expr | No type: a new initial value for a field inherited from a trait. |
Lists ([T]) and sets ({T}) are never required: an absent collection is empty.
typerule
entity OrderLine:
order: Order parent
product: Product
qty: Qty<pcs> where > 0
price: Price<EUR, pcs> default product.price # snapshot of the list price
discount: Percent where 0%..100% default 0%
amount = (qty * price * (1 - discount)).round() # always follows qty, price, discountThe difference between default and = is the most important decision when modelling: a formula follows the data forever, a default takes a picture once. See Defaults and formulas.
A formula is stored in a column of its entity's table. Most formulas (rollups over lines, rollups of rollups, values read through references, recursions over a tree) are kept up to date by each commit and read from that column, so opening a region or sorting customers by revenue does not walk their orders: see Formulas kept up to date.
Modifiers
Modifiers follow the type, in any order:
| Modifier | Meaning | Example |
|---|---|---|
where > x (also >=, <, <=, ==, !=) | The value must compare that way to x. | qty: Qty<pcs> where > 0 |
where a..b | The value must lie between a and b, both included. | decimals: Int where 0..4 default 2 |
where v => cond | The value must satisfy the condition. For a list or a set, each element must. | code: Code where c => c.len() == 3 |
required when cond | On an optional field: a value is required while cond holds. Reading it gives T, not T?. | vat_number: VatNumber? required when kind is Organization |
unique | No two records may have the same value (none excepted). | login: Code unique |
personal | Data about a person: erase empties it. Optional fields only. | email: Email? personal |
expire N unit after field | Emptied when the time ends (Retention and erasure). Optional fields only. | notes: Text? expire 30 days after created_at |
retain N unit after field | Kept at least that long: erase leaves it until then. | vat_number: VatNumber? personal retain 10 years after created_at |
parent | The record is a line of the referenced document (composition). | order: SalesOrder parent |
per company | One value per company. | list_price: Price<company.currency, uom> per company |
via field | The inverse of a collection goes through this field of the other entity. | contacts: [Partner] via parent |
through Entity | A many-to-many relation through a linking entity. | vendors: {Partner} through SupplierInfo |
max size | On a File field: the largest file it takes (KB, MB, GB); on a list, of each file. | receipt: File<pdf, image>? max 10 MB |
A where condition can read the other fields of the record and the referenced records: purchase_uom: Uom where u => u.category == uom.category. Violations are reported when the transaction ends, for example Product.hs_code = 1: does not satisfy its `where` . The where on a reference also tells the type checker that two currencies are equal; see Types.
Modifiers that do not fit on one line can go on the following lines, indented, together with the default or the =:
typerule
vendor: Partner? where v => v.as_vendor != none
required when route is Buy
default product.suppliers.sort_by(s => s.lead_time).first()?.vendorPer-company fields
A field marked per company holds a separate value for each company. In its type and its formula, company is the company whose value is computed.
typerule
extend Product:
list_price: Price<company.currency, uom> per company
stock_qty: Qty<uom> per company =
Stock.where(s => s.product == self and s.company == company).sum(s => s.qty)| Expression | Value |
|---|---|
product.list_price | the value for current.company |
product.list_price[c] | the value for company c (none if it has none) |
Writing product.list_price = … sets the value of the current company. A missing value of a per-company field is not reported as missing.
Files
A File field holds a file the users upload (or a document of the app, attach); a [File] holds several. Its type says which files it takes, and max how large (Types):
typerule
entity Expense:
title: Text
receipt: File<pdf, image>? max 10 MB
photos: [File<image>] max 2 MBThe bytes are never in the database nor in its log: they are in the store of files (.typerule/files of the project, or the bucket of [files]), and the field holds their SHA-256 (sha256:<hex>). A file goes in in three steps, the bytes going straight from the browser to the store:
POST /v1/uploadswith the field, the file's name and size: refused at once if the user may not write the field, or if the name or the size say the field does not take it. The answer is a signed URL to put the bytes to, valid 15 minutes.- The browser puts the bytes to that URL.
- A transaction writes
upload:<id>to the field. Before it runs, the server reads the bytes, tells their type from them, checks it and the size against the field, and keeps the file under its SHA-256; the field then holdssha256:<hex>. A file refused says why:report.pdf: the file is an application/x-msdownload, but Expense.receipt accepts pdf, image,scan.pdf: the file is 12.4 MB, but Expense.receipt takes files up to 10 MB.
An upload is for one user and one field, and is named once. One never named is removed with its bytes after 24 hours. A File field written through the API takes an upload, or a file it already has (to keep the files of a list while another is added): never a sha256: of another file.
Reading the file needs the right to read the field: GET /v1/records/Expense/45/files/receipt (/files/photos/0 for the first of a list) sends who may to a signed URL, valid 5 minutes. PDFs and images are shown in the page; other files, and any file with ?download, are downloaded. SVG and HTML are never shown in the page. See the HTTP API.
Relations
| Declaration | Relation |
|---|---|
customer: Customer | many-to-one: each order has one customer |
customer: Customer? | optional many-to-one |
orders: [Order] | one-to-many: the orders whose customer is this record (the inverse is computed, not stored) |
contacts: [Partner] via parent | one-to-many through a named field, when the inverse is ambiguous |
order: SalesOrder parent | composition: the line belongs to the order |
tags: {Tag} | many-to-many: a stored set of references |
partners: {Partner} via tags | the inverse of a stored set |
vendors: {Partner} through SupplierInfo | many-to-many through a linking entity with attributes |
origin: Document? | a reference to any entity that implements the trait Document |
For orders: [Order], the inverse field is the first field of Order whose type is the owner (or one of the owner's traits). Use via to name it explicitly.
A list of child records can be given when the parent is created; their back-reference is set automatically:
typerule
let o = Order(customer=anna, lines=[OrderLine(product=pen, qty=4 pcs)])
expect o.lines.single().order == oComposition: parent
A field marked parent makes the record a line of its document. A line:
- is frozen with its document (
frozen when): it cannot change, its formulas read the data as of the freezing (except those it declareslive), and it is editable again when the document is unfrozen; - follows the permissions of its document: reading or changing a line is reading or changing the document (Security);
- is copied with its document by
copy().
Lines can be read without ?.: l.order.customer.
record
(A record can also describe a document or a file of another system, read and written as JSON, XML, CSV or fixed-width lines: record ShopOrder from json:, record PosRow from csv(sep=";"):, with at "…" on its fields. See Formats.)
A record is a value with named parts and no identity of its own: two equal addresses are the same address. It is stored inside the entity that holds it.
typerule
record Address:
street: Text
city: Text
zip: Text
country: Country
entity Partner:
name: Text
address: Address
delivery_addresses: [Address]typerule
let p = Partner(name="Rossi Spa",
address=Address(street="Via Roma 1", city="Milano", zip="20100", country=it))
expect p.address.city == "Milano"In the field types of a record, owner is the record or entity that holds it:
typerule
record CustomerRole:
credit_limit: Money<owner.currency>? # the currency of the Partner holding the roleThe rules written inside a record (where, required when, check, warn) are checked when the record that holds it is saved, also for each element of a list of records and for records inside records; a warn goes to the holding record:
typerule
record PostalAddress:
street: Text
zip: Text where z => z.len() == 5
country: Code default "IT"
province: Code? required when country == "IT"text
PostalAddress.zip = 1: does not satisfy its `where`trait
A trait declares fields (and their defaults) that several entities share. An entity that implements a trait has its fields as if declared in the entity.
typerule
trait Document:
company: Company default current.company
number: Code?
date: Date default today()
entity Invoice is Document:
number default next_number(company, "INV") # a new default for an inherited field
total: Money<company.currency>A trait is also a type: a field of type Document can refer to any entity that implements it, and in the database the reference is a real foreign key. Test which entity it is with is and take it apart with match:
typerule
origin: Document?
check origin is not SalesLine or direction is Out, "A sales line on a vendor bill"
kind_label: Text = match origin:
SalesOrder(o) => "sale"
PurchaseOrder(o) => "purchase"
_ => "other"Document.all() and Document.where(…) scan the records of every implementing entity (on a database, a query over one entity is answered in SQL: see Storage).
check, warn and unique (…) written in a trait apply to every entity that implements it (also through another trait). A unique holds among the records of each implementing entity, as in the database, where every entity has its own table: Document unique (company, number) lets an invoice and an order have the same number.
extend
extend adds members to an entity declared elsewhere, typically in a lower module (or in the standard library). The members behave as if they had been written in the entity.
typerule
extend Company:
vat_number: VatNumber unique
po_approval_limit: Money<currency>?
extend SalesOrder:
live invoices, invoice_status
invoices = lines.flat_map(l => l.invoice_lines).map(i => i.invoice).distinct()
action create_invoice -> Invoice:
require state in {Confirmed, Locked}, "Only confirmed orders can be invoiced"
…extend accepts fields, formulas, check, warn, unique (…), actions, functions, post to and live, and two members only when the entity does not have them yet:
| Member | In an extend |
|---|---|
flow field: | allowed on a field that has no flow; a second flow on the same field is an error: `SalesOrder.state` already has a flow (add the transitions to the existing one) |
frozen when … | allowed if the entity has none; otherwise an error: `SalesOrder` already has `frozen when` (declare the new fields live, or list them in except where the entity is declared) |
load from | an error: it belongs in the declaration of a temporary entity |
An entity of the standard library (Company, User, Sequence, Currency, …) is extended, never declared again: entity Company: in a project is an error, `Company` is declared in the standard library (`std.org`): use `extend Company:`.
enum
typerule
enum ProductType: Storable, Consumable, Service
enum AccountType:
Receivable, Payable, Bank, Cash
Income, Expense
enum PriceTarget:
AllProducts
OneProduct(product: Product)
InCategory(category: Category)An enum can also be written inline, as the type of a field. This is the usual way to declare the states of a document:
typerule
state: enum(Draft, Confirmed, Done, Cancelled) default DraftVariants are written by their bare name (Draft, Storable); the type the context expects decides which enum they belong to. A variant that does not belong to the expected enum is an error: `Archived` is not a value of enum(Draft, Confirmed, Done, Cancelled).
Test a value with is and in, or take it apart with match, which must cover every variant:
typerule
specificity: Int = match target:
OneProduct(_) => 3
InCategory(_) => 2
AllProducts => 1A variant that carries data is constructed like a call, with its fields by name or in the declared order: OneProduct(product=p) or OneProduct(p). A missing field (`OneProduct` needs `product`), an unknown one (`OneProduct` has no field `x`) and arguments to a variant without data (`AllProducts` has no fields) are compile errors.
Creating records
An entity name called with named arguments creates a record:
typerule
let anna = Customer(name="Anna Bianchi")
let o = Order(customer=anna, lines=[OrderLine(product=pen, qty=4 pcs, discount=10%)])| Rule | Detail |
|---|---|
| Named arguments only | `Line(…)` takes named arguments: `Line(field=value)` |
| Fields only | `Line` has no field `nope`; a formula cannot be set: `amount` is a formula: it cannot be set; the field of a flow takes only its initial state (Flows) |
| Defaults | Fields not given take their default, computed at the end of the statement (or when first read), so a default can use the other fields given and other defaults. |
| Validation | where, required, unique and check rules are verified when the transaction ends, not at each assignment. |
| Traits | A trait cannot be created: `Document` is a trait: create one of its entities instead. |
copy()
record.copy(field=value, …) creates a new record with the same stored values, then applies the given values. It does not copy formulas, the state of a flow (it restarts from its default), the field number, or fields that are unique on their own. Lines (children whose back-reference is parent) are copied too.
typerule
let credit = inv.copy(kind=CustomerCredit, reverses=inv, date=today(), notes=reason)A record is not deleted unless its entity says so. A record that should no longer count is usually cancelled, reversed or deactivated by the model (a state, an active flag): an invoice, an order, a payment keep their place in the history. An entity whose records may go, the pages of a website, the sessions of a login, says deletable, and may keep a trash first (Retention and erasure):
typerule
entity Page:
deletable, trash 30 days
history last 10
slug: Text unique
title: TextTemporary entities
A temporary entity is a staging table for a migration: its records exist only while the migration runs and are removed before it commits. With load from, its records are read from a CSV file (path relative to the project root).
typerule
temporary entity CustomsCode:
product_code: Text
code: Code where c => c.len() == 8
load from "customs.csv"text
product_code,code
A,73181500
B,35069100| CSV rule | Detail |
|---|---|
| Header | The first line names the fields, in any order. |
| Separator and quotes | , separates cells; "…" quotes a cell, with "" for a quote inside it. |
| Ignored lines | Empty lines and lines starting with #. |
| Empty cell | none. |
| Values | Int, Decimal, Bool (true, yes, 1), Date (YYYY-MM-DD), enum variants by name, text otherwise. |
| References | A cell for a reference field is matched against the target's unique fields first, then its other stored fields: no Product matches `X` if nothing matches. |
| Errors | Reported with the file's path and the line: …/customs.csv line 3: `x` is not an integer. |
seed
A seed: block creates reference data. Seeds run once, in the first transaction of a new database (typerule db init), and before every test.
typerule
seed:
Location(name="Suppliers", kind=Supplier)
Location(name="Customers", kind=Customer)Seeds of Currency and Uom records also tell the type checker which currencies and units exist (see Types).
const
typerule
const STANDARD_VAT: Percent = 22%A constant has an optional type and a value, evaluated where it is used.
Functions
typerule
fn price_for(product: Product, pricelist: Pricelist?, qty: Qty, on: Date) -> Exact<Price>:
if pricelist == none:
return product.list_price
…| Form | Where | Notes |
|---|---|---|
fn name(p: T, q: T = default) -> R: | top level | A function. Parameters may have defaults; the return type is checked against every return. |
fn name(…) -> R: | inside an entity | A method: it sees the record's fields by their bare names and self. Called as record.name(…). |
fn name(p, …) -> R: | inside a block (reaction, test) | A local function; it sees the variables of the block. |
builtin fn name(…) -> R | standard library | Implemented by the engine (next_number). |
typerule
entity PaymentTermLine:
term: PaymentTerm parent
days: Duration default 0 days
end_of_month: Bool default false
fn due_from(start: Date) -> Date:
let d = start + days
return d.end_of_month() if end_of_month else dArguments are positional or named (exchange_rate(USD, EUR, on=today())). A Money parameter without a currency carries its own: the result type can refer to it, and at the call site it becomes the currency of the argument.
typerule
fn split(amount: Money, start: Date) -> [(due: Date, amount: Money<amount.currency>)]:
return lines
.allocate(amount, by=l => l.percent)
.map(a => (due: a.item.due_from(start), amount: a.share))Events
typerule
event SendEmail(to: Email, template: Text, document: Document)An event is a typed message for the outside world. It is sent with emit from an action or a reaction; see Flows and actions.
Views and reports
view and report declarations are parsed and their names are checked, but the web client does not draw them yet; the grammar is in docs/GRAMMAR.md.
typerule
view SalesOrderForm(o: SalesOrder):
header: o.number, o.customer, o.date, o.state
table o.lines as l: l.product, l.qty, l.price, l.subtotal
footer: o.untaxed, o.tax, o.total
actions: o.send, o.confirm