跳到主要內容

建立 Prisma Schema

更新 Prisma Schema

開啟 prisma/schema.prisma 檔案,並將預設內容替換為以下內容

prisma/schema.prisma
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
}

與 PostgreSQL 等關聯式資料庫相比,結構描述的設定方式也存在一些細微差異。

例如,底層 ID 欄位名稱始終為 _id,並且必須使用 @map("_id") 進行映射。

如需更多資訊,請查看 MongoDB 結構描述參考