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

相关推荐
CoderYanger14 小时前
Java EE 进阶:13. Spring事务和事务传播机制(第二弹)
java·开发语言·数据库·spring boot·后端·spring·java-ee
FungLeo17 小时前
FengCMS · PHP+MySQL 升级实录 ·双数据库验证:MySQL8.4与MariaDB 10.11的兼容矩阵
数据库·mysql·php·工程实践·fengcms
XW96325818 小时前
除湿机行业组织能力与人才地图报告
大数据·服务器·网络·数据结构·数据库·人工智能
Decay__20 小时前
PostgreSQL 性能调优方法论 —— pg_stat_statements、EXPLAIN ANALYZE、瓶颈定位流程与参数基线
运维·数据库·sql·postgresql
前端兰博1 天前
分库分表
数据库
智能收银研究所1 天前
收银系统选型对比:美团、客如云、拉卡拉、收钱吧各适合什么门店
java·前端·数据库
七夜zippoe1 天前
金仓 Kingbase 安装与 Java 连接(保姆级):从装库到 JDBC 跑通全记录
java·数据库·jdbc·kingbase·金仓
stormsha1 天前
中文路径让 MySQL 起不来:Failed to find valid data directory 与 Illegal byte sequence
运维·数据库·mysql
stormsha1 天前
MySQL 报 `data directory has files in it`:初始化失败一次,之后就永久起不来
运维·数据库·mysql
YYYing.1 天前
【Redis剑指offer系列 (一) 】Redis数据结构
数据库·redis·缓存·八股