Database Models
Working with Prisma database models in the went framework.
Schema Configuration
prisma/schema.prisma
generator client { provider = "prisma-client-js" output = "../src/generated/prisma" } datasource db { provider = "postgresql" url = env("DATABASE_URL") directUrl = env("DIRECT_URL") }
The went framework uses PostgreSQL as the default database and generates the Prisma client to src/generated/prisma
for better organization.
Default Models
User Model
model User { id String @id @default(cuid()) email String @unique createdAt DateTime @default(now()) updatedAt DateTime @updatedAt orders Order[] }
Fields:
- •
id
- Unique identifier using CUID - •
email
- User email address (unique) - •
createdAt
- Timestamp of creation - •
updatedAt
- Timestamp of last update - •
orders
- Related orders (one-to-many)
Order Model
model Order { id String @id @default(cuid()) userId String stripePaymentId String @unique status String createdAt DateTime @default(now()) stripePriceId String templateName String user User @relation(fields: [userId], references: [id]) }
Fields:
- •
id
- Unique identifier using CUID - •
userId
- Reference to User model - •
stripePaymentId
- Stripe payment ID (unique) - •
status
- Order status - •
stripePriceId
- Stripe price identifier - •
templateName
- Name of purchased template - •
user
- Related user (many-to-one)
Working with Models
Database Client
Import the Prisma client from the generated directory:
import { PrismaClient } from '@/generated/prisma' const prisma = new PrismaClient() // Create a new user const user = await prisma.user.create({ data: { email: 'user@example.com' } }) // Find user with orders const userWithOrders = await prisma.user.findUnique({ where: { email: 'user@example.com' }, include: { orders: true } })
Common Patterns
Creating Relations
// Create order with user relation const order = await prisma.order.create({ data: { stripePaymentId: 'pi_abc123', status: 'completed', stripePriceId: 'price_123', templateName: 'SaaS Ready', user: { connect: { id: userId } } } })
Querying with Filters
// Find orders by status const completedOrders = await prisma.order.findMany({ where: { status: 'completed' }, include: { user: true }, orderBy: { createdAt: 'desc' } })
Database Commands
went CLI Database Commands
went db migrate init
Initialize and run database migrations
went db generate
Generate Prisma client after schema changes
went db studio
Open Prisma Studio for database GUI
went db reset
Reset database and apply all migrations
went db seed
Seed database with initial data
Best Practices
Schema Design
- • Use descriptive field names
- • Add indexes for frequently queried fields
- • Use appropriate data types
- • Define proper relationships
Query Optimization
- • Use
select
to limit returned fields - • Use
include
carefully to avoid N+1 queries - • Implement pagination for large datasets
- • Use database indexes for performance
Migration Safety
- • Always backup production data before migrations
- • Test migrations in development first
- • Use
went db migrate init
for new migrations - • Review generated SQL before applying
Next Steps
Continue your journey with Went: