Prisma 配接器
資源
設定
安裝
npm install @prisma/client @auth/prisma-adapter
npm install prisma --save-dev
環境變數
Prisma 需要設定環境變數,以建立與資料庫的連線並檢索資料。Prisma 需要 DATABASE_URL
環境變數來建立連線。如需詳細資訊,請閱讀文件。
DATABASE_URL=postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA
組態
為了使用 Prisma ORM
改善效能,我們可以設定 Prisma 執行個體,以確保整個專案只會建立一個執行個體,然後在任何需要時從任何檔案匯入。這種方法可避免每次使用時都重新建立 PrismaClient 的執行個體。最後,我們可以從 auth.ts
檔案組態匯入 Prisma 執行個體。
import { PrismaClient } from "@prisma/client"
const globalForPrisma = globalThis as unknown as { prisma: PrismaClient }
export const prisma = globalForPrisma.prisma || new PrismaClient()
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma
如果使用中介軟體或任何其他 edge 執行階段,我們建議使用 @prisma/client@5.12.0
或更新版本。請參閱下方的edge 相容性以取得更多資訊。
import NextAuth from "next-auth"
import { PrismaAdapter } from "@auth/prisma-adapter"
import { prisma } from "@/prisma"
export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: PrismaAdapter(prisma),
providers: [],
})
Edge 相容性
Prisma 已在其客戶端 5.12.0
版本中提供 edge 執行階段支援。您可以在他們的edge 文件中閱讀更多相關資訊。這需要特定的資料庫驅動程式,因此僅與某些資料庫類型/託管提供者相容。在開始之前,請查看他們的支援的驅動程式清單。您可以查看一個 Auth.js 應用程式範例,其中 next-auth
和 Prisma 在 edge 上這裡。
如需一般 edge 相容性的詳細資訊,請查看我們的edge 相容性指南。
原始資料庫 edge 執行階段的解決方案 (將您的 auth.ts
組態分割成兩個) 將保留在下方。
舊版 Edge 解決方案
目前,Prisma 仍在努力完全相容於 Vercel 等 edge 執行階段。請參閱這裡追蹤的問題,以及 Prisma 在 5.9.1
變更記錄中關於早期 edge 支援的公告。有兩種選項可處理此問題
- 使用 Prisma 的 Accelerate 功能
- 依照我們的Edge 相容性頁面作為解決方案。這會使用
jwt
工作階段策略,並將auth.ts
組態分成兩個檔案。
搭配 jwt
工作階段策略和 @prisma/client@5.9.1
或更新版本使用 Prisma,除了確保您不在中介軟體中執行任何資料庫查詢之外,不需要任何額外的修改。
由於 @prisma/client@5.9.1
,Prisma 不再於實例化時拋出與 edge 執行階段不相容的問題,而是在查詢時拋出。因此,只要您不在中介軟體中執行任何查詢,就可以在您的中介軟體中使用的檔案中匯入它。
綱要
您需要使用至少 Prisma 2.26.0
。在 prisma/schema.prisma
建立具有下列模型的綱要檔案。
套用綱要
這將建立 SQL 移轉檔案並執行它
npm exec prisma migrate dev
請注意,您需要在環境變數 DATABASE_URL
中指定資料庫連線字串。您可以在專案根目錄的 .env
檔案中設定。請注意,Prisma 不支援 .env.local
語法,它必須命名為 .env
。如需詳細資訊,請查看他們的環境變數文件。
產生 Prisma 用戶端
prisma migrate dev
也會產生 Prisma 用戶端,但如果您需要再次手動產生,可以執行下列命令。
npm exec prisma generate
開發工作流程
當您在應用程式上工作並變更資料庫綱要時,您每次變更綱要時都需要再次執行移轉命令,以便 Prisma (1) 產生移轉檔案並將其套用至基礎資料庫,以及 (2) 在您的專案中重新產生具有最新類型和模型方法的 Prisma 用戶端。
npm exec prisma migrate dev
命名慣例
如果混合使用 snake_case
和 camelCase
資料行名稱對您和/或您的基礎資料庫系統造成問題,我們建議使用 Prisma 的 @map()
功能來變更欄位名稱。這不會影響 Auth.js,但可讓您自訂資料行名稱以符合您偏好的任何命名慣例。
例如,改用 snake_case
和複數形式的資料表名稱。
model Account {
id String @id @default(cuid())
userId String @map("user_id")
type String
provider String
providerAccountId String @map("provider_account_id")
refresh_token String? @db.Text
access_token String? @db.Text
expires_at Int?
token_type String?
scope String?
id_token String? @db.Text
session_state String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@unique([provider, providerAccountId])
@@map("accounts")
}
model Session {
id String @id @default(cuid())
sessionToken String @unique @map("session_token")
userId String @map("user_id")
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@map("sessions")
}
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime? @map("email_verified")
image String?
accounts Account[]
sessions Session[]
@@map("users")
}
model VerificationToken {
identifier String
token String
expires DateTime
@@unique([identifier, token])
@@map("verification_tokens")
}