Prisma:模型查询方法

  • findUnique

    • 使用唯一条件来获取单个数据记录,如根据id查询

    • 支持关键字:where, select, include

    • const result = await prisma.user.findUnique({
        where: {
          email: 'alice@prisma.io',
        },
      })
      
  • findFirst

    • 返回第一个匹配条件的记录

    • 支持关键字:select, include, rejectOnNotFound, where, orderBy, cursor, take, skip, distinct

    • // 获取 title 字段以 A test 开头的第一个 Post 记录,并反转列表(take)
      async function main() {
        const a = await prisma.post.create({
          data: {
            title: 'A test 1',
          },
        })
      
        const b = await prisma.post.create({
          data: {
            title: 'A test 2',
          },
        })
      
        const c = await prisma.post.findFirst({
          where: {
            title: {
              startsWith: 'A test',
            },
          },
          orderBy: {
            title: 'asc',
          },
          take: -1, // 反转列表
        })
      }
      
  • findMany

    • 返回多条记录

    • 支持关键字:select, include, where, orderBy, cursor, take, skip, distinct

    • const user = await prisma.user.findMany({
        where: { name: 'Alice' },
      })
      
  • create

    • 创建一条新的数据库记录

    • 支持关键字:data, select, include

    • Prisma Client 当前不支持在数据库级别批量插入,可手动通过循环实现

    • const user = await prisma.user.create({
        data: { email: 'alice@prisma.io' },
      })
      
  • update

    • 更新数据

    • 支持关键字:data, where, select, include

    • const user = await prisma.user.update({
        where: { id: 1 },
        data: { email: 'alice@prisma.io' },
      })
      
  • upsert

    • 更新现有、或创建新的数据库记录

    • 支持关键字:create, update, where, select, include

    • // 更新(如果存在)或创建一条 email 为 alice@prisma.io 的 User 记录
      const user = await prisma.user.upsert({
        where: { id: 1 },
        update: { email: 'alice@prisma.io' },
        create: { email: 'alice@prisma.io' },
      })
      
  • delete

    • 删除现有的数据库记录

    • 只支持根据id,或者unique属性进行删除

    • 支持关键字:where, select, include

    • const user = await prisma.user.delete({
        where: { id: 1 },
      })
      
  • deleteMany

    • 删除多条记录,可根据筛选条件批量删除

    • 支持关键字:where

    • // 删除所有 name 为 Alice 的 User 记录
      const deletedUserCount = await prisma.user.deleteMany({
        where: { name: 'Alice' },
      })
      
      // 删除所有User
      const deletedUserCount = await prisma.user.deleteMany({})
      
  • createMany

    • 在一个事务中创建多个记录,并返回成功插入数

    • 支持关键字:data, skipDuplicates

    • const users = await prisma.user.createMany({
        data: [
          { name: 'Sonali', email: 'sonali@prisma.io' },
          { name: 'Alex', email: 'alex@prisma.io' },
        ],
      })
      
  • updateMany

    • 更新一批已存在的数据库记录,并返回更新的记录数

    • 支持关键字:data, where

    • const updatedUserCount = await prisma.user.updateMany({
        where: { name: 'Alice' },
        data: { name: 'ALICE' },
      })
      
  • count

    • 返回符合条件的数据计数

    • 支持关键字:where, cursor, skip, take, orderBy, distinct, select

    • // 查询所有记录总数、查询name字段非空的总数,查询city字段非空的总数
      const c = await prisma.user.count({
        select: {
          _all: true,
          city: true,
          name: true,
        },
      })
      
  • aggregate

    • 支持关键字:where, orderBy, cursor, skip, take, distinct, _count, _avg, _sum, _min, _ max

    • // 返回所有User记录的profileViews的_min、_max和_count
      const minMaxAge = await prisma.user.aggregata({
          _count: {
          _all: true,
      },
          _max: {
          profileViews: true,
      },
          _min: {
          profileViews: true,
      }
      })
      
      // return : {
      //  _count: { _all: 29 },
      //  _max: { profileViews: 90 },
      //  _min: { profileViews: 0 }
      //}
      
  • groupBy

    • 聚合操作

    • 支持关键字:where, orderBy, by, having, skip, take, _count, _avg, _sum, _min, _max

    • // 按平均 profileViews 大于 200 的 country/city 分组,并返回每组 profileViews 的 _sum
      const groupUsers = await prisma.user.groupBy({
        by: ['country', 'city'],
        _count: {
          _all: true,
          city: true,
        },
        _sum: {
          profileViews: true,
        },
        orderBy: {
          country: 'desc',
        },
        having: {
          profileViews: {
            _avg: {
              gt: 200,
            },
          },
        },
      })
      
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

川涂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值