使用 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: '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.dev.org.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.dev.org.tw/docs/',14 },15 },16 },17 include: {18 posts: true,19 },20})
Prisma Client 確保完全型別安全的資料庫查詢,讓您永遠不會撰寫無效的查詢
Prisma 的內建 dataloader 確保最佳化和高效能的資料庫查詢,即使是 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 團隊互動。