Building Database Apps
Cubby automatically provisions Postgres databases for apps that use Prisma. This guide covers everything you need to know about database-backed apps.Automatic Database Provisioning
When Cubby detects Prisma in your project (viaprisma/schema.prisma), it automatically:
- Provisions a Postgres 17 container alongside your app
- Creates a named volume for data persistence
- Injects
DATABASE_URLinto your environment - Runs migrations via
prisma db pushon each deploy
Prisma Setup
Thecubby init template includes Prisma pre-configured:
The Prisma Client Singleton
lib/db.ts provides a singleton Prisma client that prevents connection exhaustion during development:
Defining Models
Editprisma/schema.prisma to define your data models:
Common Field Types
| Prisma Type | PostgreSQL Type | Example |
|---|---|---|
String | TEXT | name String |
Int | INTEGER | count Int |
Float | DOUBLE PRECISION | price Float |
Boolean | BOOLEAN | active Boolean |
DateTime | TIMESTAMP | createdAt DateTime |
Json | JSONB | metadata Json |
Default Values
Relations
CRUD Operations
Create
Read
Update
Delete
Local Development
cubby dev handles everything:
- Starts a local Postgres container
- Syncs your schema with
prisma db push - Makes the database available at
localhost:5432
Resetting Local Data
To start with a fresh database:Manual Schema Sync
If you need to sync manually:Deployment
When you runcubby deploy:
- Cubby detects
prisma/schema.prisma - Provisions (or reuses) a Postgres container
- Runs
prisma db pushto sync the schema - Injects
DATABASE_URLinto your app
User-Scoped Data
Most Cubby apps store user-specific data. Use theuserId from headers:
Best Practices
Use Transactions for Related Updates
Index Frequently Queried Fields
Handle Not Found
Related
- Authentication - Access user identity
cubby dev- Local development with databasecubby deploy- Deploy with database provisioning