PermDock
Concepts

Apps and profiles

definePermDock binds the data sources, caches, opt-in features and services an app wires once (memberships, custom roles, relations, hosted policies, approval policies, limits, sink, approval store, version and subject caches) to a policy, so createPermDock and every adapter read them from one place; profile dev fills in in-process defaults and profile prod refuses to start when a grant needs a service the app lacks.

definePermDock(policy, options) from permdock/app returns the policy with the app's sources, caches, features and services bound to it. Pass the result wherever a policy goes: core getPermDock, every adapter and the CLI. Each one reads the app definition, and its own options win key by key. permdock/app is server-only.

// src/permdock.ts
import {
  approvalPolicies,
  hostedPolicies,
  limits,
  memoryLimitStore,
} from "permdock";
import { definePermDock } from "permdock/app";
import { memoryApprovalStore } from "permdock/approvals";
import { policy } from "./policy";

export const app = definePermDock(policy, {
  memberships, // MembershipSource, or several
  customRoles, // CustomRoleSource
  relations, // RelationSource
  approvalPolicies: approvalPolicies(approvalPolicySource),
  policies: hostedPolicies(policySource),
  limits: limits(memoryLimitStore()),
  sink,
  versions, // VersionCache shared by every adapter
  instances, // InstanceCache shared by every adapter
  subjects, // SubjectCache for postgrestSources
  store: memoryApprovalStore(),
  profile: process.env.NODE_ENV === "production" ? "prod" : "dev",
});
// src/server.ts
import { createPermDock } from "permdock/hono";
import { app } from "./permdock";

export const { protect, middleware } = createPermDock(app, { subject });

What an app holds

KeyWhatWithout it
memberships, customRoles, entitlements, relations, principals, settingsThe extension interfaces that read the application's own dataMemberships come from the principal; no custom roles, entitlements or relations
policieshostedPolicies(source): hosted grants on hostable permissionsThe code policy only
approvalPoliciesapprovalPolicies(source), or a memoryApprovalPolicySource([...]) as isApproval requirements from code only
limitslimits(store): counts limit grants in a LimitStoreA grant with limit denies with limit-unavailable
sinkA DecisionSinkNo decision log
storeThe ApprovalStore every adapter opens and resumes approvals inapproval-required cannot be resumed
versions, instances, subjectsThe VersionCache, InstanceCache and SubjectCache that every adapter and permdockFor shareEach adapter reads its own
claimsA ClaimsMap for fromClaims; adapters do not read itfromClaims reads the JWT defaults
credentialssubjectFromApiKey options without permissions, for fromApiKeyfromApiKey throws a TypeError
sources(client) => { memberships?, customRoles?, subjects? }, for forClient: the sources of one request-scoped or lazily created clientforClient throws a TypeError
profile"prod" (default) or "dev""prod"

hostedPolicies, approvalPolicies and limits are features, not bare sources. The code that merges hosted grants, tightens approvals and counts quotas ships only with an app that imports them, so a plain RBAC app does not carry it.

Members

The result is a policy with five more members. They are not enumerable, so the policy still serialises as a policy.

MemberWhat
getPermDock(user, query?)The instance for user, built with the app's sources. query is { tenant?, intent? }.
fromClaims(claims, query?)The instance for claims a provider already verified, mapped through claims. Claims with no id are anonymous.
fromApiKey(key, query?)The instance for an opaque API key through credentials. Every failure is anonymous.
sources(versions?)The instance options for one version cache, the same object on every call, with memberships wrapped in claimsFirst. The core of permdockFor.sources.
evict(principal)Drops the principal from instances, versions and subjects.
forClient(client)The definition bound to client: the same policy, caches and services, with memberships, customRoles and subjects from sources(client), run once per client and cached in a WeakMap.

There is no .next or .authorizer member. Each would make permdock/app load permdock/next or permdock/better-supabase, both optional peers, in every app that imports it. Call createPermDock(app) from permdock/next and authorizer({ policy: app }) from permdock/better-supabase instead. permdockFor, authorizer, authorizationHooks, supportPolicy, subscriberPolicy and postgrestSources read memberships, instances, versions, subjects and the other sources from the definition, with their own options winning.

Profiles

  • dev fills in in-process defaults: one memory ApprovalStore that every adapter of the app shares, so a request opened through one adapter resumes through another, and limits(memoryLimitStore()) when a grant has a limit. The defaults come from definePermDock, never from an adapter, so an adapter bundle carries no store. A new app runs before any infrastructure exists. Memory stores are per process: their state is lost on restart and is not shared between instances.
  • prod fills in nothing. definePermDock throws at startup when a grant requires approval and there is no store, or a grant has a limit and there are no limits. The alternative is an app that starts and then denies every such call at run time.

No adapter falls back to a memory store on its own. An approval store exists because the app or the adapter call named one, or because the profile is dev.

Why

A Supabase admin client is created per request or lazily, so memberships cannot be a module-level option of definePermDock. sources(client) and forClient(client) keep the app definition at module scope and bind the client when it exists. The WeakMap ties a client's sources to the client, so a request-scoped client frees them with it, and a module-level cache would not.

Before definePermDock, each adapter took its own copy of the same sources and services, and the CLI and the runtime named the same tables twice. A team that added a second surface (an MCP server next to a Next.js app) wired everything again and could miss the sink or the store on one of them. One app definition removes that duplication without a new interface: the result is still a Policy, so no adapter signature changes and an adapter option still overrides the app.

The profile makes the trade-off between a quick start and a safe production setup explicit. A memory store is right for the first hour and wrong in production. The dev profile gives it, prod refuses to start without the real one, and the choice is one line the reader can see.

defineApp is removed. Decision 0056 in docs/decisions records why the entry is permdock/app and the name is definePermDock. permdock codemod 0.5 lists each defineApp import.

Last updated on

On this page