Overview
Documenso uses Prisma as its ORM (Object-Relational Mapping) tool with PostgreSQL as the database. This guide covers database setup, schema management, migrations, and common operations.Database Stack
- Database: PostgreSQL 15+
- ORM: Prisma 6.19.0
- Query Builder: Kysely (via prisma-kysely extension)
- Schema Location:
packages/prisma/schema.prisma - Migrations:
packages/prisma/migrations/
Schema Overview
The Prisma schema defines the entire database structure including:- Users & Authentication: User accounts, sessions, passkeys, OAuth providers
- Documents & Envelopes: Document storage, signing workflows, templates
- Recipients & Fields: Signer information, form fields, signatures
- Organizations & Teams: Multi-tenant structure, team management
- Subscriptions & Billing: Stripe integration, subscription claims
- Webhooks & API: Webhook configurations, API tokens
- Storage: Document data, attachments, avatar images
packages/prisma/schema.prisma.
Database Setup
Using Docker (Recommended)
The quickest way to set up a PostgreSQL database:Start Docker services
localhost:54320 with:- User:
documenso - Password:
password - Database:
documenso
Manual PostgreSQL Setup
If you have PostgreSQL installed locally:Prisma Commands
All Prisma commands are available as npm scripts:Running Commands with Environment Variables
If running Prisma commands directly, wrap them withwith:env:
Working with Migrations
Creating a Migration
When you modify the Prisma schema:Update the schema
Edit
packages/prisma/schema.prisma to add, modify, or remove models and fields.Example:Create the migration
- Prompt you for a migration name
- Generate SQL migration files
- Apply the migration to your database
- Regenerate the Prisma Client
Migration Files
Migrations are stored inpackages/prisma/migrations/ with timestamps:
migration.sql- SQL statements to apply the migration
Applying Migrations in Production
For production deployments, usemigrate deploy instead of migrate dev:
- Applies pending migrations
- Does NOT prompt for input (CI/CD friendly)
- Does NOT automatically create migrations
- Does NOT reset the database
Resetting the Database
To completely reset your database (useful for development):- Drop the database
- Recreate it
- Apply all migrations
- Run the seed script (if configured)
Database Seeding
The seed script creates test data for development:Running the Seed
Seed Script Location
The seed script is located atpackages/prisma/seed-database.ts and configured in packages/prisma/package.json:
What Gets Seeded
The seed script typically creates:- Test user accounts
- Sample documents
- Example templates
- Test recipients and signatures
packages/prisma/seed/ for details.
Prisma Studio
Prisma Studio is a visual database browser:Access the interface
Open http://localhost:5555 in your browser
Prisma Client Usage
The Prisma Client is auto-generated and provides type-safe database queries.Importing Prisma Client
Example Queries
Database Connection
Connection Strings
Documenso uses two connection strings:-
Standard Connection (
NEXT_PRIVATE_DATABASE_URL):- Used for normal application queries
- Can use connection pooling (e.g., PgBouncer)
- Format:
postgres://user:password@host:port/database
-
Direct Connection (
NEXT_PRIVATE_DIRECT_DATABASE_URL):- Used for migrations and schema operations
- Must be a direct connection (no pooling)
- Format: Same as above for most setups
Connection Pooling
For production, consider using connection pooling:Code Generators
Prisma schema uses multiple generators:Prisma Client
Kysely
Zod Schemas
JSON Types
Database Schema Conventions
When modifying the schema, follow these conventions:Naming
- Model names: PascalCase (e.g.,
User,Envelope,DocumentMeta) - Field names: camelCase (e.g.,
userId,createdAt,emailVerified) - Enum values: SCREAMING_SNAKE_CASE (e.g.,
DRAFT,PENDING,COMPLETED)
Timestamps
Include timestamps on most models:IDs
Use appropriate ID types:Relations
Always define both sides of relations:Troubleshooting
Migration Failed
If a migration fails:- Check the error message for SQL issues
- Review the migration SQL file
- Fix the schema and try again
- If needed, reset the database:
npm run prisma:migrate-reset
Schema Out of Sync
If your database schema doesn’t match the Prisma schema:Prisma Client Not Found
Regenerate the client:Type Errors After Schema Changes
After modifying the schema:- Run
npm run prisma:migrate-dev(generates new types) - Restart your TypeScript server
- Update code to match new types
