The family: several recipes in one project¶
Experimental
Everything on this page is experimental:
app.recipes, app.currency, apps.<label>.route, features.people,
features.events, features.timeline, features.preferences, the requires, conflicts and version keys of
recipe.toml, karni recipes add, remove and upgrade, karni new
--recipe a --recipe b, the .karni/recipes/ record,
frontend/src/karni/parts.ts and karni_cli.recipes.compose. The error
codes keep their meaning.
A project may hold several recipes: a booking app and a CRM, a shop and a
course catalogue. Each recipe in a project is a part. A part is one Django
app, and its app label is its prefix (bookings, crm). Every part works
alone, and the parts installed together share one database, one sign-in, one
set of roles and one menu.
karni new "Studio" --recipe booking --recipe crm # two parts from the start
karni recipes add crm # a part added to a built project
karni recipes remove crm --allow-destructive # and taken out again
karni recipes upgrade # each part's newer files
What karni recipes add does¶
- It reads the recipe's
karni.yaml(its fragment) and merges it into the project's karni.yaml by the rules below. A conflict stops it before anything is written, with a code fromC442toC447. - It writes the merged karni.yaml, keeps the fragment as it was added in
.karni/recipes/<name>/karni.yaml, and names the part in.karni/build.json. - It runs
karni build, which writes the part's own files by the stub rule, records the sha256 of each in.karni/recipes/<name>/record.json, migrates, seeds only the new tables, runs the tests andkarni check.
--no-build stops after step 2. karni build then does step 3.
karni recipes remove <name> takes the part's apps, roles, pages, menu group
and seed counts out of karni.yaml, and its record out of .karni/. Dropping
its tables is a risky change, so the build that follows needs
--allow-destructive (C270). The part's files the project never changed
(their recorded sha256) are deleted, so its tests do not run against models
that are gone. The files you changed stay on disk, each one listed, for you to
delete. Adding the recipe again writes the deleted ones again.
How a fragment is merged¶
| Key | Rule | On conflict |
|---|---|---|
version |
Must be equal. | C214 |
app.name, app.kind |
The project's stay. A recipe is an app: a kind: site project refuses it. |
C440 |
app.recipes |
The part is appended. A part listed already is refused. | C442 |
app.currency |
One currency for the project: its own, else the first part's. A part's money:XYZ fields are written as money, which reads app.currency, and the payments module's currency follows it. |
none |
modules |
Union. An option the project wrote itself keeps its value. Two parts asking for different values of an option are refused. | C445 |
features |
Union. terms keeps the project's version and unions paths. |
none |
roles |
admin and customer are the family roles: their grants add up and the project's name stays. Every other role is <prefix>-<role>, is new to the project, holds no "*" and is not a default role. With the people contract on, admin gets people.*:merge and people.person:erase, and customer gets people.address: own (their saved addresses on the account page). |
C443 |
apps |
The part's app label must be new. | C442 |
apps.<label>.route |
Where the part's pages live: /<label> unless the recipe names another. It must be the part's own and not a family page's. |
C444 |
pages |
Appended. home belongs to the family: the build writes home: dashboard, and a part that lists it when the project has one is refused, as is a page at another family route. An entity name two apps define is written app.Name. A part's own code pages live under its route: shop.reports: code is the page at /shop/reports. |
C444 |
navigation |
Menu by menu. A list is the app menu (the back office's sidebar). A part's app menu becomes one group, labelled with the recipe's title, and a part installed alone keeps its entries flat. A part's entries of the other named menus (public, the site's header, and account, the signed-in person's links) join the project's in line, each once. A menu may name a family page the project's modules and contracts provide without listing it in pages: orders, cart and checkout with commerce, settings with preferences, people with people, inbox with notify, and home, signin, account and search always. |
none |
dashboard |
The part's home tiles are appended. A tile whose key the project has already (two parts that both show commerce.revenue) is kept once. karni recipes remove takes the part's own tiles out and keeps a contract's. |
none |
seed |
Counts add up per entity. | none |
The family pages are home (/), signin, account, search, inbox,
settings, people, cart, checkout and orders. They come from Karni
and its contracts, never from a part.
The pages, layout and home the build writes¶
A part ships no family page and no layout. For a project of parts, karni
build writes them in frontend/src/karni/ (Karni-owned, rewritten on every
build), each a few lines around a component of @karni/ui or
@karni/commerce-ui:
| Page | Route | When | Component |
|---|---|---|---|
| sign-in | /signin |
always | SignInPage: a code by email, a password, a new account |
| account | /account |
always | AccountPage with the sections the parts register |
| search | /search |
always | SearchPage: every part's results, part by part |
| inbox | /inbox |
features.notify |
InboxPage |
| settings | /settings |
features.preferences |
SettingsPage: details, a tab per part, Emails, Connections |
| people | /people, /people/$id |
features.people |
PeoplePage and PersonPage (a tab per part, the timeline, merge, erase) |
| cart, checkout | /cart, /checkout |
the commerce module | CartPage, CheckoutPage |
| orders | /orders, /orders/new, /orders/$number, its invoice and packing slip |
the commerce module | FamilyOrdersPage, NewOrderPage, FamilyOrderPage, OrderDocumentPage |
Each is registered in routes.tsx's Karni block. A route the project wrote
itself at the same path (outside the block) keeps the project's page.
frontend/src/karni/layout.tsx is the one layout (FamilyLayout): a route
with staticData: { area: "store" } shows in the storefront's frame (the
public and account menus, the mini cart with commerce), the others in
the back office (the app menu, the search box, the inbox bell with notify,
the account menu), and the home and an area: "family" route (/orders) in
the frame of who is looking: the back office for staff, the storefront for
everyone else. frontend/src/karni/home.tsx is the one home (FamilyHome):
each staff role's tiles from every part, the public home a part registers
(the shop's storefront) for visitors and customers, and what to do when no
part is installed.
karni new writes frontend/src/layout.tsx and frontend/src/pages/home.tsx.
While they are still what it wrote, the build turns them into one-line
re-exports of the family's. A project that changed one keeps it, and the
build says so: re-export the family's file to use it, or keep your own.
parts.ts registers the family's own sections first (registerFamily: your
addresses with people, your emails and notifications with notify, and
karni-commerce's orders section, person tab and order links), then each
part's register(), so a part may replace any of them.
The people list is called Customers, and lists the people who bought, in a project whose only part sells (a shop alone), and People otherwise.
recipe.toml: requires, conflicts and version¶
name = "visits"
version = "1.0.0"
requires = ["people>=1", "events>=1", "storage"]
conflicts = ["another-recipe"]
requires names core contracts and modules, each with an optional version
range (>=, >, <=, <, ==, !=, joined by commas). What a part
requires is installed with it:
| Requirement | What the merge does |
|---|---|
people |
features.people: true: karni build writes KARNI_PEOPLE = True, which installs karni.people (people), and admin gets the explicit grants people.*:merge and people.person:erase |
events |
features.events: true: karni build adds karni.outbox to INSTALLED_APPS (family events) |
timeline |
features.timeline: true, with features.people and features.events, which it needs: karni build adds karni.timeline after karni.outbox (timeline) |
preferences |
features.preferences: true: karni build adds karni.preferences to INSTALLED_APPS and writes KARNI_PREFERENCES_DEFAULTS with the project's name, and KARNI_PREFERENCES_LOCKED with its currency (every money field is in it, so Settings does not change it). The part declares its settings section and its connections (preferences) |
notify |
features.notify: true: karni build adds karni.notify to INSTALLED_APPS. The part declares its message kinds in its messages.py, and a role may grant notify.emailtemplate to whoever edits the emails (notifications) |
a module (storage, payments, commerce) |
lists it under modules, so the build adds it, with the modules its manifest requires (commerce brings payments) |
A contract version the installed Karni does not have, or a name that is
neither a contract nor a module karni-cli knows, is refused (C446).
conflicts names recipes and modules a part cannot share a project with,
checked both ways (C447). version is the recipe's own version, recorded
when the part is added and compared by karni recipes upgrade.
Routes and the frontend¶
A part's entity pages are mounted under its route: with route: /bookings
the resources list is /bookings/resources and a booking is
/bookings/bookings/<id>. apps.<label>.route in karni.yaml changes it.
A project without a route keeps its pages at /<entity route>, as before.
The build writes frontend/src/karni/parts.ts, which Karni owns. It imports
each part's frontend/src/<prefix>/index.ts when the part has one, and calls
its register() at startup, in the order of app.recipes. routes.tsx
imports it in the block Karni writes. A part registers its own screens,
sections and routes there. It never edits another part's.
Upgrades¶
karni recipes upgrade [name] compares each part with the recipe karni-cli
has now:
- a file of the part's the project never changed (its sha256 is the recorded one) is replaced, and a new file is added.
- a file the project changed is kept, and the recipe's new version is written
beside it as
<file>.recipe-<version>for you to merge by hand. - in karni.yaml, each of the part's entities and roles the project left as it was added is updated, and each one it changed is reported.
Then run karni build. Shared code (Karni, its contracts, the modules,
@karni/ui) is a versioned dependency and gets its fixes through karni
upgrade, never through a part.
What a part may and may not do¶
A part may depend on Karni, its contracts, @karni/ui and the modules in its
requires. It may subscribe to any declared event by name.
A part must not:
- import another recipe's code, Python or TypeScript.
- point a relation at another recipe's model.
- add a column to a contract's table (it keeps a profile table of its own).
- name a role without its prefix, or hold
"*". - own a family page or route.
- assume it is alone: no test that counts every row of a shared table.
How the rules are kept¶
karni check enforces the rules above on the built project, not only when
karni recipes add merges a part. It reads .karni/recipes/ (the fragment each
part was added with) and karni.yaml, so a project without parts is unaffected.
pytest --pyargs karni.testing.architecture runs the same rules.
| Rule | Code | What fails |
|---|---|---|
part-imports-part |
K315 | a file of one part (its app folder or frontend/src/<prefix>/) imports another part or names its app label: "crm.Deal", "crm.deal:own", "crm:deal-list", "crm/x.html", apps.get_model("crm", ...), a migration dependency, a template include, extends or url, a frontend import |
part-role-prefix |
K315 | with two or more parts, a part after the first has a role that is not <prefix>-<role> and not admin or customer |
part-route |
K315 | a part's base route is a family page's or another app's, or one of its generated pages in routes.tsx is outside its base |
The contracts are not parts, so karni.people, karni.events, karni.timeline,
karni.preferences and karni_commerce are always allowed. The first part keeps
its roles as its recipe wrote them, as the merge does. To join two parts on
purpose, put the reason in an allow marker on the line or the line above:
# karni: allow(part-imports-part): <reason> (// in TypeScript, {# ... #}
in a template).
How the family is tested¶
- Each recipe's own tests run when its project is built alone.
tests/test_family_build.pybuilds one part alone and two together, adds a third and removes it, on every push (thefamilyjob).tests/test_family_rules.pybuilds booking and crm together and breaks each rule in the built project (a bridge file, a role without its prefix, a route that is a family page's), and builds a deliberately bad test part that must failkarni check.tests/test_family_pairs.pybuilds every pair of the family's parts together: its tests andkarni checkmust be green. Thepairsjob in.github/workflows/family-pairs.ymlruns it nightly and on demand, not on every push. The list of parts (PARTS) grows with the family, and a test fails when a recipe with connections is missing from it.tests/test_family_matrix.pyreads every recipe'sconnections.pyand fails when a declared connection has no test with it on, no test with it off, or names another recipe inneeds.
Codes¶
| Code | When |
|---|---|
| C442 | an app label or a recipe the project has already |
| C443 | a role slug: without the prefix, taken, holding "*", or a default role |
| C444 | a route: two apps on one, a family route, or a page listed twice |
| C445 | a module option two parts set differently |
| C446 | a requirement the project cannot meet |
| C447 | a recipe's conflicts |
| C448 | karni recipes remove of a recipe the project does not hold |
| C449 | karni recipes upgrade without the part's record |
| K315 | a part naming another part, a role without its prefix, or a route outside its base |
| K315 | a part's role without its prefix, or its route outside its base |