Skip to main content

你应该使用 Prisma ORM 吗?

Prisma ORM 是一种新型 ORM, - 像任何其他工具一样 - 有它自己的权衡。本页解释了 Prisma ORM 何时适合,并提供了其他场景的替代方案。

¥Prisma ORM is a new kind of ORM that - like any other tool - comes with its own tradeoffs. This page explains when Prisma ORM would be a good fit, and provides alternatives for other scenarios.

Prisma ORM 可能非常适合你,如果......

¥Prisma ORM likely is a good fit for you if ...

...你正在构建一个与数据库对话的服务器端应用

¥... you are building a server-side application that talks to a database

这是 Prisma ORM 的主要用例。服务器端应用通常是 API 服务器,通过 REST、GraphQL 或 gRPC 等技术公开数据操作。它们通常构建为微服务或整体应用,并通过长期运行的服务器或无服务器功能进行部署。Prisma ORM 非常适合所有这些应用和部署模型。

¥This is the main use case for Prisma ORM. Server-side applications typically are API servers that expose data operations via technologies like REST, GraphQL or gRPC. They are commonly built as microservices or monolithic apps and deployed via long-running servers or serverless functions. Prisma ORM is a great fit for all of these application and deployment models.

请参阅 Prisma ORM supports 的数据库(关系型、NoSQL 和 NewSQL)的完整列表。

¥Refer to the full list of databases (relational, NoSQL, and NewSQL) that Prisma ORM supports.

...你关心生产力和开发者体验

¥... you care about productivity and developer experience

生产力和开发者经验是我们构建工具的核心。我们希望为手动执行时复杂、容易出错且耗时的任务构建开发者友好的抽象。

¥Productivity and developer experience are core to how we're building our tools. We're looking to build developer-friendly abstractions for tasks that are complex, error-prone and time-consuming when performed manually.

无论你是 SQL 新手还是老手,Prisma ORM 都会为你最常见的数据库工作流程带来显着的生产力提升。

¥No matter if you're a SQL newcomer or veteran, Prisma ORM will give you a significant productivity boost for the most common database workflows.

以下是我们在设计和构建工具时应用的一些指导原则和一般实践:

¥Here are a couple of the guiding principles and general practices we apply when designing and building our tools:

...你在一个团队中工作

¥... you are working in a team

Prisma ORM 在协作环境中使用时尤其出色。

¥Prisma ORM shines especially when used in collaborative environments.

声明性 Prisma 架构 提供了数据库当前状态的概述,每个人都易于理解。这是对传统工作流程的重大改进,在传统工作流程中,开发者必须深入挖掘迁移文件才能了解当前的表结构。

¥The declarative Prisma schema provides an overview of the current state of the database that's easy to understand for everyone. This is a major improvement to traditional workflows where developers have to dig through migration files to understand the current table structure.

Prisma 客户端 的最小 API 界面使开发者能够快速掌握它,而无需太多学习开销,因此新开发者加入团队变得更加顺利。

¥Prisma Client's minimal API surface enables developers to pick it up quickly without much learning overhead, so onboarding new developers to a team becomes a lot smoother.

Prisma 迁移 工作流程的设计方式涵盖了协作环境中的数据库模式更改。从最初的架构创建到将架构更改部署到生产并解决并行修改引入的冲突,Prisma Migrate 都能满足你的需求。

¥The Prisma Migrate workflows are designed in a way to cover database schema changes in collaborative environments. From the initial schema creation up to the point of deploying schema changes to production and resolving conflicts that were introduced by parallel modifications, Prisma Migrate has you covered.

...你需要一个全面涵盖你的数据库工作流程的工具

¥... you want a tool that holistically covers your database workflows

Prisma ORM 比 "只是另一个 ORM" 更强大。我们正在构建一个数据库工具包,涵盖与数据库交互的应用开发者的日常工作流程。一些例子是:

¥Prisma ORM is a lot more than "just another ORM". We are building a database toolkit that covers the daily workflows of application developers that interact with databases. A few examples are:

...你重视类型安全

¥... you value type-safety

Prisma ORM 是 TypeScript 生态系统中唯一完全类型安全的 ORM。生成的 Prisma 客户端即使对于部分查询和关系也能确保类型化的查询结果。你可以在 与 TypeORM 的类型安全比较 中了解更多相关信息。

¥Prisma ORM is the only fully type-safe ORM in the TypeScript ecosystem. The generated Prisma Client ensures typed query results even for partial queries and relations. You can learn more about this in the type-safety comparison with TypeORM.

...你想要编写原始、类型安全的 SQL

¥... you want to write raw, type-safe SQL

除了直观的高级查询 API 之外,Prisma ORM 还为你提供了一种 编写具有完全类型安全性的原始 SQL 的方法。

¥In addition to the intuitive, higher-level query API, Prisma ORM also offers a way for you to write raw SQL with full type safety.

...你想要一个具有透明开发流程、适当维护和支持的 ORM

¥... you want an ORM with a transparent development process, proper maintenance & support

Prisma ORM 的开源工具的开发是公开进行的。大部分内容直接发生在 GitHub 上的 prisma/prisma 主存储库中:

