Skip to the page

Feature by feature

Chapter 7 of 11

Organizations and billing

Required schema links model workspaces, roles and ownership hierarchies without repeating tenant IDs on every table. Single-statement transactions keep invites and credit ledgers exact while the compiler infers shard roots automatically.

Up to this point, each Note belonged to a single user identity. Multi-user software groups records into workspaces or organizations, assigns member roles and tracks usage balances without leaking rows across tenants or double-spending credits under concurrent traffic.

In practice

Required object links enforce tenant boundaries without per-query filters. When Ada creates an organization and invites Ben, Ben sees only the pending invite until he accepts, and myOrganizations then returns the organization alongside his role.

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 determine both visibility and role with no manual tenant filter in the query.

Hierarchies and role links#

Pillar at work: One typed graph

Instead of repeating a tenant_id column across every table, child types link to their natural parent in the graph, and access policies walk that chain. Wrapping our notes schema in workspaces links Note to Workspace and Workspace to Organization.

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 policies from earlier chapters   access policy org_members_can_read    allow select    using ((global caller in .workspace.org.members.identity) ?? false);}
Required links connect Note through Workspace to Organization while tables keep standard UUID keys.

A Membership type assigns each member a role such as Owner, Admin or Member. Because the policy on Note walks .workspace.org.members.identity, every read and write stays isolated to the caller’s organizations automatically.

Invites and SSO#

Onboarding teammates combines access policies with the transactional jobs from Actions, jobs and webhooks. One EdgeQL statement inserts the Invite row and queues the email job 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 }
If the invite insert rolls back, no invitation email is queued.

Policies let the invited user inspect and accept the invite at the offered role, or enterprise customers can configure OIDC domain sign-in so verified employees join automatically on first login.

Atomic credit ledgers#

Pillar at work: Commit-atomic

Reading a wallet balance in application code before deducting usage creates a race condition under parallel requests. Composing an insert and an update inside one EdgeQL statement records the immutable LedgerEntry and decrements CreditWallet.balance_credits atomically.

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 row and wallet debit commit together in a single statement.

A schema constraint prevents balance_credits from dropping below zero, and an exclusive constraint on idempotency_key rejects duplicate charges when clients retry after a network timeout.

Atomic credit ledger

Each debit writes a ledger row and updates the balance in one statement.

Placement roots from links#

Pillar at work: Distributed from links

Those same required single links also tell Napalm how to shard data across primaries when a database grows (Placement roots and sharding). The compiler walks required stored links upward to find your schema’s ownership roots. Any top-level project type with no outbound required link that owns child types becomes a placement root, whether that type is Account in a consumer app, Project in a developer tool, Site in fleet software or Organization in B2B software. Every child type reaching that root unambiguously is rooted on the same shard.

Inferred placement roots

Required single links group each root record and its descendants on one shard.

When your domain needs explicit control, ext::napalm::root annotations mark top-level 'root' types, replicate shared 'reference' lookup tables (such as BillingPlan) to every shard in one commit, or pick the 'owner' link when a record points to more than one parent. Links to ext::auth::Identity, multi links and links marked 'none' never claim ownership, so users belong to multiple organizations and guest collaborators span roots freely.

Running napalm roots on our workspace notes schema shows that general rule on concrete types: because Membership, Workspace, Note and CreditWallet all chain up to Organization through required links, all five types are ready for sharding with standard uuid keys and unchanged 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 roots inspects required link chains and reports each type’s root classification.

Those same structured records and access policies can power semantic search, training datasets and live model features without a separate ML data stack.

Type a word or two to search every docs page.