Prisma Schema 概覽
Prisma Schema(簡稱 schema)是設定 Prisma ORM 的主要方式。它由以下幾個部分組成:
- 資料來源 (Data sources):指定 Prisma ORM 應連接的資料來源詳細資訊(例如 PostgreSQL 資料庫)。
- 產生器 (Generators):指定應根據資料模型產生哪些用戶端(例如 Prisma Client)。
- 資料模型定義 (Data model definition):指定您的應用程式模型(每個資料來源的資料結構)及其關聯。
它通常是一個名為 schema.prisma 的單一檔案(或是副檔名為 .prisma 的多個檔案),儲存在一個預設但可自訂的位置。如果您喜歡,也可以將 Prisma Schema 組織在多個檔案中。
請參閱 Prisma Schema API 參考 以獲取關於 schema 每個部分的詳細資訊。
每當執行 prisma 指令時,CLI 通常會從 schema 中讀取某些資訊,例如:
prisma generate:讀取上述 Prisma Schema 中的所有資訊,以產生正確的資料來源用戶端程式碼(例如 Prisma Client)。prisma migrate dev:讀取資料來源和資料模型定義,以建立新的遷移 (migration)。
您也可以在 schema 中使用環境變數,以便在呼叫 CLI 指令時提供設定選項。
範例
以下是一個指定了以下內容的 Prisma Schema 範例:
- 資料來源(PostgreSQL 或 MongoDB)
- 產生器(Prisma Client)
- 包含兩個模型(具有一個關聯)和一個
enum的資料模型定義 - 數個原生資料類型屬性(
@db.VarChar(255),@db.ObjectId)
- 關聯式資料庫
- MongoDB
datasource db {
provider = "postgresql"
}
generator client {
provider = "prisma-client"
output = "./generated"
}
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
email String @unique
name String?
role Role @default(USER)
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
published Boolean @default(false)
title String @db.VarChar(255)
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}
enum Role {
USER
ADMIN
}
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
createdAt DateTime @default(now())
email String @unique
name String?
role Role @default(USER)
posts Post[]
}
model Post {
id String @id @default(auto()) @map("_id") @db.ObjectId
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
published Boolean @default(false)
title String
author User? @relation(fields: [authorId], references: [id])
authorId String @db.ObjectId
}
enum Role {
USER
ADMIN
}
語法
Prisma Schema 檔案使用 Prisma Schema Language (PSL) 編寫。請參閱資料來源、產生器、資料模型定義以及 Prisma Schema API 參考頁面以獲取詳細資訊和範例。
VS Code
PSL 的語法高亮可透過 VS Code 擴充功能 取得(它還允許您自動格式化 Prisma Schema 的內容,並用紅色波浪線標示語法錯誤)。進一步了解如何在您的編輯器中設定 Prisma ORM。
GitHub
GitHub 上的 PSL 程式碼片段也可以透過使用 .prisma 副檔名,或在 Markdown 中將程式碼區塊標記為 prisma 來呈現語法高亮。
```prisma
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
email String @unique
name String?
}
```
從 Schema 存取環境變數
您可以使用環境變數在呼叫 CLI 指令或執行 Prisma Client 查詢時提供設定選項。
雖然可以直接在 Schema 中硬編碼 URL,但並不建議這麼做,因為這會帶來安全風險。在 Schema 中使用環境變數可以讓您將機密資訊排除在 Schema 之外,進而透過在不同環境中使用相同的 Schema 來提高其可攜性。
可以使用 env() 函式存取環境變數:
datasource db {
provider = "postgresql"
}
您可以在以下位置使用 env() 函式:
- 資料來源的 url
- 產生器的二進位目標 (binary targets)
請參閱環境變數以獲取有關如何在開發期間使用 .env 檔案的更多資訊。
註解
Prisma Schema Language 支援三種註解:
// 註解:此註解用於讓閱讀者更清晰,不會出現在 Schema 的抽象語法樹 (AST) 中。/// 註解:這些註解會以 AST 節點描述的形式出現在 Schema 的抽象語法樹 (AST) 中。工具隨後可以使用這些註解來提供額外資訊。所有註解都會附加到下一個可用的節點 - 懸浮註解 是不被支援的,也不會包含在 AST 中。/* 區塊註解 */:這些註解與///註解類似,也會出現在抽象語法樹中。
以下是一些不同的範例:
/// This comment will get attached to the `User` node in the AST
model User {
/// This comment will get attached to the `id` node in the AST
id Int @default(autoincrement())
// This comment is just for you
weight Float /// This comment gets attached to the `weight` node
}
// This comment is just for you. It will not
// show up in the AST.
/// This comment will get attached to the
/// Customer node.
model Customer {
/**
* ...and so will this comment
*/
}
自動格式化
Prisma ORM 支援自動格式化 .prisma 檔案。有兩種方式可以格式化 .prisma 檔案:
- 執行
prisma format指令。 - 安裝 Prisma VS Code 擴充功能 並呼叫 VS Code 格式化操作——可手動或在儲存時執行。
沒有設定選項——格式化規則是固定的(類似於 Golang 的 gofmt,但與 Javascript 的 prettier 不同)。
格式化規則
設定區塊會根據其 = 符號進行對齊
block _ {
key = "value"
key2 = 1
long_key = true
}
欄位定義會對齊為由 2 個或更多空格分隔的欄位
block _ {
id String @id
first_name LongNumeric @default
}
空行會重設區塊對齊和格式化規則
block _ {
key = "value"
key2 = 1
key10 = true
long_key = true
long_key_2 = true
}
block _ {
id String @id
@default
first_name LongNumeric @default
}
多行欄位屬性會與其餘欄位屬性適當地對齊
block _ {
id String @id
@default
first_name LongNumeric @default
}
區塊屬性會排序到區塊的末尾
block _ {
key = "value"
@@attribute
}