Appearance
Tutorial: build a small shop
In this tutorial you build the back office of a small shop that sells coffee beans, tea and mugs. You start with a single entity and finish with an application you could actually run: customers, a catalog priced in euros per kilogram or per piece, orders with a life cycle, invoices and payments, stock, permissions for each kind of employee, a database with an API, the tools to change all of it safely once it holds real data, and the connections to the systems around it (a web shop, a carrier, a till, a bank and the accountant's database), and the documents it sends: invoices, labels, signed files.
Each step adds one idea to the same project. Every step is a complete, working project: its code compiles with zero errors and all of its tests pass.
What you'll build
| Step | You learn | The shop gets |
|---|---|---|
| 1. Your first entity | entities, fields, typerule check, a first test | customers and products |
| 2. Money, quantities and units | Money, Price, Qty, Percent, dates; what the compiler refuses | prices per kg and per piece |
| 3. Relations | references, optionals ?. ??, lists, via, parent, through, modules | categories, suppliers, orders with lines |
| 4. Formulas | formulas, lambdas, sum/map/group_by/flat_map, rollups, when: | totals, VAT, shipping, customer tiers |
| 5. Rules | check, warn, where, required when, unique | data that is always valid |
| 6. Flows and actions | flow, action, require, frozen when … except | orders that are confirmed, shipped, cancelled |
| 7. Invoices: frozen and live | documents that follow documents, live, multi-hop rollups | invoices and payments |
| 8. Tests | fixtures, clock, expect_error, expect_compile_error, patch | a real test suite |
| 9. Permissions | roles, row and field rules, hide, deny, as user: | clerks, warehouse, accountants |
| 10. Ledgers | ledger, post to, automatic reversals | stock and receivables |
| 11. Database and API | typerule db, typerule serve, tokens, the HTTP API, the web client | a running app |
| 12. Migrations and changes | migration (rename, convert, backfill, verify), provisional values, change | a model that evolves with real data |
| 13. Talking to other systems | [vars], connections, inbound, outbound, publish, records from json, receive / expect sent / deliver | orders from a web shop, parcels, emails |
| 14. Files, formats and databases | SFTP and FTP, from csv and from fixed, atomic, write, insert, on-premise agents | the till, the bank, the accountant |
| 15. Documents, labels and signatures | .trp files, document, label, render, zpl, attach, signers | invoices in PDF, parcel labels, signed files |
| 16. Retention, erasure and a signed history | retain, expire, personal, erase, signed commits, archives | GDPR requests, invoices kept ten years |
| 17. Pages, history and the trash | history last 10, history none, deletable, the trash, [data] | a site edited often, baskets that go |
| Next steps | where to go from here |
How each step works
Every step has the same shape:
- The goal — what the shop needs next.
- The concept, explained with the smallest piece of code that shows it.
- The code of the step. Snippets are taken from the step's project; highlighted lines are what changed.
- Run it: the commands, and the output you should see.
- Try it: a small exercise to do on your own.
- Recap.
At the top of each step there is a button:
Open in Sandbox runs in your browserIt opens the complete project as of the end of that step in the Sandbox, which runs the compiler and the tests in your browser. You can follow the whole tutorial there, with nothing installed.
If you prefer your own machine (Installation), create a folder and keep it for the whole tutorial: at each step, add or update the files shown, then run typerule check and typerule test in that folder. From step 11 on you will also need a database in that same folder, because step 12 migrates the data you created in step 11.
Conventions
- Commands run in the project folder (the one with
typerule.toml). - Output shown in
textblocks is the real output of the commands on the step's project. # commentsin the code explain what a line does; you don't have to type them.