NextJS开发:Prisma数据库事务处理

Prisma是一个开源的ORM库,用于在Node.js和TypeScript中访问数据库。它可以轻松地将数据库模式转换为GraphQL API,并提供查询和变更解析器。Prisma支持多个数据库,包括PostgreSQL,MySQL,SQLite和SQL Server。Prisma使用数据模型定义和可重用模块的概念来帮助开发者快速建设可扩展、可维护的应用程序。它还提供了数据实时更新和订阅的功能,使得应用程序更加响应和动态。Prisma提供了多种开启事务处理的编码方式,适用于不同场景。

1、$transaction api方式处理事务

const deleteChapter = prisma.chapter.delete({
  where: {
    docId: docId,
    chapterId: chapterId,
  },
})

const deleteChapterContent =  prisma.chapterContent.deleteMany({
  where: {
    chapterId: chapterId,
  },
})

await prisma.$transaction([deleteChapter, deleteChapterContent])

适用于:后一条数据库,不需要前一条的返回的结果,甚至变换前后执行顺序也不影响最终结果的场景。关联保存场景,先保存主表,再用主表的自增主键来继续保存子表数据显然就不适用这种适用于嵌套事务(推荐)或交互式事务。

2、嵌套事务

一对一关联

model Chapter {
  @@map("d_chapter")
  chapterId         BigInt @id @default(autoincrement()) @map("chapter_id")
  chapterName       String @db.VarChar(128) @map("chapter_name")
  chapterContent    ChapterContent?
}

model ChapterContent {
  @@map("d_chapter_content")
  chapterId         BigInt @id @map("chapter_id")
  content           String? @db.Text
  chapter           Chapter @relation(fields: [chapterId], references: [chapterId])
}

嵌套事务操作数据保存

import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
let chapter = await prisma.chapter.create({
  data: {
    chapterName: "test",
    content: {
      create: {
        content: '', createAt: DateUtils.now()
      }
    }
  },
})

一对多关联

let chapter = await prisma.account.create({
  data: {
    userName: "test",
    articles: 
      create: [
        { content: '', createAt: DateUtils.now() },
        { content: '', createAt: DateUtils.now() }
      ]
    }
  },
})

3、交互式事务

适用于事务期间有逻辑判断是否执行后续事务分支的场景,为了支持这种用例和更多的用例,你可以通过在你的 Prisma Schema 的生成器中添加interactiveTransactions来启用交互式事务

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["interactiveTransactions"]
}
import { Seat } from 'prisma'
const userEmail = 'alice@prisma.io'
const movieName = 'HiddenFigures'

// 开始事务
const seat: Seat = await prisma.$transaction(async (prisma) => {
  // 找到一个可用座位
  const availableSeat = await prisma.seat.findFirst({
    where: {
      claimedBy: null,
      movie: {
        name: movieName,
      },
    },
  })

  // 如果座位不存在抛异常
  if (!availableSeat) {
    // 这将会滚事务
    throw new Error(`Oh no! ${movieName} is all booked.`)
  }

  // 认领座位,提交事务,返回结果
  return prisma.seat.update({
    data: {
      claimedBy: userEmail,
    },
    where: {
      id: availableSeat.id,
    },
  })
})
  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ArslanRobot

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

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

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

打赏作者

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

抵扣说明:

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

余额充值