Skip to content

Commit

Permalink
fix: 🐛 use different method to check if transactionId should be used …
Browse files Browse the repository at this point in the history
…for batch
  • Loading branch information
dennemark committed Dec 5, 2024
1 parent 5234086 commit 5bd5dc7
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export function useCaslAbilities(


return Prisma.defineExtension((client) => {
const transactionsToBatch = new Set()

const allOperations = (getAbilities: () => AbilityBuilder<PureAbility<AbilityTuple, PrismaQuery>>) => ({
async $allOperations<T>({ args, query, model, operation, ...rest }: { args: any, query: any, model: any, operation: any }) {

Expand Down Expand Up @@ -202,14 +204,12 @@ export function useCaslAbilities(
return transactionQuery((client as any)._createItxClient(transaction))
} else {
return client.$transaction(async (tx) => {

(tx as any)[
Symbol.for("prisma.client.transaction.id")
] = 'casl-extension-' + (tx as any)[
Symbol.for("prisma.client.transaction.id")
]
const transactionId = tx[Symbol.for("prisma.client.transaction.id")].toString()
transactionsToBatch.add(transactionId)
//@ts-ignore
return transactionQuery(tx)
return transactionQuery(tx).finally(() => {
transactionsToBatch.delete(transactionId)
});
}, {
//https://github.com/prisma/prisma/issues/20015
maxWait: 10000 // default prisma pool timeout. would be better to get it from client
Expand All @@ -229,12 +229,11 @@ export function useCaslAbilities(
; (client as any)._requestHandler.dataloader.options.batchBy = (
request: any,
) => {

if (request.transaction?.id && !request.transaction?.id?.toString().startsWith('casl-extension-')) {
const batchId = getBatchId(request.protocolQuery);
if (request.transaction?.id && (!transactionsToBatch.has(request.transaction.id.toString()) && batchId)) {
return `transaction-${request.transaction.id}`;
}

return getBatchId(request.protocolQuery);
return batchId
};


Expand Down

0 comments on commit 5bd5dc7

Please sign in to comment.