tenant-kit schema

Synced from tenant-kit/sql/README.md — the repo is canonical.

Numbered files, applied in order. Idempotent — every object is created IF NOT EXISTS or OR REPLACE, so re-applying is safe.

File Owns
001_core.sql The tenancy schema: tenants, memberships.
002_rls.sql tenancy.current_tenant() and tenancy.protect() — the row-level-security machinery for the shared-schema strategy.

Apply with anything that runs SQL files:

psql "$DATABASE_URL" -f node_modules/tenant-kit/sql/001_core.sql
psql "$DATABASE_URL" -f node_modules/tenant-kit/sql/002_rls.sql

Then protect your own tables (once, in your own migrations):

SELECT tenancy.protect('public.projects');            -- column defaults to tenant_id
SELECT tenancy.protect('public.documents', 'org_id'); -- or name it

The files ship in the npm tarball and are importable as tenant-kit/sql/001_core.sql for migration tools that resolve module paths.

Two deliberate choices, documented in the files themselves:

  • tenancy.tenants and tenancy.memberships are not row-level-secured — they are the directory the resolve path reads before any tenant scope exists.
  • tenant_id is text, matching billing-kit's opaque TenantId, so one id flows through both schemas without casts.