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}
5
6generator client {
7 provider = "prisma-client-js"
8}
9model Post {
10 id String @id @default(auto()) @map("_id") @db.ObjectId
11 slug String @unique
12 title String
13 body String
14 comments Comment[]
15 author User @relation(fields: [authorId], references: [id])
16 authorId String @db.ObjectId
17}
18
19// Comments contain a comment string and connect back to the post.
20// postId must have @db.ObjectId to match up with Post's id type
21model Comment {
22 id String @id @default(auto()) @map("_id") @db.ObjectId
23 post Post @relation(fields: [postId], references: [id])
24 postId String @db.ObjectId
25 comment String
26}
27
28model User {
29 id String @id @default(auto()) @map("_id") @db.ObjectId
30 email String @unique
31 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.

Edit this page on GitHub