可以简单的理解为堆积木,也就是说在调用"一、"内提到的方法时,传参根据各自的具体业务去决定传"二、"中提到的参数,不过传参要遵循附录的"参数可用矩阵",哪些函数可以用哪些参数要严格遵循
一、API
| 操作 | 方法 | 说明 |
|---|---|---|
| 查一条 | findUnique() |
通过唯一字段(如 id、username)查找 |
| 查一条 | findFirst() |
通过任意条件查找第一条 |
| 查多条 | findMany() |
返回列表,支持分页/排序/过滤 |
| 计数 | count() |
返回符合条件的记录数 |
| 创建 | create() |
插入一条新记录 |
| 批量创建 | createMany() |
批量插入,性能更好 |
| 更新 | update() |
通过唯一字段定位并更新 |
| 批量更新 | updateMany() |
按条件批量更新 |
| 删除 | delete() |
通过唯一字段定位并删除 |
| 批量删除 | deleteMany() |
按条件批量删除 |
| 存在则更新否则创建 | upsert() |
原子操作,避免竞态条件 |
二、传参
1.条件筛选:where
|-----------------|----------------------------------------------------------|
| 等于 | { name : 'admin' } |
| 不等于 | { status: { not: 3 } } |
| 大于 | { age: { gt: 18 } } |
| 大于等于 | { age: { gte: 18 } } |
| 小于 | { age: { lt: 60 } } |
| 小于等于 | { age: { lte: 60 } } |
| 在集合里 | { id: { in: 1, 2, 3 } } |
| 不在集合里 | { id: { notIn: 4, 5 } } |
| 包含(模糊匹配) | { name: { contains: '用户' } } |
| 以...开头(模糊匹配) | { name: { startsWith: '系统' } } |
| 以...结尾(模糊匹配) | { name: { endsWith: '管理' } } |
| 不区分大小写的包含(模糊匹配) | { username: { contains: 'Admin', mode: 'insensitive' } } |
| 同时满足多个条件 | { AND: { status: 1 }, { type: { not: 3 } } } |
| 满足任一条件 | { OR: { username: 'admin' }, { com: '管理员'} } |
| 取反 | { NOT: { status: 0 } } |
| 嵌套组合 | { status: 1, OR: { type: 1 }, { type: 2 } } |
| 字段为 null | { deletedAt: null } |
| 字段不为 null | { deletedAt: { not: null } } |
2.返回指定字段:select
TypeScript
select: { // 支持一个或多个字段,具体由自己指定
id: true,
username: true,
nickname: true
}
注意:select 和 include 不能在同一层级同时使用,但可以在select内嵌套关联
TypeScript
select: {
username: true,
posts: {
select: { title: true } // 关联对象内部可以用 select 或 include
}
}
3.关联查询:include
include 用于加载关联模型的数据,前提是你在 schema.prisma 中定义了关系。
比如说你在schema之中定义了这样的关系:
TypeScript
model user_table {
id Int @id
username String
posts post_table[]
profile profile_table?
}
一对多和一对一关联的写法一致,只是返回的数据不同,如果是一对多关联那么对应返回的是一个数组,如果是一对一关联那么返回的是一个对象或者null
TypeScript
const user = await prisma.user_table.findUnique({
where: { id: 1 },
include: { posts: true }
})
// 如果是一对多,user.posts 是一个数组;如果是一对一,user.posts 是一个对象或者null
多层嵌套关联
TypeScript
include: {
posts: {
include: {
comments: {
where: { approved: true },
take: 5
}
}
}
}
4.排序:orderBy
|-------|-----------------------------------------------------------------------------|
| 单字段升序 | orderBy: { sort: 'asc' } |
| 单字段降序 | orderBy: { createdAt: 'desc' } |
| 多字段排序 | orderBy: { sort: 'asc' }, { id: 'desc' } //先按 sort 升序,sort 相同时按 id 降序 |
5.分页:skip + take
skip:起始记录,表示从哪条开始取
take:取多少条
TypeScript
const page = 2
const pageSize = 10
const users = await prisma.user_table.findMany({
where: { status: 1 },
skip: (page - 1) * pageSize,
take: pageSize,
orderBy: { id: 'desc' }
})
// 获取总数(配合分页使用)
const total = await prisma.user_table.count({
where: { status: 1 }
})
附录:参数可用矩阵
| 参数 | findUnique | findFirst | findMany | create | update | delete | count | upsert |
|---|---|---|---|---|---|---|---|---|
where |
✓(唯一字段) | ✓ | ✓ | ✘ | ✓ (唯一字段) | ✓ (唯一字段) | ✓ | ✓ (唯一字段) |
select |
✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✘ | ✓ |
include |
✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✘ | ✓ |
orderBy |
✘ | ✓ | ✓ | ✘ | ✘ | ✘ | ✘ | ✘ |
skip/take |
✘ | ✓ | ✓ | ✘ | ✘ | ✘ | ✘ | ✘ |
data |
✘ | ✘ | ✘ | ✓ | ✓ | ✘ | ✘ | ✓ |
data:写入的内容