Database Setup
Three supported paths for creating the auth tables.
The library ships fourteen TypeORM entities, exported as a bundle:
You have three ways to actually create the tables. Pick whichever matches your team's deploy story.
Path 1 — synchronize: true (development only)
Easiest. Tables are created automatically on app boot from the entity metadata.
When synchronize: true:
- ✅ First boot creates every auth table.
- ✅ Adding a column to an entity adds it to the DB on next boot.
- ⚠️ Removing a column drops it. Don't use this in production.
Use this for local dev and ephemeral test environments. Switch to one of the other paths for staging/prod.
Path 2 — TypeORM migrations
The recommended production path. You let TypeORM diff your entity metadata against the live schema and generate a migration file.
Generate a migration:
The generated file contains the full set of CREATE TABLE, indexes, and foreign keys. Review it, commit it, and run:
When the library version bumps and entities change, migration:generate again — TypeORM emits only the diff.
Path 3 — Copy-paste SQL
For teams that don't want to wire up TypeORM CLI, or that manage migrations with Flyway / Liquibase / Sqitch / handwritten SQL. The library's docs ship a deterministic SQL snapshot per dialect, generated from the entity metadata.
The snapshots live at:
| Dialect | URL |
|---|---|
| Postgres | /sql/postgres.sql |
| MySQL | /sql/mysql.sql |
| SQLite | /sql/sqlite.sql |
(These are also checked into the docs site at apps/docs/public/sql/ and regenerated on every release by apps/docs/scripts/build-sql-snapshots.ts.)
Paste the snapshot into your migration tool of choice. When the library bumps, diff the new snapshot against your applied schema and write the alteration.
What gets created
Fourteen tables — see Entities for the full list and field tables.
| Table | Purpose |
|---|---|
nest_auth_users | Auth user fields |
nest_auth_sessions | Active sessions |
nest_auth_identities | OAuth provider linkage |
nest_auth_access_keys | API keys |
nest_auth_otps | One-time codes |
nest_auth_mfa_secrets | TOTP secrets |
nest_auth_trusted_devices | MFA bypass tokens |
nest_auth_roles | RBAC roles |
nest_auth_permissions | RBAC permissions |
nest_auth_role_permissions | Role↔permission |
nest_auth_tenants | Tenants |
nest_auth_user_accesses | Per-tenant memberships |
nest_auth_platform_accesses | Cross-tenant staff roles |
nest_auth_admin_users | Admin console users |
Recommended indexes
Already shipped on the entities. If you're hand-rolling SQL, make sure these exist:
Foreign keys
nest_auth_user.id is referenced (with ON DELETE CASCADE) by:
nest_auth_sessions.user_idnest_auth_identities.user_idnest_auth_otps.user_idnest_auth_mfa_secrets.user_idnest_auth_access_keys.user_idnest_auth_trusted_devices.user_idnest_auth_user_accesses.user_idnest_auth_platform_accesses.user_id
Deleting a user removes everything attached to them. Match this on your AppUser table — see User Model.
Seeding
The example app under apps/example-nest/seeder.config.js shows a simple seeder for dev users. For production, seed via a one-shot migration that calls your UserService rather than inserting raw rows — that way password hashing and event emission run.
Next
- Environment & Secrets.
- Quickstart — Backend.
- Entities — column-level details.