¥Development of Prisma ORM's open source tools is happening in the open. Most of it happens directly on GitHub in the main prisma/prisma repo:

  • 我们存储库中的问题和 PR 会被分类并确定优先级(通常在 1-2 天内)

    ¥issues and PRs in our repos are triaged and prioritized (usually within 1-2 days)

  • 具有新功能和改进的新 releases 每三周发布一次

    ¥new releases with new features and improvements are issued every three weeks

  • 我们有专门的支持团队来回答 GitHub 讨论 中的问题

    ¥we have a dedicated support team that responds to questions in GitHub Discussions

...你想成为一个很棒的社区的一部分

¥... you want to be part of an awesome community

Prisma 有一个活泼的 community,你可以在 Discord 上找到它。我们还定期举行聚会、会议和其他以开发者为中心的活动。加入我们!

¥Prisma has a lively community that you can find on Discord. We also regularly host Meetups, conferences and other developer-focused events. Join us!

Prisma ORM 可能不太适合你,如果......

¥Prisma ORM likely is not a good fit for you if ...

...你需要完全控制所有数据库查询

¥... you need full control over all database queries

Prisma ORM 是一个抽象。因此,Prisma ORM 的固有权衡是减少控制量以换取更高的生产率。这意味着,在某些情况下,Prisma 客户端 API 的功能可能比普通 SQL 的功能要少。

¥Prisma ORM is an abstraction. As such, an inherent tradeoff of Prisma ORM is a reduced amount of control in exchange for higher productivity. This means, the Prisma Client API might have less capabilities in some scenarios than you get with plain SQL.

如果你的应用对 Prisma ORM 不提供的数据库查询有要求,并且解决方法成本太高,那么你可能最好使用一个允许你使用纯 SQL 完全控制数据库操作的工具。

¥If your application has requirements for database queries that Prisma ORM does not provide and the workarounds are too costly, you might be better off with a tool that allows you to exercise full control over your database operations using plain SQL.

注意:如果你可以解决某些限制,但仍然希望看到 Prisma ORM 处理这种情况的方式有所改进,我们鼓励你在 GitHub 上创建 功能要求,以便我们的产品和工程团队可以研究它。

¥Note: If you can work around a certain limitation but still would like to see an improvement in the way how Prisma ORM handles the situation, we encourage you to create a feature request on GitHub so that our Product and Engineering teams can look into it.

备择方案:SQL 驱动程序(例如 node-postgresmysqlsqlite3...)

¥Alternatives: SQL drivers (e.g. node-postgres, mysql, sqlite3, ...)

...你不想为后端编写任何代码

¥... you do not want to write any code for your backend

如果你不想为后端编写任何代码,而只想生成开箱即用的 API 服务器和数据库,那么你可能更愿意为你的项目选择后端即服务 (BaaS)。

¥If you don't want to write any code for your backend and just be able to generate your API server and the database out-of-the-box, you might rather choose a Backend-as-a-Service (BaaS) for your project.

使用 BaaS,你通常可以通过高级 API(例如 GraphQL SDL)或可视化编辑器来配置数据模型。基于此数据模型,BaaS 生成 CRUD API 并为你提供数据库。通过此设置,你通常无法控制 API 服务器和数据库运行的基础设施。

¥With a BaaS, you can typically configure your data model via a high-level API (e.g. GraphQL SDL) or a visual editor. Based on this data model, the BaaS generates a CRUD API and provisions a database for you. With this setup, you typically don't have control over the infrastructure the API server and database are running on.

借助 Prisma ORM,你可以使用 Node.js 或 TypeScript 自行构建后端。这意味着与使用 BaaS 相比,你必须做更多的编码工作。这种方法的好处是,你可以完全灵活地构建、部署、扩展和维护后端,并且堆栈的关键部分不依赖于第三方软件。

¥With Prisma ORM, you are building the backend yourself using Node.js or TypeScript. This means you'll have to do a lot more coding work compared to using a BaaS. The benefit of this approach is that you have full flexibility for building, deploying, scaling and maintaining your backend and are not dependent on 3rd party software for a crucial part of your stack.

备择方案:AWS 应用同步8base恩斯特SupabaseFirebase扩增

¥Alternatives: AWS AppSync, 8base, Nhost, Supabase, Firebase, Amplication

...你想要一个 CRUD GraphQL API,而无需编写任何代码

¥... you want a CRUD GraphQL API without writing any code

虽然 nexus-plugin-prismatypegraphql-prisma 等工具允许你在 GraphQL API 中为 Prisma ORM 模型快速生成 CRUD 操作,但这些方法仍然需要你手动设置 GraphQL 服务器并做一些工作来公开定义的模型的 GraphQL 查询和突变 在你的 Prisma 架构中。

¥While tools like the nexus-plugin-prisma and typegraphql-prisma allow you to quickly generate CRUD operations for your Prisma ORM models in a GraphQL API, these approaches still require you to set up your GraphQL server manually and do some work to expose GraphQL queries and mutations for the models defined in your Prisma schema.

如果你想为数据库获取开箱即用的 GraphQL 端点,其他工具可能更适合你的用例。

¥If you want to get a GraphQL endpoint for your database out-of-the box, other tools might be better suited for your use case.

备择方案:Hasura后图形

¥Alternatives: Hasura, Postgraphile