Jon Higger (He / Him)
Jon Higger (He / Him)
Explore posts from servers
PPrisma
Created by Jon Higger (He / Him) on 11/12/2024 in #help-and-questions
Do transactions work when you nest them?
Hello, I'm trying to figure out if transactions work correctly when you nest them. In my example I've got a couple of pretty complex business-logic-required queries, and each might have their own transactions within them:
import prisma from "./db.server"
export const reorderAllChildEntities = (entity, prismaInstance?) => {
return (prismaInstance ?? prisma).$transaction(tPrisma => {
// do stuff with tPrisma (actual code is way to big for this)
})
}

// in other file
export const deleteEntityAndReorderAllChildEntitiesForParent = ( entity ) => {
return prisma.$transaction(tPrisma => {
await tPrisma.entity.delete({where: {id: entity.id}})
const parentEntity = await prisma.parentEntity.find(
{where: {id: entity.parentID}}
)
await reorderAllChildEntities(parentEntity, tPrisma)
})
}
import prisma from "./db.server"
export const reorderAllChildEntities = (entity, prismaInstance?) => {
return (prismaInstance ?? prisma).$transaction(tPrisma => {
// do stuff with tPrisma (actual code is way to big for this)
})
}

// in other file
export const deleteEntityAndReorderAllChildEntitiesForParent = ( entity ) => {
return prisma.$transaction(tPrisma => {
await tPrisma.entity.delete({where: {id: entity.id}})
const parentEntity = await prisma.parentEntity.find(
{where: {id: entity.parentID}}
)
await reorderAllChildEntities(parentEntity, tPrisma)
})
}
If deleteEntityAndReorderAllChildEntitiesForParent fails from inside the transaction within reorderAllChildEntities function, will BOTH transactions be rolled back correctly, or is this bad practice?
12 replies