使用 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,它通过从数据库模式自动生成类型,以零成本为您提供类型安全的好处。 它非常适合构建可靠的数据密集型应用程序。当您将数据存储在关系数据库中时,Prisma 让您更加自信和高效。您可以将其与任何 Node.js 服务器框架一起使用,以与您的数据库进行交互。
Prisma 模式 使用 Prisma 的建模语言来定义您的数据库模式并生成相应的 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 模式生成的类型确保您的所有数据库查询都是类型安全的。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}
定义一次您的模式,Prisma 将为您生成 TypeScript 类型。无需手动同步数据库模式和应用程序代码中的类型。
下面的代码演示了 Prisma 的数据库查询如何完全类型安全 – 适用于所有查询,包括部分查询和关系。
Prisma Client 的查询始终具有推断的返回类型,即使在您获取关系时,也易于推断返回的数据。
1// Inferred type:2// User & {3// posts: Post[];4// }5const user = await prisma.user.create({6 data: {7 email: '[email protected]',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: '[email protected]',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 模式映射到数据库,这样您就不需要编写 SQL 来管理您的数据库模式。
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 柏林聚会始于 2019 年,是世界上最受欢迎的 TypeScript 聚会之一
使用 Prisma、TypeScript 和各种不同框架和 API 技术的即用型示例项目
一个使用 hapi 和 Prisma 构建现代后端的教程系列
我们有多个渠道,您可以在其中与我们社区的成员以及 Prisma 团队互动。