Skip to the page

Feature by feature

Chapter 7 of 11

Organizations and billing

Required links in the schema isolate every customer organization without manual filters. Single-statement updates keep invites, roles and credit balances exact.

Most B2B applications add an organization column to every table and filter every query by hand. Usage billing adds a second risk when a separate billing service allows two concurrent requests to spend the final credit twice.

In practice

In Napalm, required links between types isolate every company without manual filters. Ada creates an organization and invites Ben. Ben sees only the pending invite until he accepts it, and then he reads the organization and his role through myOrganizations.

dbschema/queries/myOrganizations.edgeql
select Organization {  id,  name,  slug,  my_role := assert_single((select .members filter .identity ?= global caller)).role,}order by .name
Membership links decide both visibility and role without any tenant filter in the query.

Organizations and roles#

Pillar at work: One typed graph

Instead of copying organization ids across every table, you wrap Note inside Workspace and Organization using required links in the same typed graph.

dbschema/default.gel
type Organization {  required name: str;  required slug: str { constraint exclusive; };  multi members := .<org[is Membership];} type Workspace {  required org: Organization { on target delete delete source; };  required name: str;} type Note {  required workspace: Workspace { on target delete delete source; };  required owner: ext::auth::Identity;  multi editors: ext::auth::Identity;  required title: str;  required body: str;  # …owner and editor rules from earlier chapters   access policy org_members_can_read    allow select    using ((global caller in .workspace.org.members.identity) ?? false);}
Required links connect every Note through Workspace up to Organization.

Compiler-inferred Organization root

Required links chain Note -> Workspace -> Organization so Napalm infers Organization as the shard root without tenant_id columns.

Membership assigns each user Owner, Admin or Member on an organization. Because Note walks .workspace.org.members, your queries need no tenant filters and one company never sees another company’s notes.

Invites#

Adding teammates builds directly on the transactional jobs from Actions, jobs and webhooks. One statement creates the invite record and queues the invitation email together.

dbschema/queries/inviteMember.edgeql
with  invite := (    insert Invite {      org := (select Organization filter .id = <uuid>$org),      email := str_lower(<str>$email),      role := <Role>$role,    }  ),  email := (    insert ext::napalm::Job {      query := 'sendInviteEmail',      variables := <json>(invite := <str>invite.id),    }  ),select invite { id, email, role, expires }
When the invite fails, no invitation email goes out.

Access rules let the invited person see the invite and join at the offered role, or companies can enable domain sign-in so teammates join without an invite.

Credits#

Pillar at work: Commit-atomic

Checking a wallet balance in application code before subtracting credits creates a read-modify-write race where two parallel requests spend the final credit twice. Metered features charge the organization’s CreditWallet by inserting a ledger entry and subtracting credits in one statement.

dbschema/queries/spendCredits.edgeql
with  amount := <int64>$amount,  wallet := assert_single((select CreditWallet filter .org.id = <uuid>$org)),  entry := (    insert LedgerEntry {      wallet := wallet,      delta_credits := -amount,      reason := <str>$reason,      idempotency_key := <str>$key,    }  ),  debited := (    update wallet    set { balance_credits := .balance_credits - amount }  ),select {  entry_id := entry.id,  remaining_credits := debited.balance_credits,}
The ledger entry and the wallet debit save together on the organization’s wallet.

Single-statement seat & credit ledger

Ledger inserts and CreditWallet debits commit in one EdgeQL statement with an exclusive idempotency_key and non-negative balance constraint.

A schema constraint stops the balance from dropping below zero, and an exclusive idempotency key blocks duplicate charges on network retries.

Roots#

Pillar at work: Distributed from links

Organization has no outbound required links, while Membership, Workspace, Note and CreditWallet all chain up to Organization through required links. Napalm infers Organization as the shard root for every tenant record without adding shard keys to your queries.

Your terminal
napalm roots notes
Roots: 1 root, 4 rooted.
Placeable: Organization.
Type Kind Root By Placeable
──────────── ────── ──────────── ────────────── ─────────
Organization root yes
Membership rooted Organization .org yes
Workspace rooted Organization .org yes
Note rooted Organization .workspace.org yes
CreditWallet rooted Organization .org yes
Napalm walks required links in dbschema/default.gel to prove every tenant type co-locates under Organization.

Customer data stays isolated, credit balances stay exact and your schema is ready to spread organizations across shards in Scale and the chaos lab.

Customer workspaces are isolated and billed cleanly. Those same workspace records can power search, model training and live feature serving.

Type a word or two to search every docs page.