使用 Prisma 從 MySQL、PostgreSQL 與 SQL Server 資料庫查詢資料 – 這是適用於 Node.js 的型別安全 TypeScript ORM
Prisma 提供的資料庫工具是構建高效能 Next.js 應用程式的最佳夥伴,並具備絕佳的開發者體驗(DX)。
Prisma ORM 是 最受歡迎的 TypeScript ORM。它具備易於閱讀的 Schema(架構)、自動化遷移功能,以及直觀且完全型別安全的查詢 API。
深入了解 Prisma ORM→Prisma Postgres 是第一個 無冷啟動問題的無伺服器資料庫。它基於 unikernels 技術,運行於裸機之上,並內建快取、高效能查詢和無縫擴展功能 —— 同時提供卓越的開發者體驗。
深入了解 Prisma Postgres→TypeScript 是一種建立在 JavaScript 之上的靜態型別語言。它為您提供了 JavaScript 的所有功能,並具備額外的型別定義與程式碼驗證能力,能在執行程式碼前捕捉錯誤並提供修復建議,進而節省您的開發時間。所有有效的 JavaScript 程式碼也都是 TypeScript 程式碼,這使得您能輕鬆採用 TypeScript。
Prisma 是一個適用於 Node.js 和 TypeScript 的 ORM,它透過從您的資料庫架構(Schema)自動產生型別,以零成本為您帶來型別安全的好處。它是建構可靠、資料密集型應用程式的理想選擇。Prisma 讓您在關聯式資料庫中儲存資料時,能更加自信且具備生產力。您可以將其與任何 Node.js 伺服器框架搭配使用,以與您的資料庫進行互動。
Prisma schema 使用 Prisma 的建模語言來定義您的資料庫架構,並產生對應的 TypeScript 型別。它使資料建模變得簡單且直觀,特別是在處理關聯(relations)建模時。
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 型別。無需在資料庫架構與應用程式程式碼中的型別之間進行手動同步。
下方的程式碼展示了使用 Prisma 進行資料庫查詢時,如何達到完全的型別安全——適用於所有查詢,包括部分查詢(partial queries)與關聯查詢。
使用 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 內建的 dataloader 可確保資料庫查詢經過優化且高效能,即使處理 N+1 查詢問題也不在話下。
Prisma 在您撰寫查詢時提供豐富的自動完成功能,協助您編寫查詢語句。
使用 Prisma Client 進行的查詢,其回傳型別總是被推斷出來的,讓您能輕鬆理解您的資料。
將 Prisma schema 對應至資料庫,您無需手動撰寫 SQL 即可管理資料庫結構。
Prisma Client 為常見的資料庫功能提供了便捷的 API,大幅減少了樣板程式碼(boilerplate)。
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 聚會之一。
可直接運行的範例專案,使用了 Prisma、TypeScript 以及各種不同的框架與 API 技術。
一系列使用 hapi 與 Prisma 建構現代化後端的教學系列文章。
我們擁有多個頻道,您可以藉此與社群成員及 Prisma 團隊互動。