Database schema
The application uses the following database tables:User
Stores user accounts and authentication information.id- UUID primary key, auto-generatedemail- User email address (max 64 characters)password- Hashed password (nullable for guest users)
Chat
Stores chat sessions and metadata.id- UUID primary key, auto-generatedcreatedAt- Timestamp when chat was createdtitle- Chat title/nameuserId- Reference to User tablevisibility- Eitherpublicorprivate(default:private)
Message_v2
Stores chat messages with support for multi-part content and attachments.id- UUID primary key, auto-generatedchatId- Reference to Chat tablerole- Message role (user, assistant, system)parts- JSON array of message parts (text, code, images, etc.)attachments- JSON array of file attachmentscreatedAt- Timestamp when message was created
The
Message_v2 table replaces the deprecated Message table. See the migration guide for details.Vote_v2
Stores user feedback (upvotes/downvotes) on messages.chatId- Reference to Chat tablemessageId- Reference to Message_v2 tableisUpvoted- True for upvote, false for downvote- Composite primary key on (
chatId,messageId)
Document
Stores user documents created in the chat (text, code, images, spreadsheets).id- UUID, auto-generatedcreatedAt- Timestamp when document was createdtitle- Document titlecontent- Document content (text, code, etc.)kind- Document type:text,code,image, orsheetuserId- Reference to User table- Composite primary key on (
id,createdAt)
Suggestion
Stores AI-generated suggestions for document improvements.id- UUID primary key, auto-generateddocumentId+documentCreatedAt- Composite foreign key to DocumentoriginalText- Original text being improvedsuggestedText- AI-suggested improvementdescription- Description of the suggestionisResolved- Whether suggestion was accepted/rejecteduserId- User who requested the suggestioncreatedAt- Timestamp when suggestion was created
Stream
Stores streaming session metadata.Database configuration
The database is configured using Drizzle Kit indrizzle.config.ts:
drizzle.config.ts
schema- TypeScript schema definition (lib/db/schema.ts)out- Migration files output directory (lib/db/migrations)dialect- Database type (postgresql)dbCredentials.url- Connection string fromPOSTGRES_URLenvironment variable
Setting up PostgreSQL
Option 1: Vercel Postgres (recommended)
The easiest way to set up PostgreSQL is using Vercel Postgres:Create database
- Go to your Vercel project dashboard
- Click on the “Storage” tab
- Click “Create Database”
- Select “Postgres”
- Choose a region close to your deployment
Connect to project
Vercel automatically adds the following environment variables to your project:
POSTGRES_URLPOSTGRES_PRISMA_URLPOSTGRES_URL_NON_POOLING- Additional connection details
Option 2: Other PostgreSQL providers
You can use any PostgreSQL provider:- Neon - Serverless Postgres (https://neon.tech)
- Supabase - Open-source Firebase alternative (https://supabase.com)
- Railway - Simple cloud database (https://railway.app)
- Amazon RDS - AWS managed database
- Google Cloud SQL - GCP managed database
- Self-hosted - Your own PostgreSQL instance
POSTGRES_URL environment variable with your connection string:
Running migrations
Migrations are managed using Drizzle ORM and stored inlib/db/migrations/.
Automatic migrations (during build)
Migrations run automatically during the build process:lib/db/migrate.ts):
lib/db/migrate.ts
If
POSTGRES_URL is not defined, migrations are skipped gracefully. This is useful for preview deployments without a database.Manual migrations
You can also run migrations manually:Generating new migrations
When you modify the schema inlib/db/schema.ts, generate a migration:
lib/db/migrations/ based on schema changes.
Database management commands
Thepackage.json includes several database management scripts:
package.json
Available commands
db:generate
db:generate
Generate migration files from schema changes:Use this after modifying
lib/db/schema.ts to create a new migration.db:migrate
db:migrate
Run pending migrations:Applies all migrations from
lib/db/migrations/ that haven’t been run yet.db:studio
db:studio
Open Drizzle Studio (database GUI):Launches a web interface to browse and edit your database.
db:push
db:push
Push schema changes directly to database:⚠️ Warning: This bypasses migrations and directly modifies your database. Use for development only.
db:pull
db:pull
Pull schema from existing database:Introspects your database and generates a schema file.
db:check
db:check
Validate migration files:Checks for issues in your migrations.
Troubleshooting
Migration failed - connection refused
Migration failed - connection refused
Cause: Cannot connect to PostgreSQL database.Solutions:
- Verify
POSTGRES_URLis correctly set - Check database is running and accessible
- Verify network/firewall allows connections
- Test connection:
psql $POSTGRES_URL
Migration failed - permission denied
Migration failed - permission denied
Cause: Database user lacks necessary permissions.Solutions:
- Ensure database user has CREATE and ALTER permissions
- Check user can create tables and modify schema
- On managed services, verify user role/permissions
Duplicate migration error
Duplicate migration error
Cause: Migration has already been applied.Solutions:
- Check migration history in database
- Drizzle tracks applied migrations automatically
- If needed, manually remove migration from tracking table
Schema mismatch
Schema mismatch
Cause: Database schema doesn’t match TypeScript schema.Solutions:
- Generate a new migration:
pnpm db:generate - Review the generated migration file
- Apply migration:
pnpm db:migrate - For development, you can use:
pnpm db:push(destructive)
Best practices
- Always use migrations - Don’t manually modify production database schema
- Review migrations - Check generated migration files before applying
- Test migrations - Test on development/staging before production
- Backup before migrating - Always backup production data before running migrations
- Version control migrations - Commit migration files to Git
- Never edit applied migrations - Create new migrations for changes
- Use transactions - Migrations run in transactions by default for safety
Next steps
Deploy to Vercel
Deploy your application with the configured database
Environment variables
Configure remaining environment variables