Skip to content

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 ​

StepYou learnThe shop gets
1. Your first entityentities, fields, typerule check, a first testcustomers and products
2. Money, quantities and unitsMoney, Price, Qty, Percent, dates; what the compiler refusesprices per kg and per piece
3. Relationsreferences, optionals ?. ??, lists, via, parent, through, modulescategories, suppliers, orders with lines
4. Formulasformulas, lambdas, sum/map/group_by/flat_map, rollups, when:totals, VAT, shipping, customer tiers
5. Rulescheck, warn, where, required when, uniquedata that is always valid
6. Flows and actionsflow, action, require, frozen when … exceptorders that are confirmed, shipped, cancelled
7. Invoices: frozen and livedocuments that follow documents, live, multi-hop rollupsinvoices and payments
8. Testsfixtures, clock, expect_error, expect_compile_error, patcha real test suite
9. Permissionsroles, row and field rules, hide, deny, as user:clerks, warehouse, accountants
10. Ledgersledger, post to, automatic reversalsstock and receivables
11. Database and APItyperule db, typerule serve, tokens, the HTTP API, the web clienta running app
12. Migrations and changesmigration (rename, convert, backfill, verify), provisional values, changea model that evolves with real data
13. Talking to other systems[vars], connections, inbound, outbound, publish, records from json, receive / expect sent / deliverorders from a web shop, parcels, emails
14. Files, formats and databasesSFTP and FTP, from csv and from fixed, atomic, write, insert, on-premise agentsthe till, the bank, the accountant
15. Documents, labels and signatures.trp files, document, label, render, zpl, attach, signersinvoices in PDF, parcel labels, signed files
16. Retention, erasure and a signed historyretain, expire, personal, erase, signed commits, archivesGDPR requests, invoices kept ten years
17. Pages, history and the trashhistory last 10, history none, deletable, the trash, [data]a site edited often, baskets that go
Next stepswhere to go from here

How each step works ​

Every step has the same shape:

  1. The goal — what the shop needs next.
  2. The concept, explained with the smallest piece of code that shows it.
  3. The code of the step. Snippets are taken from the step's project; highlighted lines are what changed.
  4. Run it: the commands, and the output you should see.
  5. Try it: a small exercise to do on your own.
  6. Recap.

At the top of each step there is a button:

Open in Sandbox runs in your browser

It 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 text blocks is the real output of the commands on the step's project.
  • # comments in the code explain what a line does; you don't have to type them.

Ready? Start with your first entity →

TypeRule: the books balance.