Creating the Prisma schema
JavaScript
MongoDB
Update the Prisma schema
Open the prisma/schema.prisma
file and replace the default contents with the following:
prisma/schema.prisma
1datasource db {2 provider = "mongodb"3 url = env("DATABASE_URL")4}56generator client {7 provider = "prisma-client-js"8}910model Post {11 id String @id @default(auto()) @map("_id") @db.ObjectId12 slug String @unique13 title String14 body String15 author User @relation(fields: [authorId], references: [id])16 authorId String @db.ObjectId17 comments Comment[]18}1920model User {21 id String @id @default(auto()) @map("_id") @db.ObjectId22 email String @unique23 name String?24 address Address?25 posts Post[]26}2728model Comment {29 id String @id @default(auto()) @map("_id") @db.ObjectId30 comment String31 post Post @relation(fields: [postId], references: [id])32 postId String @db.ObjectId33}3435// Address is an embedded document36type Address {37 street String38 city String39 state String40 zip String41}
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.