内省
使用 Prisma ORM 对数据库进行内省
为了本指南的目的,我们将使用一个包含三个表的演示 SQL 模式
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;
展开查看表格的图形概览
文章
列名 | 类型 | 主键 | 外键 | 必需 | 默认 |
---|---|---|---|---|---|
id | int | ✔️ | 否 | ✔️ | 自增 |
createdAt | datetime(3) | 否 | 否 | ✔️ | now() |
updatedAt | datetime(3) | 否 | 否 | ✔️ | |
title | varchar(255) | 否 | 否 | ✔️ | - |
content | varchar(191) | 否 | 否 | 否 | - |
published | tinyint(1) | 否 | 否 | ✔️ | false |
authorId | int | 否 | 否 | ✔️ | - |
个人资料
列名 | 类型 | 主键 | 外键 | 必需 | 默认 |
---|---|---|---|---|---|
id | int | ✔️ | 否 | ✔️ | 自增 |
bio | varchar(191) | 否 | 否 | 否 | - |
userId | int | 否 | 否 | ✔️ | - |
用户
列名 | 类型 | 主键 | 外键 | 必需 | 默认 |
---|---|---|---|---|---|
id | int | ✔️ | 否 | ✔️ | 自增 |
name | varchar(191) | 否 | 否 | 否 | - |
email | varchar(191) | 否 | 否 | ✔️ | - |
作为下一步,你将对数据库进行内省。内省的结果将是你的 Prisma 模式中的一个 数据模型。
运行以下命令对数据库进行内省
npx prisma db pull
此命令读取 .env
文件中定义的 DATABASE_URL
环境变量并连接到你的数据库。连接建立后,它会对数据库进行内省(即*读取数据库模式*)。然后将 SQL 数据库模式转换为 Prisma 数据模型。
内省完成后,你的 Prisma 模式会更新
数据模型现在看起来类似于这样
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?
}
有关模式定义的详细信息,请参阅 Prisma 模式参考。
Prisma 的数据模型是数据库模式的声明性表示,是生成的 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 模式中手动重命名它们,而无需更改数据库。
在此示例中,数据库模式遵循 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 页面上了解更多信息。