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?
Only one way, I think you can make something that accepts a full prisma client but not a transaction client, but not vise verce.
12 replies
PPrisma
Created by Jon Higger (He / Him) on 11/12/2024 in #help-and-questions
Do transactions work when you nest them?
export const dbFnA = ({
otherParams,
prismaInstance // can be undefined
}) => {
return transactionOrUseExisting(prismaInstance)($prisma => {

})
}

// That way I either get a transaction when I use dbFnA OR I can also compose it from another fn such that the transaction works across both as if it's one transaction
export const dbFnB = ({
otherParams,
}) => {
return prisma.$transaction(async $prisma => {
const b = await dbFnB({ ...otherParams, prismaInstance: $prisma })
})
}
export const dbFnA = ({
otherParams,
prismaInstance // can be undefined
}) => {
return transactionOrUseExisting(prismaInstance)($prisma => {

})
}

// That way I either get a transaction when I use dbFnA OR I can also compose it from another fn such that the transaction works across both as if it's one transaction
export const dbFnB = ({
otherParams,
}) => {
return prisma.$transaction(async $prisma => {
const b = await dbFnB({ ...otherParams, prismaInstance: $prisma })
})
}
12 replies
PPrisma
Created by Jon Higger (He / Him) on 11/12/2024 in #help-and-questions
Do transactions work when you nest them?
Which can then be used like this
12 replies
PPrisma
Created by Jon Higger (He / Him) on 11/12/2024 in #help-and-questions
Do transactions work when you nest them?
Here's a function I made that basically allows you to either make a transaction inside of a function, or allow you to pass a prisma instance in and use that as the transaction client
/**
* If you pass in a prisma client, it will use that client and not create a new transaction.
* Otherwise it will create a new transaction.
*/
export const transactionOrUseExisting =
(
prismaClient?: Prisma.TransactionClient,
// you can ignore the deferContraints stuff, that's because I need to defer constraints in my transactions specifically for a reordering algorithm
{ deferConstraints = true }: { deferConstraints?: boolean } = {},
) =>
async <T>(
businessLogicCb: (defPrisma: Prisma.TransactionClient) => Promise<T>,
) => {
if (prismaClient) {
if (deferConstraints) {
await runDeferredStatement(prismaClient);
}
return businessLogicCb(prismaClient);
} else {
return prisma.$transaction(async ($prisma) => {
if (deferConstraints) {
await runDeferredStatement($prisma);
}
return businessLogicCb($prisma);
});
}
};
/**
* If you pass in a prisma client, it will use that client and not create a new transaction.
* Otherwise it will create a new transaction.
*/
export const transactionOrUseExisting =
(
prismaClient?: Prisma.TransactionClient,
// you can ignore the deferContraints stuff, that's because I need to defer constraints in my transactions specifically for a reordering algorithm
{ deferConstraints = true }: { deferConstraints?: boolean } = {},
) =>
async <T>(
businessLogicCb: (defPrisma: Prisma.TransactionClient) => Promise<T>,
) => {
if (prismaClient) {
if (deferConstraints) {
await runDeferredStatement(prismaClient);
}
return businessLogicCb(prismaClient);
} else {
return prisma.$transaction(async ($prisma) => {
if (deferConstraints) {
await runDeferredStatement($prisma);
}
return businessLogicCb($prisma);
});
}
};
12 replies
PPrisma
Created by Jon Higger (He / Him) on 11/12/2024 in #help-and-questions
Do transactions work when you nest them?
This is what I did, I just passed the prisma instance around
12 replies