Skip to main content

Creating the Prisma schema

Update the Prisma schema

Open the prisma/schema.prisma file and replace the default contents with the following:

datasource db {
provider = "mongodb"
url = env("DATABASE_URL")

generator client {
provider = "prisma-client-js"

model Post {
id String @id @default(auto()) @map("_id") @db.ObjectId
slug String @unique
title String
body String
author User @relation(fields: [authorId], references: [id])
authorId String @db.ObjectId
comments Comment[]

model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
email String @unique
name String?
address Address?
posts Post[]

model Comment {
id String @id @default(auto()) @map("_id") @db.ObjectId
comment String
post Post @relation(fields: [postId], references: [id])
postId String @db.ObjectId

// Address is an embedded document
type Address {
street String
city String
state String
zip String

There are also a number of subtle differences in how the schema is setup when compared to relational databases like PostgreSQL.

For example, the underlying ID field name is always _id and must be mapped with @map("_id").

For more information check out the MongoDB schema reference.