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")
}

Generate the Prisma Client

After modifying the schema, regenerate the Prisma client:

pnpm db:generate

Push to the database

Apply the changes to your database:

pnpm db:push

Prisma Commands

CommandDescription
pnpm db:generateGenerates the Prisma client based on your schema for type-safe database access.
pnpm db:pushPushes 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

  1. Add the model to packages/db/prisma/schema.prisma following MongoDB conventions.
  2. Add any new enums to both schema.prisma and packages/utils/src/types/db.ts.
  3. Run pnpm db:generate then pnpm db:push.
  4. Create a repository in packages/api/src/routers/{domain}/{domain}.repository.ts.
  5. Create service and router files (see the API section).