在 TypeScript 專案中對 MySQL 進行內省
使用 Prisma ORM 內省你的資料庫
為了本指南的目的,我們將使用一個包含三張表的演示 SQL 模式
CREATE TABLE User (
id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
name VARCHAR(255),
email VARCHAR(255) UNIQUE NOT NULL
);
CREATE TABLE Post (
id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
title VARCHAR(255) NOT NULL,
createdAt TIMESTAMP NOT NULL DEFAULT now(),
content TEXT,
published BOOLEAN NOT NULL DEFAULT false,
authorId INTEGER NOT NULL,
FOREIGN KEY (authorId) REFERENCES User(id)
);
CREATE TABLE Profile (
id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
bio TEXT,
userId INTEGER UNIQUE NOT NULL,
FOREIGN KEY (userId) REFERENCES User(id)
);
展開以查看錶的圖形概覽
使用者
| 列名 | 型別 | 主鍵 | 外部索引鍵 | 必需 | 預設值 |
|---|---|---|---|---|---|
id | INTEGER | ✔️ | 否 | ✔️ | 自增 |
name | VARCHAR(255) | 否 | 否 | 否 | - |
email | VARCHAR(255) | 否 | 否 | ✔️ | - |
文章
| 列名 | 型別 | 主鍵 | 外部索引鍵 | 必需 | 預設值 |
|---|---|---|---|---|---|
id | INTEGER | ✔️ | 否 | ✔️ | 自增 |
createdAt | DATETIME(3) | 否 | 否 | ✔️ | now() |
title | VARCHAR(255) | 否 | 否 | ✔️ | - |
content | TEXT | 否 | 否 | 否 | - |
published | BOOLEAN | 否 | 否 | ✔️ | false |
authorId | INTEGER | 否 | ✔️ | ✔️ | false |
個人資料
| 列名 | 型別 | 主鍵 | 外部索引鍵 | 必需 | 預設值 |
|---|---|---|---|---|---|
id | INTEGER | ✔️ | 否 | ✔️ | 自增 |
bio | TEXT | 否 | 否 | 否 | - |
userId | INTEGER | 否 | ✔️ | ✔️ | - |
下一步,你將內省你的資料庫。內省的結果將是你的 Prisma 模式中的一個資料模型。
執行以下命令以內省你的資料庫
npx prisma db pull
此命令讀取在 .env 中定義的 DATABASE_URL 環境變數並連線到你的資料庫。連線建立後,它會內省資料庫(即讀取資料庫模式)。然後它將資料庫模式從 SQL 轉換為 Prisma 資料模型。
內省完成後,你的 Prisma 模式將更新

資料模型現在看起來與此類似(請注意,模型上的欄位已重新排序以提高可讀性)
model Post {
id Int @id @default(autoincrement())
title String @db.VarChar(255)
createdAt DateTime @default(now()) @db.Timestamp(0)
content String? @db.Text
published Boolean @default(false)
authorId Int
User User @relation(fields: [authorId], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "Post_ibfk_1")
@@index([authorId], map: "authorId")
}
model Profile {
id Int @id @default(autoincrement())
bio String? @db.Text
userId Int @unique(map: "userId")
User User @relation(fields: [userId], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "Profile_ibfk_1")
}
model User {
id Int @id @default(autoincrement())
name String? @db.VarChar(255)
email String @unique(map: "email") @db.VarChar(255)
Post Post[]
Profile Profile?
}
有關模式定義的詳細資訊,請參閱Prisma 模式參考。
Prisma ORM 的資料模型是你的資料庫模式的宣告性表示,並作為生成的 Prisma Client 庫的基礎。你的 Prisma Client 例項將暴露專為這些模型定製的查詢。
目前,資料模型存在一些小“問題”
User關係欄位是大寫的,因此不符合 Prisma 的命名約定。為了表達更多的“語義”,如果這個欄位被稱為author以更好地描述User和Post之間的關係,那會更好。User上的Post和Profile關係欄位以及Profile上的User關係欄位都是大寫的。為了遵循 Prisma 的命名約定,這兩個欄位都應該小寫為post、profile和user。- 即使小寫後,
User上的post欄位仍然有點命名不當。這是因為它實際上指的是一個帖子列表——因此,一個更好的名稱應該是複數形式:posts。
這些更改與生成的 Prisma Client API 相關,在其中使用小寫的關係欄位 author、posts、profile 和 user 對於 JavaScript/TypeScript 開發者來說會感覺更自然和地道。因此,你可以配置你的 Prisma Client API。
由於關係欄位是虛擬的(即它們不會直接在資料庫中體現),你可以在你的 Prisma 模式中手動重新命名它們,而無需更改資料庫。
model Post {
id Int @id @default(autoincrement())
title String @db.VarChar(255)
createdAt DateTime @default(now()) @db.Timestamp(0)
content String? @db.Text
published Boolean @default(false)
authorId Int
author User @relation(fields: [authorId], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "Post_ibfk_1")
@@index([authorId], map: "authorId")
}
model Profile {
id Int @id @default(autoincrement())
bio String? @db.Text
userId Int @unique(map: "userId")
user User @relation(fields: [userId], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "Profile_ibfk_1")
}
model User {
id Int @id @default(autoincrement())
name String? @db.VarChar(255)
email String @unique(map: "email") @db.VarChar(255)
posts Post[]
profile Profile?
}
在此示例中,資料庫模式確實遵循了 Prisma ORM 模型的命名約定(只有從內省生成的虛擬關係欄位不符合約定,需要調整)。這優化了生成的 Prisma Client API 的人體工程學。
不過,有時你可能希望對 Prisma Client API 中暴露的列和表的名稱進行額外更改。一個常見的例子是將資料庫模式中常用的蛇形命名法 (snake_case) 轉換為對 JavaScript/TypeScript 開發者來說更自然的帕斯卡命名法 (PascalCase) 和駝峰命名法 (camelCase)。
假設你從內省中獲得了以下基於蛇形命名法 (snake_case) 的模型
model my_user {
user_id Int @id @default(autoincrement())
first_name String?
last_name String @unique
}
如果你為此模型生成了 Prisma Client API,它將在其 API 中沿用蛇形命名法 (snake_case)。
const user = await prisma.my_user.create({
data: {
first_name: 'Alice',
last_name: 'Smith',
},
})
如果你不想在 Prisma Client API 中使用資料庫中的表和列名,你可以使用@map 和 @@map 來配置它們。
model MyUser {
userId Int @id @default(autoincrement()) @map("user_id")
firstName String? @map("first_name")
lastName String @unique @map("last_name")
@@map("my_user")
}
透過這種方法,你可以隨意命名你的模型及其欄位,並使用 @map(用於欄位名)和 @@map(用於模型名)來指向底層表和列。你的 Prisma Client API 現在看起來如下:
const user = await prisma.myUser.create({
data: {
firstName: 'Alice',
lastName: 'Smith',
},
})
在配置你的 Prisma Client API 頁面瞭解更多資訊。