Creating the Prisma schema

TypeScript
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}
5
6generator client {
7 provider = "prisma-client-js"
8}
9
10model Post {
11 id String @id @default(auto()) @map("_id") @db.ObjectId
12 slug String @unique
13 title String
14 body String
15 author User @relation(fields: [authorId], references: [id])
16 authorId String @db.ObjectId
17 comments Comment[]
18}
19
20model User {
21 id String @id @default(auto()) @map("_id") @db.ObjectId
22 email String @unique
23 name String?
24 address Address?
25 posts Post[]
26}
27
28model Comment {
29 id String @id @default(auto()) @map("_id") @db.ObjectId
30 comment String
31 post Post @relation(fields: [postId], references: [id])
32 postId String @db.ObjectId
33}
34
35// Address is an embedded document
36type Address {
37 street String
38 city String
39 state String
40 zip String
41}

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.