如何从 TypeORM 迁移到 Prisma ORM
介绍
¥Introduction
本指南将向你展示如何将应用从 TypeORM 迁移到 Prisma ORM。我们将使用 TypeORM Express 示例 的扩展版本作为 示例项目 来演示迁移步骤。
¥This guide shows you how to migrate your application from TypeORM to Prisma ORM. We'll use an extended version of the TypeORM Express example as a sample project to demonstrate the migration steps.
本迁移指南以 PostgreSQL 为示例数据库,但同样适用于任何其他 Prisma ORM 支持 关系数据库。你可以在 Prisma ORM 与 TypeORM 页面上了解 Prisma ORM 与 TypeORM 的比较。
¥This migration guide uses PostgreSQL as the example database, but it equally applies to any other relational database that's supported by Prisma ORM. You can learn how Prisma ORM compares to TypeORM on the Prisma ORM vs TypeORM page.
先决条件
¥Prerequisites
在开始本指南之前,请确保你已准备好:
¥Before starting this guide, make sure you have:
-
一个要迁移的 TypeORM 项目
¥A TypeORM project you want to migrate
-
已安装 Node.js(版本 16 或更高版本)
¥Node.js installed (version 16 or higher)
-
PostgreSQL 或其他受支持的数据库
¥PostgreSQL or another supported database
-
基本了解 TypeORM 和 Express.js
¥Basic familiarity with TypeORM and Express.js
2. 准备迁移
¥ Prepare for migration
2.1.了解迁移过程
¥2.1. Understand the migration process
无论你构建的是哪种应用或 API 层,从 TypeORM 迁移到 Prisma ORM 的步骤始终相同:
¥The steps for migrating from TypeORM to Prisma ORM are always the same, no matter what kind of application or API layer you're building:
-
安装 Prisma CLI
¥Install the Prisma CLI
-
检查你的数据库
¥Introspect your database
-
创建基线迁移
¥Create a baseline migration
-
安装 Prisma 客户端
¥Install Prisma Client
-
逐步用 Prisma 客户端替换 TypeORM 查询
¥Gradually replace your TypeORM queries with Prisma Client
无论你是在构建 REST API(例如,使用 Express、Koa 或 NestJS)、GraphQL API(例如,使用 Apollo Server、TypeGraphQL 或 Nexus),还是任何其他使用 TypeORM 进行数据库访问的应用,这些步骤都适用。
¥These steps apply whether you're building a REST API (e.g., with Express, Koa, or NestJS), a GraphQL API (e.g., with Apollo Server, TypeGraphQL, or Nexus), or any other kind of application that uses TypeORM for database access.
2.2.设置 Prisma 配置
¥2.2. Set up Prisma configuration
创建一个新的 Prisma 模式文件:
¥Create a new Prisma schema file:
npx prisma init --output ../generated/prisma
使用数据库连接字符串更新 .env
文件中的 DATABASE_URL
:
¥Update the DATABASE_URL
in the .env
file with your database connection string:
DATABASE_URL="postgresql://USER:PASSWORD@HOST:PORT/DATABASE"
3. 迁移数据库模式
¥ Migrate the database schema
3.1.检查你的数据库
¥3.1. Introspect your database
运行 Prisma 的自省功能,从现有数据库创建 Prisma 模式:
¥Run Prisma's introspection to create the Prisma schema from your existing database:
npx prisma db pull
这将创建一个包含数据库架构的 schema.prisma
文件。
¥This will create a schema.prisma
file with your database schema.
3.2.创建基线迁移
¥3.2. Create a baseline migration
创建并应用基线迁移文件以标记数据库的当前状态:
¥Create and apply a baseline migration to mark the current state of your database:
npx prisma migrate diff --from-empty --to-schema-datamodel prisma/schema.prisma --script > baseline.sql
npx prisma migrate resolve --applied "baseline"
4. 更新你的应用代码
¥ Update your application code
4.1.安装 Prisma 客户端
¥4.1. Install Prisma Client
安装 Prisma 客户端包:
¥Install the Prisma Client package:
npm install @prisma/client
生成 Prisma 客户端:
¥Generate Prisma Client:
npx prisma generate
4.2.替换 TypeORM 查询
¥4.2. Replace TypeORM queries
开始使用 Prisma 客户端替换你的 TypeORM 查询。以下是如何转换一些常见查询的示例:
¥Start replacing your TypeORM queries with Prisma Client. Here's an example of how to convert some common queries:
- TypeORM
- Prisma Client
// Find one
const user = await userRepository.findOne({
where: { id: 1 }
});
// Create
const user = await userRepository.save({
email: 'alice@prisma.io',
name: 'Alice'
});
// Update
await userRepository.update(1, {
name: 'New name'
});
// Delete
await userRepository.delete(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 }
});
4.3.更新你的控制器
¥4.3. Update your controllers
更新 Express 控制器以使用 Prisma 客户端。例如,以下是如何更新 CreateUserAction
:
¥Update your Express controllers to use Prisma Client. For example, here's how to update the CreateUserAction
:
import { prisma } from '../client'
export class CreateUserAction {
async run(req: Request, res: Response) {
const { email, name } = req.body
const result = await prisma.user.create({
data: {
email,
name,
},
})
return res.json(result)
}
}
5. 测试和部署
¥ Test and deploy
5.1.测试你的更改
¥5.1. Test your changes
测试所有迁移的端点,以确保它们按预期工作:
¥Test all migrated endpoints to ensure they work as expected:
npm test
5.2.部署你的更改
¥5.2. Deploy your changes
-
部署你的架构更改:
¥Deploy your schema changes:
npx prisma migrate deploy
-
使用更新的依赖部署你的应用代码。
¥Deploy your application code with the updated dependencies.
下一步
¥Next steps
现在你已经迁移到 Prisma ORM,你可以:
¥Now that you've migrated to Prisma ORM, you can:
-
使用 Prisma 强大的查询 API 添加更复杂的查询
¥Add more complex queries using Prisma's powerful query API
-
设置 Prisma Studio 进行数据库管理
¥Set up Prisma Studio for database management
-
实现数据库监控
¥Implement database monitoring
-
使用 Prisma 的测试实用程序
¥Add automated tests using Prisma's testing utilities
更多信息:
¥For more information:
Stay connected with Prisma
Continue your Prisma journey by connecting with our active community. Stay informed, get involved, and collaborate with other developers:
- Follow us on X for announcements, live events and useful tips.
- Join our Discord to ask questions, talk to the community, and get active support through conversations.
- Subscribe on YouTube for tutorials, demos, and streams.
- Engage on GitHub by starring the repository, reporting issues, or contributing to an issue.