Creating the Prisma schema
JavaScript
MongoDB
Manually create the Prisma schema
Open the prisma/schema.prisma
file and replace the default configurations 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}9model Post {10 id String @id @default(auto()) @map("_id") @db.ObjectId11 slug String @unique12 title String13 body String14 comments Comment[]15 author User @relation(fields: [authorId], references: [id])16 authorId String @db.ObjectId17}1819// Comments contain a comment string and connect back to the post.20// postId must have @db.ObjectId to match up with Post's id type21model Comment {22 id String @id @default(auto()) @map("_id") @db.ObjectId23 post Post @relation(fields: [postId], references: [id])24 postId String @db.ObjectId25 comment String26}2728model User {29 id String @id @default(auto()) @map("_id") @db.ObjectId30 email String @unique31 name String?32 posts Post[]33}
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.