What it does
permly provides two CLI commands to set up permissions in your app:
npx permly init— Creates a schema file in./migrationsand a starterpermly.js(or.ts) insrc/or rootnpx permly migrate— Creates database tables or collections to store permissions, roles, and assignments
When to use it
Run both in order when setting up permly:
npx permly init— First, to create the configuration filenpx permly migrate— Then, to create the database schema
The init command is run once per project. The migrate command is idempotent and safe to run multiple times (useful in CI/CD).
Init Command
npx permly init creates three things:
- Schema file in
./migrations/— SQL for MySQL/Postgres, or collection setup for MongoDB - Starter file (
permly.js,permly.ts,.mjs, or.cjs) — Your database adapter and permissions config - Prompts you for:
- Database: mysql, postgres, or mongodb
- Table prefix (default:
perm_) - PostgreSQL schema (if using Postgres; default:
public) - Language and module format (auto-detected from your project)
Init example
$ npx permly init
permly init
Using: postgres · schema public · table prefix perm_ · language TypeScript · ESM
Created ./migrations/permly.sql
Created ./src/permly.ts
Init options
# Interactive (asks what you need)
npx permly init
# Non-interactive (pass --db or it fails without a TTY)
npx permly init --db postgres --prefix auth_
# Force overwrite existing files
npx permly init --force
# Choose language and module format explicitly
npx permly init --ts --esm
npx permly init --js --cjs
Supported databases: mysql, postgres, mongodb
What gets created
Starter file (./src/permly.ts or root):
import { createPermissions } from "permly";
import { memoryAdapter } from "permly/memory";
export const perms = createPermissions({
adapter: memoryAdapter(),
permissions: ["posts.create", "posts.edit", "posts.delete"],
roles: ["editor", "viewer"],
});
export async function setupPermissions() {
const { createdRoles } = await perms.sync();
if (createdRoles.includes("editor")) {
await perms.role("editor").givePermission("posts.create", "posts.edit");
}
}
await setupPermissions(); // ✓
Edit the permissions and roles arrays to match your app. Import and call setupPermissions() once on app startup.
Schema file (./migrations/permly.sql) — Contains CREATE TABLE statements. Use with your favorite migration tool, or pass to the CLI:
psql -d mydb -f ./migrations/permly.sql
Or let the CLI handle it with npx permly migrate.
Migrate Command
npx permly migrate creates database tables or collections from your schema file.
Migrate example
# Use DATABASE_URL env var
DATABASE_URL=postgres://user:pass@localhost/mydb npx permly migrate
# Or use --url flag
npx permly migrate --url "mysql://user:pass@localhost/mydb"
# Non-interactive (good for CI/CD)
npx permly migrate --yes
Migrate options
npx permly migrate [--url <url>] [--yes]
--url <url> Database URL (default: $DATABASE_URL env var)
--yes Skip confirmation prompt (required in CI/CD)
Connection strings
# MySQL / MariaDB
DATABASE_URL="mysql://user:password@host:port/database"
# PostgreSQL
DATABASE_URL="postgres://user:password@host:port/database"
# PostgreSQL with SSL (Supabase, AWS RDS, Neon)
DATABASE_URL="postgres://user:password@host:port/database?sslmode=require"
# MongoDB
DATABASE_URL="mongodb://host:port/database"
# MongoDB Atlas
DATABASE_URL="mongodb+srv://user:password@cluster.mongodb.net/database"
What gets created
MySQL/PostgreSQL: Five tables
perm_permissions— permission namesperm_roles— role namesperm_user_permissions— user-to-permission assignmentsperm_user_roles— user-to-role assignmentsperm_role_permissions— role-to-permission assignments
MongoDB: Six collections
perm_permissions— permission documentsperm_roles— role documentsperm_user_permissions— user-to-permission assignmentsperm_user_roles— user-to-role assignmentsperm_role_permissions— role-to-permission assignmentsperm_locks— advisory locks for concurrent safety
Each has indexes for fast queries.
Schema customization
# PostgreSQL: put tables in a specific schema
# (create the schema first: CREATE SCHEMA auth)
npx permly migrate --schema auth
# All databases: use a custom table prefix
npx permly migrate --prefix auth_
Tables will be named auth_permissions, auth_roles, etc.
CI/CD Setup
Always pass --yes in automated environments:
# GitHub Actions
- name: Create database schema
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
run: npx permly migrate --yes
# The CLI never prompts without a TTY
# init also requires --db in CI since it can't ask
npx permly init --db postgres --force
The migrate command is idempotent: running it multiple times safely skips existing tables.
Custom migration handling
If you prefer to manage the schema yourself, you have two options:
-
Run the SQL file manually:
psql -d mydb -f ./migrations/permly.sql -
Import schema exports from permly:
import { mysqlSchema } from "permly/mysql"; import { postgresSchema } from "permly/postgres"; import { mongodbSetup } from "permly/mongodb"; // Use these in your migration tool
Common mistakes
Forgetting to run migrate — The tables won't exist until you run the command. Your first permission check will fail.
Wrong connection string — Double-check the hostname, port, database name, and credentials.
Missing schema (PostgreSQL) — If you use --schema auth, the schema must exist first. Create it with CREATE SCHEMA auth.
Not using --yes in CI — Without it, the CLI waits for user input and the job hangs.
Running on the wrong database — Use different DATABASE_URL values for dev, staging, and production.
Related pages
- Installation — How to set up permly
- MySQL & MariaDB — MySQL setup details
- PostgreSQL — PostgreSQL setup details
- MongoDB — MongoDB setup details