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

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

1、$transaction api方式处理事务

typescript 复制代码
const deleteChapter = prisma.chapter.delete({
  where: {
    docId: docId,
    chapterId: chapterId,
  },
})

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

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

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

2、嵌套事务

一对一关联

typescript 复制代码
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])
}

嵌套事务操作数据保存

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

一对多关联

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

3、交互式事务

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

typescript 复制代码
generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["interactiveTransactions"]
}
typescript 复制代码
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,
    },
  })
})
相关推荐
samonyu25 分钟前
fnm 简介及使用
前端·node.js
李游Leo12 小时前
Node.js 多版本管理与 nvm/nvs 使用全流程(含国内镜像加速与常见坑)
node.js
Q_Q196328847513 小时前
python+springboot+uniapp微信小程序题库系统 在线答题 题目分类 错题本管理 学习记录查询系统
spring boot·python·django·uni-app·node.js·php
陈随易18 小时前
适合中国宝宝的AI编程神器,文心快码
前端·后端·node.js
Q_Q196328847521 小时前
python+springboot大学生心理测评与分析系统 心理问卷测试 自动评分分析 可视化反馈系统
开发语言·spring boot·python·django·flask·node.js·php
EndingCoder1 天前
Electron 新特性:2025 版本更新解读
前端·javascript·缓存·electron·前端框架·node.js·桌面端
machinecat1 天前
node,小程序合成音频的方式
前端·node.js
草木红1 天前
express 框架基础和 EJS 模板
arcgis·node.js·express
亮子AI2 天前
【npm】npm 包更新工具 npm-check-updates (ncu)
前端·npm·node.js
Yvonne爱编码2 天前
构建高效协作的桥梁:前后端衔接实践与接口文档规范详解
前端·git·ajax·webpack·node.js