如何從 Sequelize 遷移到 Prisma ORM
簡介
本指南將示範如何將您的應用程式從 Sequelize 遷移到 Prisma ORM。我們將使用 Sequelize Express 範例 的擴展版本作為 範例專案,以示範遷移步驟。
本遷移指南使用 PostgreSQL 作為範例資料庫,但同樣適用於任何其他 Prisma ORM 支援的關聯式資料庫。您可以在 Prisma ORM 與 Sequelize 的比較 頁面上了解 Prisma ORM 與 Sequelize 的比較。
先決條件
開始本指南之前,請確保您已具備
- 您想要遷移的 Sequelize 專案
- 已安裝 Node.js (版本 18 或更高版本)
- PostgreSQL 或另一個支援的資料庫
- 基本熟悉 Sequelize 和 Express.js
1. 準備遷移
1.1. 了解遷移過程
從 Sequelize 遷移到 Prisma ORM 的步驟始終相同,無論您正在建構何種類型的應用程式或 API 層
- 安裝 Prisma CLI
- 內省您的資料庫
- 建立基準遷移
- 安裝 Prisma Client
- 逐步將您的 Sequelize 查詢替換為 Prisma Client
無論您是使用 REST API (例如,使用 Express、Koa 或 NestJS)、GraphQL API (例如,使用 Apollo Server、TypeGraphQL 或 Nexus) 還是任何其他使用 Sequelize 進行資料庫存取的應用程式,這些步驟都適用。
1.2. 設定 Prisma 組態
建立新的 Prisma schema 檔案
npx prisma init
此命令建立一個名為 prisma
的新目錄,其中包含以下檔案
schema.prisma
:您的 Prisma schema,指定您的資料庫連線和模型.env
:一個dotenv
,用於將您的資料庫連線 URL 設定為環境變數
Prisma schema 目前如下所示
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
如果您使用 VS Code,請務必安裝 Prisma VS Code 擴充套件,以獲得語法突顯、格式化、自動完成以及更多酷炫功能。
使用您的資料庫連線字串更新 .env
檔案中的 DATABASE_URL
DATABASE_URL="postgresql://USER:PASSWORD@HOST:PORT/DATABASE"
2. 遷移資料庫 schema
2.1. 內省您的資料庫
執行 Prisma 的內省以從您現有的資料庫建立 Prisma schema
npx prisma db pull
這將建立一個包含您的資料庫 schema 的 schema.prisma
檔案。
2.2. 建立基準遷移
若要繼續使用 Prisma Migrate 來演進您的資料庫 schema,您需要建立資料庫基準。
首先,建立一個 migrations
目錄,並在其中新增一個目錄,並以您偏好的名稱作為遷移名稱。在本範例中,我們將使用 0_init
作為遷移名稱
mkdir -p prisma/migrations/0_init
接下來,使用 prisma migrate diff
產生遷移檔案。使用以下引數
--from-empty
:假設您要遷移的資料模型是空的--to-schema-datamodel
:使用datasource
區塊中的 URL 的目前資料庫狀態--script
:輸出 SQL 指令碼
npx prisma migrate diff --from-empty --to-schema-datamodel prisma/schema.prisma --script > prisma/migrations/0_init/migration.sql
npx prisma migrate resolve --applied 0_init
此命令會將 0_init
標記為已套用,方法是將其新增至 _prisma_migrations
表格。
您現在已擁有目前資料庫 schema 的基準。若要進一步變更您的資料庫 schema,您可以更新您的 Prisma schema 並使用 prisma migrate dev
將變更套用至您的資料庫。
3. 更新您的應用程式程式碼
3.1. 安裝 Prisma Client
下一步,您可以在您的專案中安裝 Prisma Client,以便您可以開始替換專案中目前使用 Sequelize 進行的資料庫查詢
npm install @prisma/client
安裝 Prisma Client 後,您可以產生 Prisma Client 程式碼
npx prisma generate
3.2. 替換 Sequelize 查詢
在本節中,我們將展示一些範例查詢,這些查詢是根據範例 REST API 專案中的範例路由從 Sequelize 遷移到 Prisma Client 的。如需 Prisma Client API 與 Sequelize 的差異的完整概述,請查看 API 比較 頁面。
- Sequelize
- Prisma Client
// Find one
const user = await User.findOne({
where: { id: 1 }
});
// Create
const user = await User.create({
email: 'alice@prisma.io',
name: 'Alice'
});
// Update
await User.update({ name: 'New name' }, {
where: { id: 1 }
});
// Delete
await User.destroy({
where: { id: 1 }
});
// Find one
const user = await prisma.user.findUnique({
where: { id: 1 }
});
// Create
const user = await prisma.user.create({
data: {
email: 'alice@prisma.io',
name: 'Alice'
}
});
// Update
await prisma.user.update({
where: { id: 1 },
data: { name: 'New name' }
});
// Delete
await prisma.user.delete({
where: { id: 1 }
});
3.3. 更新您的控制器
更新您的 Express 控制器以使用 Prisma Client。例如,以下是如何更新使用者控制器
import { prisma } from '../client'
export class UserController {
async create(req: Request, res: Response) {
const { email, name } = req.body
const result = await prisma.user.create({
data: {
email,
name,
},
})
return res.json(result)
}
}
下一步
現在您已遷移到 Prisma ORM,您可以
- 使用 Prisma 強大的查詢 API 新增更複雜的查詢
- 設定 Prisma Studio 以進行資料庫管理
- 實作資料庫監控
- 使用 Prisma 的測試公用程式新增自動化測試
如需更多資訊和更新