prisma-client-api
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePrisma Client API Reference
Prisma Client API参考
Complete API reference for Prisma Client. This skill provides guidance on model queries, filtering, relations, and client methods for Prisma ORM 7.x.
Prisma Client的完整API参考文档。本技能为Prisma ORM 7.x版本提供模型查询、筛选、关联关系及客户端方法的使用指导。
When to Apply
适用场景
Reference this skill when:
- Writing database queries with Prisma Client
- Performing CRUD operations (create, read, update, delete)
- Filtering and sorting data
- Working with relations
- Using transactions
- Configuring client options
在以下场景下可参考本技能:
- 使用Prisma Client编写数据库查询
- 执行CRUD操作(创建、读取、更新、删除)
- 筛选和排序数据
- 处理关联关系
- 使用事务
- 配置客户端选项
Client Instantiation (v7)
客户端实例化(v7版本)
typescript
import { PrismaClient } from '../generated/client'
import { PrismaPg } from '@prisma/adapter-pg'
const adapter = new PrismaPg({
connectionString: process.env.DATABASE_URL
})
const prisma = new PrismaClient({ adapter })typescript
import { PrismaClient } from '../generated/client'
import { PrismaPg } from '@prisma/adapter-pg'
const adapter = new PrismaPg({
connectionString: process.env.DATABASE_URL
})
const prisma = new PrismaClient({ adapter })Model Query Methods
模型查询方法
| Method | Description |
|---|---|
| Find one record by unique field |
| Find one or throw error |
| Find first matching record |
| Find first or throw error |
| Find multiple records |
| Create a new record |
| Create multiple records |
| Create multiple and return them |
| Update one record |
| Update multiple records |
| Update multiple and return them |
| Update or create record |
| Delete one record |
| Delete multiple records |
| Count matching records |
| Aggregate values (sum, avg, etc.) |
| Group and aggregate |
| 方法 | 描述 |
|---|---|
| 通过唯一字段查找单条记录 |
| 查找单条记录,不存在则抛出错误 |
| 查找第一条匹配的记录 |
| 查找第一条匹配的记录,不存在则抛出错误 |
| 查找多条记录 |
| 创建一条新记录 |
| 创建多条记录 |
| 创建多条记录并返回结果 |
| 更新单条记录 |
| 更新多条记录 |
| 更新多条记录并返回结果 |
| 更新记录,不存在则创建 |
| 删除单条记录 |
| 删除多条记录 |
| 统计匹配的记录数量 |
| 聚合计算(求和、平均值等) |
| 分组并进行聚合计算 |
Query Options
查询选项
| Option | Description |
|---|---|
| Filter conditions |
| Fields to include |
| Relations to load |
| Fields to exclude |
| Sort order |
| Limit results |
| Skip results (pagination) |
| Cursor-based pagination |
| Unique values only |
| 选项 | 描述 |
|---|---|
| 筛选条件 |
| 要包含的字段 |
| 要加载的关联关系 |
| 要排除的字段 |
| 排序规则 |
| 结果数量限制 |
| 跳过指定数量的结果(分页) |
| 基于游标分页 |
| 仅返回唯一值 |
Client Methods
客户端方法
| Method | Description |
|---|---|
| Explicitly connect to database |
| Disconnect from database |
| Execute transaction |
| Execute raw SQL query |
| Execute raw SQL command |
| Subscribe to events |
| Add extensions |
| 方法 | 描述 |
|---|---|
| 显式连接数据库 |
| 断开数据库连接 |
| 执行事务 |
| 执行原生SQL查询 |
| 执行原生SQL命令 |
| 订阅事件 |
| 添加扩展 |
Quick Examples
快速示例
Find records
查找记录
typescript
// Find by unique field
const user = await prisma.user.findUnique({
where: { email: 'alice@prisma.io' }
})
// Find with filter
const users = await prisma.user.findMany({
where: { role: 'ADMIN' },
orderBy: { createdAt: 'desc' },
take: 10
})typescript
// 通过唯一字段查找
const user = await prisma.user.findUnique({
where: { email: 'alice@prisma.io' }
})
// 带筛选条件查找
const users = await prisma.user.findMany({
where: { role: 'ADMIN' },
orderBy: { createdAt: 'desc' },
take: 10
})Create records
创建记录
typescript
const user = await prisma.user.create({
data: {
email: 'alice@prisma.io',
name: 'Alice',
posts: {
create: { title: 'Hello World' }
}
},
include: { posts: true }
})typescript
const user = await prisma.user.create({
data: {
email: 'alice@prisma.io',
name: 'Alice',
posts: {
create: { title: 'Hello World' }
}
},
include: { posts: true }
})Update records
更新记录
typescript
const user = await prisma.user.update({
where: { id: 1 },
data: { name: 'Alice Smith' }
})typescript
const user = await prisma.user.update({
where: { id: 1 },
data: { name: 'Alice Smith' }
})Delete records
删除记录
typescript
await prisma.user.delete({
where: { id: 1 }
})typescript
await prisma.user.delete({
where: { id: 1 }
})Transactions
事务
typescript
const [user, post] = await prisma.$transaction([
prisma.user.create({ data: { email: 'alice@prisma.io' } }),
prisma.post.create({ data: { title: 'Hello', authorId: 1 } })
])typescript
const [user, post] = await prisma.$transaction([
prisma.user.create({ data: { email: 'alice@prisma.io' } }),
prisma.post.create({ data: { title: 'Hello', authorId: 1 } })
])Rule Files
规则文件
Detailed API documentation:
rules/constructor.md - PrismaClient constructor options
rules/model-queries.md - CRUD operations
rules/query-options.md - select, include, omit, where, orderBy
rules/filters.md - Filter conditions and operators
rules/relations.md - Relation queries and nested operations
rules/transactions.md - Transaction API
rules/raw-queries.md - $queryRaw, $executeRaw
rules/client-methods.md - $connect, $disconnect, $on, $extends详细的API文档:
rules/constructor.md - PrismaClient构造函数选项
rules/model-queries.md - CRUD操作
rules/query-options.md - select, include, omit, where, orderBy
rules/filters.md - 筛选条件与操作符
rules/relations.md - 关联查询与嵌套操作
rules/transactions.md - 事务API
rules/raw-queries.md - $queryRaw, $executeRaw
rules/client-methods.md - $connect, $disconnect, $on, $extendsFilter Operators
筛选操作符
| Operator | Description |
|---|---|
| Exact match |
| Not equal |
| In array |
| Not in array |
| Less than |
| Greater than |
| String contains |
| String starts with |
| String ends with |
| Case sensitivity |
| 操作符 | 描述 |
|---|---|
| 精确匹配 |
| 不匹配 |
| 在指定数组内 |
| 不在指定数组内 |
| 小于、小于等于 |
| 大于、大于等于 |
| 字符串包含指定内容 |
| 字符串以指定内容开头 |
| 字符串以指定内容结尾 |
| 大小写敏感性设置 |
Relation Filters
关联关系筛选器
| Operator | Description |
|---|---|
| At least one related record matches |
| All related records match |
| No related records match |
| Related record matches (1-to-1) |
| Related record doesn't match |
| 操作符 | 描述 |
|---|---|
| 至少有一条关联记录匹配条件 |
| 所有关联记录均匹配条件 |
| 没有关联记录匹配条件 |
| 关联记录匹配指定条件(一对一关系) |
| 关联记录不匹配指定条件 |