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

使用 Prisma 从 MySQL、PostgreSQL 和 SQL Server 数据库查询数据——Prisma 是一个用于 Node.js 的类型安全的 TypeScript ORM

tech

什么是 Prisma?

Prisma 提供了数据库工具,这些工具是使用出色的 DX 构建高性能 Next.js 应用程序的完美伴侣。

ORM

Prisma ORM 是最受欢迎的 TypeScript ORM。它带有一个人类可读的模式、自动化迁移和一个直观的、完全类型安全的查询 API。

了解有关 Prisma ORM 的更多信息

Postgres

Prisma Postgres 是第一个没有冷启动的无服务器数据库。它基于unikernels,在裸机上运行,并带有一个内置缓存、高性能查询和无缝扩展——所有这些都具有出色的 DX。

了解有关 Prisma Postgres 的更多信息

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 模式映射到数据库,这样您就不需要编写 SQL 来管理您的数据库模式。

筛选、分页和排序

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 技术的可运行示例项目

© . This site is unofficial and not affiliated with Prisma Data, Inc.