TypeScript ORM
为您的数据库提供零成本类型安全

使用 Prisma 查询 MySQL、PostgreSQL 和 SQL Server 数据库 – 面向 Node.js 的类型安全 TypeScript ORM

tech

什么是 Prisma?

Prisma 让处理数据变得容易!它提供了类型安全的 Node.js 和 TypeScript ORM、全局数据库缓存、连接池以及实时数据库事件。

查询
// Creating a new record
await prisma.user.create({
firstName: “Alice”,
email: “alice@prisma.io”
})
表格
id firstName email
1 Bobby bobby@tables.io
2 Nilufar nilu@email.com
3 Jürgen jums@dums.edu
4 Alice alice@prisma.io

Prisma 和 TypeScript 如何协同工作

TypeScript 是一种静态类型语言,它基于 JavaScript 构建。它为您提供了 JavaScript 的所有功能,此外还能够对代码进行类型化和验证,从而在您运行代码之前捕获错误并提供修复,从而节省时间。所有有效的 JavaScript 代码也是 TypeScript 代码,这使得您可以轻松采用 TypeScript。

Prisma 是一个面向 Node.js 和 TypeScript 的 ORM,通过从您的数据库 schema 自动生成类型,为您带来零成本类型安全的好处。 它是构建可靠的数据密集型应用程序的理想选择。在将数据存储到关系型数据库时,Prisma 让您更加自信和高效。您可以将其与任何 Node.js 服务器框架一起使用来与您的数据库交互。

Prisma Schema

Prisma schema 使用 Prisma 的建模语言来定义您的数据库 schema 并 生成相应的 TypeScript 类型。它使得数据建模变得简单直观,尤其是在建模 关系 时。

1// Define the `User` table in the database
2model User {
3 id String @id @default(cuid())
4 email String @unique
5 password String
6 name String?
7 posts Post[]
8}
9
10// Define the `Post` table in the database
11model Post {
12 id String @id @default(cuid())
13 createdAt DateTime @default(now())
14 title String
15 content String?
16 authorId String
17 author User @relation(fields: [authorId], references: [id])
18}

从 Prisma schema 生成的类型确保您的所有数据库查询都是类型安全的。Prisma Client 为您提供出色的自动补全体验,让您能够快速前进并确保不会编写无效查询。

1type User = {
2 id: string
3 email: string
4 password: string
5 name: string | null
6}
7
8export type Post = {
9 id: string
10 createdAt: Date
11 title: string
12 content: string | null
13 authorId: string
14}

Prisma 和 TypeScript 代码示例

只需定义一次 schema,Prisma 就会为您生成 TypeScript 类型。无需在数据库 schema 和应用程序代码中的类型之间手动同步。

下面的代码演示了使用 Prisma 的数据库查询如何完全类型安全 – 适用于所有查询,包括部分查询和关系。

类型安全数据库查询

零成本类型安全

使用 Prisma Client 的查询总是可以推断出其返回类型,即使在获取关系时,也能轻松地推断返回数据的形状。

1// Inferred type:
2// User & {
3// posts: Post[];
4// }
5const user = await prisma.user.create({
6 data: {
7 email: 'alice@prisma.io',
8 password: '0ee4808f893b8e05bdd251048d5c4c8af8bb89403676dda95619841a481f8e87',
9 name: 'Alice',
10 posts: {
11 create: {
12 title: 'Learn how to use Prisma with TypeScript',
13 content: 'https://prisma.org.cn/docs/',
14 },
15 },
16 },
17 include: {
18 posts: true,
19 },
20})
生成的类型

零成本类型安全

使用 Prisma Client 的查询总是可以推断出其返回类型,即使在获取关系时,也能轻松地推断返回数据的形状。

1// Inferred type:
2// User & {
3// posts: Post[];
4// }
5const user = await prisma.user.create({
6 data: {
7 email: 'alice@prisma.io',
8 password: '0ee4808f893b8e05bdd251048d5c4c8af8bb89403676dda95619841a481f8e87',
9 name: 'Alice',
10 posts: {
11 create: {
12 title: 'Learn how to use Prisma with TypeScript',
13 content: 'https://prisma.org.cn/docs/',
14 },
15 },
16 },
17 include: {
18 posts: true,
19 },
20})

为什么选择 Prisma 和 TypeScript?

类型安全数据库客户端

Prisma Client 确保完全类型安全的数据库查询,让您永远不会编写无效查询

优化的数据库查询

Prisma 内置的数据加载器确保优化的、高性能的数据库查询,即使对于 N+1 查询也是如此。

自动补全

Prisma 在您编写查询时提供丰富的自动补全功能,帮助您编写查询

类型推断

使用 Prisma Client 的查询总是可以推断出其返回类型,让您轻松地推断数据的形状。

轻松的数据库迁移

将您的 Prisma schema 映射到数据库,这样您就不需要编写 SQL 来管理数据库 schema。

筛选、分页和排序

Prisma Client 通过为常见的数据库功能提供便利的 API 来减少样板代码。

index.tsx
1const users = await prisma.user.findMany({
2 where: {
3 email: {
4 endsWith: '@prisma.io',
5 }
6 },
7 include: {
8 p
9 }
10})
 
postsPost[]

我们 ❤️ TypeScript

在 Prisma,我们热爱 TypeScript,并相信其光明的前景。自 Prisma 诞生以来,我们一直将 TypeScript 编译器推向极限 以提供前所未有的类型安全数据库访问和丰富的自动补全,从而带来愉悦的开发者体验。

Prisma 的核心是类型安全,这样您可以减少错误。通过利用 TypeScript 的结构化类型系统,Prisma 将数据库查询映射到 结构化类型 因此您可以精确地知道您编写的每个 Prisma Client 查询返回的数据形状。

我们的 TypeScript 资源

TypeScript Berlin Meetup

TypeScript Berlin Meetup 始于 2019 年,是全球最受欢迎的 TypeScript Meetup 之一

Prisma TypeScript 示例

使用 Prisma、TypeScript 以及各种不同框架和 API 技术的可直接运行的示例项目

使用 TypeScript、PostgreSQL 和 Prisma 构建现代后端

使用 hapi 和 Prisma 构建现代后端的系列教程