内省
使用 Prisma ORM 内省您的数据库
为了本指南的目的,我们将使用一个包含三个表的演示 SQL 模式
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 将会把所有内容都读取为小写字符。
展开查看表的图形概述
User
列名 | 类型 | 主键 | 外键 | 必需 | 默认值 |
---|---|---|---|---|---|
id | INT8 | ✔️ | 否 | ✔️ | 自增 |
name | STRING(255) | 否 | 否 | 否 | - |
email | STRING(255) | 否 | 否 | ✔️ | - |
Post
列名 | 类型 | 主键 | 外键 | 必需 | 默认值 |
---|---|---|---|---|---|
id | INT8 | ✔️ | 否 | ✔️ | 自增 |
createdAt | TIMESTAMP | 否 | 否 | ✔️ | now() |
title | STRING(255) | 否 | 否 | ✔️ | - |
content | STRING | 否 | 否 | 否 | - |
published | BOOLEAN | 否 | 否 | ✔️ | false |
authorId | INT8 | 否 | ✔️ | ✔️ | - |
Profile
列名 | 类型 | 主键 | 外键 | 必需 | 默认值 |
---|---|---|---|---|---|
id | INT8 | ✔️ | 否 | ✔️ | 自增 |
bio | STRING | 否 | 否 | 否 | - |
userId | INT8 | 否 | ✔️ | ✔️ | - |
接下来,您将内省您的数据库。内省的结果将是您的 Prisma 模式中的数据模型。
运行以下命令内省您的数据库
npx prisma db pull
此命令会读取用于在 schema.prisma
中定义 url
的环境变量 DATABASE_URL
(在本例中,该变量设置在 .env
文件中),并连接到您的数据库。连接建立后,它会内省数据库(即读取数据库模式),然后将数据库模式从 SQL 转换为 Prisma 数据模型。
内省完成后,您的 Prisma 模式将更新
数据模型现在看起来类似于这样
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 的数据模型是数据库模式的声明性表示,并作为生成的 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 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?
}
在此示例中,数据库模式遵循了 Prisma ORM 模型的命名约定(只有通过内省生成的虚拟关联字段不符合要求,需要调整)。这优化了生成的 Prisma Client API 的人体工程学。
使用自定义模型和字段名称
但有时,您可能希望对 Prisma Client API 中暴露的列和表的名称进行额外更改。一个常见的例子是将数据库模式中常用的 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 页面上了解更多信息。