在 JavaScript 專案中對 PlanetScale 進行內省
使用 Prisma ORM 內省您的資料庫
在本指南中,我們將使用一個包含三個表的演示 SQL Schema
CREATE TABLE `Post` (
`id` int NOT NULL AUTO_INCREMENT,
`createdAt` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`updatedAt` datetime(3) NOT NULL,
`title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`content` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`published` tinyint(1) NOT NULL DEFAULT '0',
`authorId` int NOT NULL,
PRIMARY KEY (`id`),
KEY `Post_authorId_idx` (`authorId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `Profile` (
`id` int NOT NULL AUTO_INCREMENT,
`bio` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`userId` int NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `Profile_userId_key` (`userId`),
KEY `Profile_userId_idx` (`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `User` (
`id` int NOT NULL AUTO_INCREMENT,
`email` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`name` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `User_email_key` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
展開查看錶的圖形概述
Post
| 列名 | 型別 | 主鍵 | 外部索引鍵 | 必需 | 預設值 |
|---|---|---|---|---|---|
id | int | ✔️ | 否 | ✔️ | 自增 |
createdAt | datetime(3) | 否 | 否 | ✔️ | now() |
updatedAt | datetime(3) | 否 | 否 | ✔️ | |
title | varchar(255) | 否 | 否 | ✔️ | - |
content | varchar(191) | 否 | 否 | 否 | - |
published | tinyint(1) | 否 | 否 | ✔️ | false |
authorId | int | 否 | 否 | ✔️ | - |
Profile
| 列名 | 型別 | 主鍵 | 外部索引鍵 | 必需 | 預設值 |
|---|---|---|---|---|---|
id | int | ✔️ | 否 | ✔️ | 自增 |
bio | varchar(191) | 否 | 否 | 否 | - |
userId | int | 否 | 否 | ✔️ | - |
User
| 列名 | 型別 | 主鍵 | 外部索引鍵 | 必需 | 預設值 |
|---|---|---|---|---|---|
id | int | ✔️ | 否 | ✔️ | 自增 |
name | varchar(191) | 否 | 否 | 否 | - |
email | varchar(191) | 否 | 否 | ✔️ | - |
下一步,您將內省您的資料庫。內省的結果將是您 Prisma schema 中的一個資料模型。
執行以下命令來內省您的資料庫
npx prisma db pull
此命令讀取在 `.env` 中定義的 `DATABASE_URL` 環境變數並連線到您的資料庫。連線建立後,它會內省資料庫(即_讀取資料庫 schema_)。然後它將 SQL 資料庫 schema 轉換為 Prisma 資料模型。
內省完成後,您的 Prisma schema 將被更新

資料模型現在看起來類似於這樣
model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime
title String @db.VarChar(255)
content String?
published Boolean @default(false)
authorId Int
@@index([authorId])
}
model Profile {
id Int @id @default(autoincrement())
bio String?
userId Int @unique
@@index([userId])
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
}
有關 schema 定義的詳細資訊,請參閱Prisma schema 參考。
Prisma 的資料模型是您的資料庫 schema 的宣告性表示,並作為生成的 Prisma Client 庫的基礎。您的 Prisma Client 例項將暴露_為這些模型量身定製_的查詢。
然後,您需要使用關係欄位新增資料之間任何缺失的關係
model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime
title String @db.VarChar(255)
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
@@index([authorId])
}
model Profile {
id Int @id @default(autoincrement())
bio String?
user User @relation(fields: [userId], references: [id])
userId Int @unique
@@index([userId])
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
profile Profile?
}
在此之後,再次對您的資料庫執行內省
npx prisma db pull
Prisma Migrate 現在將保留手動新增的關係欄位。
因為關係欄位是_虛擬的_(即它們_不會直接體現在資料庫中_),您可以在 Prisma schema 中手動重新命名它們,而無需更改資料庫。
在此示例中,資料庫 schema 遵循 Prisma ORM 模型的命名約定。這優化了生成的 Prisma Client API 的人體工程學。
使用自定義模型和欄位名稱
然而,有時您可能希望對 Prisma Client API 中暴露的列和表的名稱進行額外更改。一個常見的例子是將資料庫 schema 中常用的_蛇形命名法_轉換為 JavaScript/TypeScript 開發者感覺更自然的_帕斯卡命名法_和_駝峰命名法_。
假設您從內省中獲得了以下基於_蛇形命名法_的模型
model my_user {
user_id Int @id @default(autoincrement())
first_name String?
last_name String @unique
}
如果您為該模型生成 Prisma Client API,它將在其 API 中採用_蛇形命名法_
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 頁面上了解更多資訊。