TypeScript ORM
為您的資料庫提供零成本型別安全

使用 Prisma(一個適用於 Node.js 的型別安全的 TypeScript ORM)查詢 MySQL、PostgreSQL 和 SQL Server 資料庫中的資料

tech

什麼是 Prisma?

Prisma 提供資料庫工具,是構建高效能 Next.js 應用並獲得卓越開發體驗的完美伴侶。

ORM

Prisma ORM 是最受歡迎的 TypeScript ORM。它具有人類可讀的模式、自動化遷移和一個直觀、完全型別安全的查詢 API。

瞭解更多關於 Prisma ORM 的資訊

Postgres

Prisma Postgres 是第一個無冷啟動的無伺服器資料庫。它基於unikernels,裸機執行,並內建快取、高效能查詢和無縫擴充套件——所有這些都帶來了卓越的開發體驗。

瞭解更多關於 Prisma Postgres 的資訊

Prisma 和 TypeScript 如何協同工作

TypeScript 是一種基於 JavaScript 構建的靜態型別語言。它提供 JavaScript 的所有功能,並額外增加了程式碼型別化和驗證的能力,透過在執行程式碼之前捕獲錯誤和提供修復來節省您的時間。所有有效的 JavaScript 程式碼也都是 TypeScript 程式碼,這使得 TypeScript 易於您採用。

Prisma 是一個適用於 Node.js 和 TypeScript 的 ORM,透過從您的資料庫 schema 自動生成型別,提供零成本的型別安全優勢。 它非常適合構建可靠的資料密集型應用程式。Prisma 讓您在關係型資料庫中儲存資料時更有信心、更高效。您可以將其與任何 Node.js 伺服器框架一起使用來與資料庫互動。

Prisma Schema

從 Prisma schema 生成的型別確保您的所有資料庫查詢都是型別安全的。Prisma schema 使用 Prisma 的建模語言來定義您的資料庫 schema,並生成相應的 TypeScript 型別。它使資料建模變得簡單直觀,尤其是在建模關係方面。

1// Define the `User` table in the database
2model User {
3 id String @id @default(cuid())
4 email String @unique
5 password String
6 name String?
7 posts Post[]
8}
9
10// Define the `Post` table in the database
11model Post {
12 id String @id @default(cuid())
13 createdAt DateTime @default(now())
14 title String
15 content String?
16 authorId String
17 author User @relation(fields: [authorId], references: [id])
18}

從 Prisma schema 生成的型別確保您的所有資料庫查詢都是型別安全的。Prisma Client 提供出色的自動補全體驗,讓您可以快速開發並確保不會編寫無效查詢。

1type User = {
2 id: string
3 email: string
4 password: string
5 name: string | null
6}
7
8export type Post = {
9 id: string
10 createdAt: Date
11 title: string
12 content: string | null
13 authorId: string
14}

Prisma 和 TypeScript 程式碼示例

只需定義一次您的 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 和 TypeScript?

型別安全的資料庫客戶端

Prisma Client 確保完全型別安全的資料庫查詢,讓您絕不會編寫無效查詢

最佳化的資料庫查詢

Prisma 內建的資料載入器確保最佳化的、高效能的資料庫查詢,即使是 N+1 查詢也不例外。

自動補全

Prisma 在您編寫查詢時提供豐富的自動補全功能,助您編寫查詢

型別推斷

Prisma Client 的查詢始終推斷其返回型別,從而更容易理解您的資料。

輕鬆的資料庫遷移

將您的 Prisma schema 對映到資料庫,這樣您就不需要編寫 SQL 來管理您的資料庫 schema。

篩選、分頁和排序

Prisma Client 透過提供常用資料庫功能的便捷 API 來減少樣板程式碼。

index.tsx
1const users = await prisma.user.findMany({
2 where: {
3 email: {
4 endsWith: '@prisma.io',
5 }
6 },
7 include: {
8 p
9 }
10})
 
postsPost[]

我們 ❤️ TypeScript

在 Prisma,我們熱愛 TypeScript 並堅信其美好的未來。自 Prisma 誕生以來,我們一直將 TypeScript 編譯器推向極限 以提供前所未有的型別安全資料庫訪問和豐富的自動補全功能,帶來愉悅的開發體驗。

Prisma 的核心是型別安全,讓您少犯錯誤。透過利用 TypeScript 的結構化型別系統,Prisma 將資料庫查詢對映到 結構化型別 因此您可以瞭解您編寫的每個 Prisma Client 查詢返回資料的精確形狀。

我們的 TypeScript 資源

TypeScript 柏林聚會

TypeScript 柏林聚會始於 2019 年,是世界上最受歡迎的 TypeScript 聚會之一

Prisma TypeScript 示例

可直接執行的示例專案,使用 Prisma、TypeScript 以及各種不同的框架和 API 技術

使用 TypeScript、PostgreSQL 和 Prisma 構建現代後端

一個使用 hapi 和 Prisma 構建現代後端系列的教程

© . This site is unofficial and not affiliated with Prisma Data, Inc.