Appearance
Step 1: Your first entity
Open in Sandbox runs in your browserGoal: describe the two things every shop has — customers and products — and check that the description is right.
The project
A TypeRule project is a folder with a typerule.toml:
toml
[project]
name = "shop"Entities and fields
An entity is a kind of thing your business keeps track of. Each one becomes a table in the database, a resource in the API and a sheet in the web client. Create shop.tr:
typerule
# Our shop: the people we sell to and the things we sell.
entity Customer:
name: Text unique
email: Email?
city: Text?
entity Product:
code: Code unique
name: Text
description: Text?
active: Bool default trueReading it line by line:
name: Text— a field and its type. Fields are required unless you say otherwise.Text?— the?makes the field optional: it may benone.unique— no two customers can have the same name.Email,Code— text types with a meaning: an email address, a short identifier.default true— the value a new product gets when you don't give one.
Blocks are made by indentation, as in Python: four spaces, never tabs.
What you don't write
Every entity also has an id, created_at, created_by, updated_at, updated_by and a version, and TypeRule keeps the full history of every field, with the cause of each change. You never declare them.
Check it
bash
typerule checktext
✓ 4 modules checked, 0 warningstyperule check reads every module of the project — yours plus the standard library, which gives you currencies, units of measure, companies and users — and checks names, types and much more. It's what your editor runs while you type.
A first test
The fastest way to see a model work is a test. Tests live in tests/; create tests/shop.test.tr:
typerule
test "a new customer":
let anna = Customer(name="Anna Rossi", email="[email protected]")
expect anna.name == "Anna Rossi"
expect anna.city == none
test "new products are active":
let mug = Product(code="MUG-01", name="Espresso mug")
expect mug.active
expect Product.count() == 1Customer(name=…, email=…)creates a record. Fields you leave out get their default, ornoneif they are optional.letgives a name to a value inside the test.expectfails the test if the condition is false.Product.count()counts the records of an entity.
bash
typerule testtext
✓ a new customer
✓ new products are active
2 passed, 0 failedEvery test starts from an empty world, and every statement in a test is one transaction, like a click on Save.
When something is wrong
Misspell a field in the test — expect anna.nmae == "Anna Rossi" — and run typerule check:
text
error: `Customer` has no field `nmae`
┌─ tests/shop.test.tr:3:12
│
3 │ expect anna.nmae == "Anna Rossi"
│ ^^^^^^^^^
✗ 1 errors, 0 warnings in 5 modulesAnd when an expectation is false, typerule test shows both sides. With expect mug.active == false:
text
✓ a new customer
✗ new products are active (tests/shop.test.tr:6)
tests/shop.test.tr:8: expectation failed: mug.active == false
left: true
right: false
1 passed, 1 failedRules declared in the model are enforced too. Creating two customers with the same name, or one without a name, fails with:
text
Customer.name must be unique: Anna already exists
Customer.name is requiredTry it
- Add a
phone: Phone?field toCustomerand set it in the first test. - Write a test that creates two products and expects
Product.count() == 2. - Remove
uniquefromCustomer.name: which test would you write to show the difference?
Recap
- A project is a folder with
typerule.tomland.trmodules. entitydeclares a kind of record; fields are required unless they end with?.uniqueanddefaultare part of the field declaration.typerule checkchecks the model;typerule testruns thetestblocks intests/.