Migrations
Learn how to modify your database schema and push changes.
When building your application, you'll need to make changes to your database schema (e.g., adding new collections, fields, or changing data types).
MongoDB with Prisma does not use traditional SQL migrations.
This means you don't need to create migrations files to apply changes to the database. This means less time spent on database migrations and more time spent on building your application. It also means you don't need to worry about database schema changes when deploying your application. Ship faster.
Instead, changes are pushed directly to the database with db push.
The database schema is defined in packages/db/prisma/schema.prisma.
Making Schema Changes
Edit the Prisma Schema
Open packages/db/prisma/schema.prisma and make your changes.
Follow the MongoDB conventions:
model Post {
id String @id @default(auto()) @map("_id") @db.ObjectId
userId String @db.ObjectId
title String
content String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([userId])
@@map("posts")
}Prisma Commands
| Command | Description |
|---|---|
pnpm db:generate | Generates the Prisma client based on your schema for type-safe database access. |
pnpm db:push | Pushes schema changes directly to the database. |
Always run pnpm db:generate after modifying schema.prisma.
The build and dev tasks depend on it automatically via Turborepo.
Adding a New Model
- Add the model to
packages/db/prisma/schema.prismafollowing MongoDB conventions. - Add any new enums to both
schema.prismaandpackages/utils/src/types/db.ts. - Run
pnpm db:generatethenpnpm db:push. - Create a repository in
packages/api/src/routers/{domain}/{domain}.repository.ts. - Create service and router files (see the API section).