@usebetterdev/tenant-cli
v0.2.1-beta.1
Published
CLI for multi-tenancy setup: init, migrate (tenants table + RLS), add-table, generate, check, seed.
Readme
@usebetterdev/tenant-cli
CLI for multi-tenancy setup: init, migrate (tenants table + RLS), add-table, generate, check, seed.
Install
pnpm add -D @usebetterdev/tenant-cliConfig
Loads config from better-tenant.config.json in the project root (or from the "betterTenant" key in package.json).
{
"tenantTables": ["projects", "tasks"]
}tenantTables lists your existing tables that should be tenant-scoped. The CLI adds a tenant_id column, RLS policies, and triggers to each one.
Commands
init — create config interactively
Connects to your database, detects tables, and creates better-tenant.config.json.
npx @usebetterdev/tenant-cli init --database-url $DATABASE_URL
npx @usebetterdev/tenant-cli init # prompts for DATABASE_URLmigrate — initial setup
Generates a single SQL migration that creates the tenants table and adds RLS policies, triggers, and tenant_id columns for all tables listed in tenantTables. Run this once when setting up multi-tenancy.
npx @usebetterdev/tenant-cli migrate --dry-run # preview SQL
npx @usebetterdev/tenant-cli migrate -o ./migrations # write to fileadd-table — add a table later
Generates SQL for a single table that wasn't in your original migration. Use this when you create a new table and want to make it tenant-scoped.
npx @usebetterdev/tenant-cli add-table comments --dry-run
npx @usebetterdev/tenant-cli add-table comments -o ./migrationsAfter running add-table, remember to add the table name to tenantTables in your config.
generate — Drizzle schema snippet
npx @usebetterdev/tenant-cli generate --dry-run
npx @usebetterdev/tenant-cli generate -o schema/better-tenant.tscheck — verify database setup
npx @usebetterdev/tenant-cli check --database-url $DATABASE_URLseed — insert a tenant
npx @usebetterdev/tenant-cli seed --name "Acme Corp" --database-url $DATABASE_URLTypical workflow
- Run
initto createbetter-tenant.config.json - Run
migrate -o ./migrationsto generate the initial migration - Apply it:
psql $DATABASE_URL -f ./migrations/*_better_tenant.sql - Run
checkto verify everything is set up correctly - Later, when you add a new table: run
add-table <name> -o ./migrationsand apply it
Programmatic API
Subpath exports for use in scripts or custom tooling:
import { generateMigrationSql } from "@usebetterdev/tenant-cli/migrate";
import { runCheck } from "@usebetterdev/tenant-cli/check";
import { runSeed } from "@usebetterdev/tenant-cli/seed";