Reusable Cloudflare-only multi-tenant SaaS starter, built as an Orun component-native desired-state repo. Identity, organizations, projects, RBAC, audit, metering, billing, webhooks, and notifications ship as separate bounded-context Cloudflare Workers behind a single public edge API, with a Next.js console on Workers + Static Assets — and Cloudflare D1 as the database, so Cloudflare is the only provider in the loop.
Not yet recorded — run flows/phases/08-docs
after phase 06 to fill this section from verified live state
(manifest · operating contract).
- Runtime is live, per environment, through Orun. The edge API, the
bounded-context Workers, and the console deploy to
stageandprodviaorun run(no direct Wrangler/Terraform/pnpm in CI). - Data plane is provisioned by Terraform: a Cloudflare D1 database per
environment and the
api-edgeidempotency KV namespace. Terraform state lives in the Orun Cloud HTTP state backend; provider credentials are BROKERED per-run from the workspace's Cloudflare connection (no AWS, no long-lived secrets at rest). - Database migrations run through the
db-migratecomponent against D1's REST API (plan on PRs, apply on merge tomain). - Billing is live end-to-end via the Polar adapter (embedded checkout, plan changes, multi-org fan-out).
- Known credential-blocked tails: full production OAuth/magic-link auth
and Stripe require human-supplied credentials. The notifications email
provider is Cloudflare Email Service (
cloudflare-email, no API key — thesend_emailbinding is the credential); it needs one-time account setup: Workers Paid plan and the sending domain verified in Email Service (DKIM/SPF). - The
devenvironment is verify-only (no provisioned database by design).
One provider means one consent, one token, and one place to look when something breaks. The cost is D1's shape, and this baseline is explicit about it rather than quiet:
- No interactive transactions. D1 offers atomic
batch()for a statement list decided up front, not a transaction held open across reads and writes.executor.transaction(...)therefore runs its statements in order with no rollback; every call site is a place a reviewer should look first. Prefer a single statement withRETURNING, or an upsert withON CONFLICT, where all-or-nothing actually matters. - SQLite types. Timestamps are ISO-8601 text, JSON documents are text,
booleans are 0/1.
@saas/db/jsonowns the decoding so row mappers never guess. - No schemas. The bounded-context boundary is a table-name prefix
(
identity_users,membership_organizations) enforced by the repositories — which is where it was actually enforced under Postgres too.
- Node.js >= 20 (CI and components run on Node 22)
- pnpm >= 10 (
npm install -g pnpm) - (Optional, for local Orun validation) the
kioxCLI on yourPATH.kioxpins the Orun provider declared inkiox.yaml; invoke Orun askiox -- orun ....
# Install all workspace dependencies
pnpm install
# Type-check / lint / test / build across the workspace (Turborepo)
pnpm typecheck
pnpm lint
pnpm test
pnpm buildapps/api-edge Public HTTP entry point (Cloudflare Worker)
apps/identity-worker Users, sessions, API keys, OAuth
apps/membership-worker Organizations, members, invitations, role assignments
apps/projects-worker Projects and environments
apps/policy-worker Deny-by-default RBAC evaluation
apps/events-worker Domain events, audit log, observability
apps/config-worker Settings, feature flags, secret metadata
apps/metering-worker Usage ingestion, quotas, rollups
apps/billing-worker Plans, subscriptions, invoices (Polar adapter)
apps/notifications-worker Email delivery and preferences
apps/webhooks-worker Outgoing webhooks: signing, delivery, replay
apps/admin-worker Audited admin/support workflows
apps/web-console-next Next.js console (Cloudflare Workers + Static Assets)
packages/contracts Shared API, tenancy, event, and error types + validators
packages/policy-engine RBAC evaluation logic
packages/db D1 executor, migration harness, manifest, and runner
packages/sdk TypeScript SDK (contract-driven)
packages/cli `asdasd` CLI
packages/notifications-client Notifications client
packages/shared Generic helpers (IDs, errors) — no domain logic
packages/testing Test fixtures and utilities
infra/terraform/cloudflare-d1 D1 database provisioning (stage/prod)
infra/terraform/cloudflare-kv api-edge idempotency KV namespace
infra/terraform/cloudflare-domain Zone adoption + console custom domain
infra/db-migrate Database migration runner component
tooling/tsconfig Shared TypeScript configurations
tooling/eslint Shared ESLint configuration
tooling/wire Deploy-time wrangler config renderer
tests/* Per-component contract and verifier test suites
Execution contracts (the composition stack) are not vendored here. They are
consumed from the published catalog at
oci://ghcr.io/sourceplane/stack-tectonic, pinned to an explicit version in
intent.yaml. Composition changes are made in
sourceplane/stack-tectonic,
released there, and adopted here by bumping the pinned tag.
CI is powered by Orun with the local Stack Tectonic
composition stack. .github/workflows/ci.yml calls only orun plan and
orun run — no direct pnpm, turbo, Wrangler, or Terraform commands run in
GitHub Actions. The Orun runtime is pinned in kiox.yaml (resolved digest in
kiox.lock); the workflow's orun-action version: matches that pin.
kiox -- orun compositions lock --intent intent.yaml
kiox -- orun validate --intent intent.yaml
kiox -- orun plan --changed --intent intent.yaml --output plan.json
kiox -- orun run --plan plan.json --dry-run --runner github-actionsUse --changed for PR-scoped checks; use a full plan when validating
environment promotion or cross-component dependencies (--view dag).
Terraform provisions the D1 database and the api-edge KV namespace for
stage and prod. Both roots carry adopt.tf, which imports an existing
resource at plan time instead of colliding with it — so a re-bootstrap over a
half-torn-down attempt heals rather than fails. Credentials are brokered per
run from the workspace's Cloudflare connection and Terraform state lives on
the platform; nothing provider-shaped is stored in this repo or in GitHub.
See specs/core/access-and-infra.md for the access model.
- Create the directory under
apps/,packages/,tests/, orinfra/. - Add a
component.yamlwith the appropriatespec.type— one ofcloudflare-worker-turbo,cloudflare-workers-assets-turbo,terraform,db-migrate, orturbo-package— plussubscribe.environmentsand the typedparametersthe composition schema requires. - Orun discovers it automatically on the next plan (
discovery.rootscoversapps/,infra/,packages/,tests/). Validate withkiox -- orun validate --intent intent.yaml.
See specs/core/orun-golden-path.md for the intent/component/composition layer
rules before changing CI, infra, or intent.yaml.