JavaScript 專案中 CockroachDB 的內省
使用 Prisma ORM 內省你的資料庫
在本指南中,我們將使用一個包含三個表的演示 SQL schema
CREATE TABLE "User" (
id INT8 PRIMARY KEY DEFAULT unique_rowid(),
name STRING(255),
email STRING(255) UNIQUE NOT NULL
);
CREATE TABLE "Post" (
id INT8 PRIMARY KEY DEFAULT unique_rowid(),
title STRING(255) UNIQUE NOT NULL,
"createdAt" TIMESTAMP NOT NULL DEFAULT now(),
content STRING,
published BOOLEAN NOT NULL DEFAULT false,
"authorId" INT8 NOT NULL,
FOREIGN KEY ("authorId") REFERENCES "User"(id)
);
CREATE TABLE "Profile" (
id INT8 PRIMARY KEY DEFAULT unique_rowid(),
bio STRING,
"userId" INT8 UNIQUE NOT NULL,
FOREIGN KEY ("userId") REFERENCES "User"(id)
);
注意:某些欄位用雙引號括起來,以確保 CockroachDB 使用正確的字母大小寫。如果未使用雙引號,CockroachDB 會將所有內容讀取為小寫字元。
展開查看錶的圖形概覽
使用者
| 列名 | 型別 | 主鍵 | 外部索引鍵 | 必需 | 預設值 |
|---|---|---|---|---|---|
id | INT8 | 是 | 否 | 是 | 自動遞增 |
name | STRING(255) | 否 | 否 | 否 | - |
email | STRING(255) | 否 | 否 | 是 | - |
帖子
| 列名 | 型別 | 主鍵 | 外部索引鍵 | 必需 | 預設值 |
|---|---|---|---|---|---|
id | INT8 | 是 | 否 | 是 | 自動遞增 |
createdAt | TIMESTAMP | 否 | 否 | 是 | now() |
title | STRING(255) | 否 | 否 | 是 | - |
content | STRING | 否 | 否 | 否 | - |
published | BOOLEAN | 否 | 否 | 是 | false |
authorId | INT8 | 否 | 是 | 是 | - |
個人資料
| 列名 | 型別 | 主鍵 | 外部索引鍵 | 必需 | 預設值 |
|---|---|---|---|---|---|
id | INT8 | 是 | 否 | 是 | 自動遞增 |
bio | STRING | 否 | 否 | 否 | - |
userId | INT8 | 否 | 是 | 是 | - |
接下來,你將內省你的資料庫。內省的結果將是你的 Prisma schema 中的一個資料模型。
執行以下命令來內省你的資料庫
npx prisma db pull
此命令讀取用於在 schema.prisma 中定義 url 的環境變數 DATABASE_URL(在我們的例子中,它設定在 .env 檔案中),並連線到你的資料庫。一旦連線建立,它將內省資料庫(即*讀取資料庫 schema*)。然後,它將資料庫 schema 從 SQL 轉換為 Prisma 資料模型。
內省完成後,你的 Prisma schema 將被更新

資料模型現在看起來類似於這樣
model Post {
id BigInt @id @default(autoincrement())
title String @unique @db.String(255)
createdAt DateTime @default(now()) @db.Timestamp(6)
content String?
published Boolean @default(false)
authorId BigInt
User User @relation(fields: [authorId], references: [id], onDelete: NoAction, onUpdate: NoAction)
}
model Profile {
id BigInt @id @default(autoincrement())
bio String?
userId BigInt @unique
User User @relation(fields: [userId], references: [id], onDelete: NoAction, onUpdate: NoAction)
}
model User {
id BigInt @id @default(autoincrement())
name String? @db.String(255)
email String @unique @db.String(255)
Post Post[]
Profile Profile?
}
Prisma ORM 的資料模型是你資料庫 schema 的宣告性表示,它作為生成的 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 schema 中重新命名它們
model Post {
id BigInt @id @default(autoincrement())
title String @unique @db.String(255)
createdAt DateTime @default(now()) @db.Timestamp(6)
content String?
published Boolean @default(false)
authorId BigInt
author User @relation(fields: [authorId], references: [id], onDelete: NoAction, onUpdate: NoAction)
}
model Profile {
id BigInt @id @default(autoincrement())
bio String?
userId BigInt @unique
user User @relation(fields: [userId], references: [id], onDelete: NoAction, onUpdate: NoAction)
}
model User {
id BigInt @id @default(autoincrement())
name String? @db.String(255)
email String @unique @db.String(255)
posts Post[]
profile Profile?
}
在這個例子中,資料庫 schema 確實遵循了 Prisma ORM 模型的命名約定(只有內省生成的虛擬關係欄位不符合約定,需要調整)。這優化了生成的 Prisma Client API 的人體工程學。
使用自定義模型和欄位名稱
不過,有時你可能希望對在 Prisma Client API 中公開的列和表的名稱進行額外更改。一個常見的例子是將資料庫 schema 中常用的 snake_case 命名法轉換為 PascalCase 和 camelCase 命名法,這對於 JavaScript/TypeScript 開發者來說更自然。
假設你從內省中獲得了以下基於 snake_case 命名法的模型
model my_user {
user_id Int @id @default(sequence())
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(sequence()) @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 頁面瞭解更多資訊。