使用 Prisma(一個適用於 Node.js 的型別安全的 TypeScript ORM)查詢 MySQL、PostgreSQL 和 SQL Server 資料庫中的資料
Prisma 提供資料庫工具,是構建高效能 Next.js 應用並獲得卓越開發體驗的完美伴侶。
Prisma Postgres 是第一個無冷啟動的無伺服器資料庫。它基於unikernels,裸機執行,並內建快取、高效能查詢和無縫擴充套件——所有這些都帶來了卓越的開發體驗。
瞭解更多關於 Prisma Postgres 的資訊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 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.tw/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.tw/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 柏林聚會始於 2019 年,是世界上最受歡迎的 TypeScript 聚會之一
可直接執行的示例專案,使用 Prisma、TypeScript 以及各種不同的框架和 API 技術
一個使用 hapi 和 Prisma 構建現代後端系列的教程
我們有多個渠道,您可以與社群成員以及 Prisma 團隊互動。