prisma-client-api

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Prisma 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

模型查询方法

MethodDescription
findUnique()
Find one record by unique field
findUniqueOrThrow()
Find one or throw error
findFirst()
Find first matching record
findFirstOrThrow()
Find first or throw error
findMany()
Find multiple records
create()
Create a new record
createMany()
Create multiple records
createManyAndReturn()
Create multiple and return them
update()
Update one record
updateMany()
Update multiple records
updateManyAndReturn()
Update multiple and return them
upsert()
Update or create record
delete()
Delete one record
deleteMany()
Delete multiple records
count()
Count matching records
aggregate()
Aggregate values (sum, avg, etc.)
groupBy()
Group and aggregate
方法描述
findUnique()
通过唯一字段查找单条记录
findUniqueOrThrow()
查找单条记录,不存在则抛出错误
findFirst()
查找第一条匹配的记录
findFirstOrThrow()
查找第一条匹配的记录,不存在则抛出错误
findMany()
查找多条记录
create()
创建一条新记录
createMany()
创建多条记录
createManyAndReturn()
创建多条记录并返回结果
update()
更新单条记录
updateMany()
更新多条记录
updateManyAndReturn()
更新多条记录并返回结果
upsert()
更新记录,不存在则创建
delete()
删除单条记录
deleteMany()
删除多条记录
count()
统计匹配的记录数量
aggregate()
聚合计算(求和、平均值等)
groupBy()
分组并进行聚合计算

Query Options

查询选项

OptionDescription
where
Filter conditions
select
Fields to include
include
Relations to load
omit
Fields to exclude
orderBy
Sort order
take
Limit results
skip
Skip results (pagination)
cursor
Cursor-based pagination
distinct
Unique values only
选项描述
where
筛选条件
select
要包含的字段
include
要加载的关联关系
omit
要排除的字段
orderBy
排序规则
take
结果数量限制
skip
跳过指定数量的结果(分页)
cursor
基于游标分页
distinct
仅返回唯一值

Client Methods

客户端方法

MethodDescription
$connect()
Explicitly connect to database
$disconnect()
Disconnect from database
$transaction()
Execute transaction
$queryRaw()
Execute raw SQL query
$executeRaw()
Execute raw SQL command
$on()
Subscribe to events
$extends()
Add extensions
方法描述
$connect()
显式连接数据库
$disconnect()
断开数据库连接
$transaction()
执行事务
$queryRaw()
执行原生SQL查询
$executeRaw()
执行原生SQL命令
$on()
订阅事件
$extends()
添加扩展

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, $extends

Filter Operators

筛选操作符

OperatorDescription
equals
Exact match
not
Not equal
in
In array
notIn
Not in array
lt
,
lte
Less than
gt
,
gte
Greater than
contains
String contains
startsWith
String starts with
endsWith
String ends with
mode
Case sensitivity
操作符描述
equals
精确匹配
not
不匹配
in
在指定数组内
notIn
不在指定数组内
lt
,
lte
小于、小于等于
gt
,
gte
大于、大于等于
contains
字符串包含指定内容
startsWith
字符串以指定内容开头
endsWith
字符串以指定内容结尾
mode
大小写敏感性设置

Relation Filters

关联关系筛选器

OperatorDescription
some
At least one related record matches
every
All related records match
none
No related records match
is
Related record matches (1-to-1)
isNot
Related record doesn't match
操作符描述
some
至少有一条关联记录匹配条件
every
所有关联记录均匹配条件
none
没有关联记录匹配条件
is
关联记录匹配指定条件(一对一关系)
isNot
关联记录不匹配指定条件

Resources

相关资源