prisma操作数据库的方法使用教程(简洁版)

可以简单的理解为堆积木,也就是说在调用"一、"内提到的方法时,传参根据各自的具体业务去决定传"二、"中提到的参数,不过传参要遵循附录的"参数可用矩阵",哪些函数可以用哪些参数要严格遵循

一、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:写入的内容

相关推荐
zyseo811 小时前
谷歌SEO 准首页词挖掘脚本:找出排在第 8-20 名的隐藏流量
大数据·数据库·人工智能
xiaogai_gai13 小时前
仅查询策略实战:从金蝶云·星空旗舰版定时拉取收料通知单
大数据·数据库·python
数据工匠老o14 小时前
异构数据库DDL同步:260条链路的结构变更自动适配方案
数据库·架构
这个DBA有点耶15 小时前
银行业市占率最高的国产数据库是谁?先搞清楚“市占率”的四种口径
数据库·程序员·架构
用户62799471826215 小时前
南大通用GBase 8a之基于审计日志的冷温热数据分析
数据库
数据库小学妹15 小时前
数据库运维五级成熟度:监控、备份、高可用怎么串起来
运维·数据库·监控
一个天蝎座的程序猿15 小时前
为了给报表提速,我把金仓KES的列存储折腾了个遍:聊聊KES的技术能力
数据库
飞越未来云城的霓虹飞猪15 小时前
AI 数据库地图:从 PostgreSQL 到向量数据库,一次看懂 AI 时代的数据底座
数据库
不像程序员的程序媛16 小时前
导出mysql数据结构
数据库·mysql
GlobalTradercrb16 小时前
独立站SEO先写博客还是先优化产品页?看站点阶段
数据库·人工智能·google·pdf·go·seo