使用 Prisma 查询 MySQL、PostgreSQL 和 SQL Server 数据库 – 面向 Node.js 的类型安全 TypeScript ORM
// Creating a new recordawait prisma.user.create({firstName: “Alice”,email: “alice@prisma.io”})
id firstName email1 Bobby bobby@tables.io2 Nilufar nilu@email.com3 Jürgen jums@dums.edu4 Alice alice@prisma.io
TypeScript 是一种静态类型语言,它基于 JavaScript 构建。它为您提供了 JavaScript 的所有功能,此外还能够对代码进行类型化和验证,从而在您运行代码之前捕获错误并提供修复,从而节省时间。所有有效的 JavaScript 代码也是 TypeScript 代码,这使得您可以轻松采用 TypeScript。
Prisma 是一个面向 Node.js 和 TypeScript 的 ORM,通过从您的数据库 schema 自动生成类型,为您带来零成本类型安全的好处。 它是构建可靠的数据密集型应用程序的理想选择。在将数据存储到关系型数据库时,Prisma 让您更加自信和高效。您可以将其与任何 Node.js 服务器框架一起使用来与您的数据库交互。
Prisma schema 使用 Prisma 的建模语言来定义您的数据库 schema 并 生成相应的 TypeScript 类型。它使得数据建模变得简单直观,尤其是在建模 关系 时。
1// Define the `User` table in the database2model User {3 id String @id @default(cuid())4 email String @unique5 password String6 name String?7 posts Post[]8}910// Define the `Post` table in the database11model Post {12 id String @id @default(cuid())13 createdAt DateTime @default(now())14 title String15 content String?16 authorId String17 author User @relation(fields: [authorId], references: [id])18}
从 Prisma schema 生成的类型确保您的所有数据库查询都是类型安全的。Prisma Client 为您提供出色的自动补全体验,让您能够快速前进并确保不会编写无效查询。
1type User = {2 id: string3 email: string4 password: string5 name: string | null6}78export type Post = {9 id: string10 createdAt: Date11 title: string12 content: string | null13 authorId: string14}
只需定义一次 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 Client 确保完全类型安全的数据库查询,让您永远不会编写无效查询
Prisma 内置的数据加载器确保优化的、高性能的数据库查询,即使对于 N+1 查询也是如此。
Prisma 在您编写查询时提供丰富的自动补全功能,帮助您编写查询
使用 Prisma Client 的查询总是可以推断出其返回类型,让您轻松地推断数据的形状。
将您的 Prisma schema 映射到数据库,这样您就不需要编写 SQL 来管理数据库 schema。
Prisma Client 通过为常见的数据库功能提供便利的 API 来减少样板代码。
1const users = await prisma.user.findMany({2 where: {3 email: {4 endsWith: '@prisma.io',5 }6 },7 include: {8 p9 }10})
在 Prisma,我们热爱 TypeScript,并相信其光明的前景。自 Prisma 诞生以来,我们一直将 TypeScript 编译器推向极限 以提供前所未有的类型安全数据库访问和丰富的自动补全,从而带来愉悦的开发者体验。
Prisma 的核心是类型安全,这样您可以减少错误。通过利用 TypeScript 的结构化类型系统,Prisma 将数据库查询映射到 结构化类型 因此您可以精确地知道您编写的每个 Prisma Client 查询返回的数据形状。
TypeScript Berlin Meetup 始于 2019 年,是全球最受欢迎的 TypeScript Meetup 之一
使用 Prisma、TypeScript 以及各种不同框架和 API 技术的可直接运行的示例项目
使用 hapi 和 Prisma 构建现代后端的系列教程
我们有多个渠道,您可以与我们的社区成员以及 Prisma 团队互动。