transaction hangs when doing async work in $extends

if (modelsListener.includes(model) && operationWouldChangeData(operation)) {
return handleModelChanged(prisma, model, operation, args, query);
} else {
return query(args);
}
if (modelsListener.includes(model) && operationWouldChangeData(operation)) {
return handleModelChanged(prisma, model, operation, args, query);
} else {
return query(args);
}
In our $allOperations hook in prisma.$extends, we are doing some work to send an SNS message (async, I think, so isn't bottlenecking the query) when a change happens on certain tables. Kind of a simple way to implement the new prisma table listener. The function handleModelChanged
export async function handleModelChanged(
prisma: PrismaClient | Prisma.TransactionClient,
model: string,
operation: string,
args: any,
query: (args: any) => Promise<any>,
) {
let queryResponse;
const awsAccessService = new AWSAccessService();

let oldData: any | null = null;

console.log("operation", operation);

if (operation === "upsert" || operation === "update") {
oldData = await prisma[model.toLowerCase()].findUnique({ where: { id: args.where.id } });
console.log("oldData", oldData);
}

queryResponse = await query(args);

const snsSend = awsAccessService.sendSnsMessage(
{
type: model.charAt(0).toLowerCase() + model.slice(1) + "-changed",
before: oldData,
after: queryResponse,
},
serverEnvironment.DATA_CHANGED_SNS_TOPIC,
);

snsSend
.then((data) => {
console.log("Message sent to SNS", data);
})
.catch((err) => {
console.log("Error sending message to SNS", err);
});

return queryResponse;
}
export async function handleModelChanged(
prisma: PrismaClient | Prisma.TransactionClient,
model: string,
operation: string,
args: any,
query: (args: any) => Promise<any>,
) {
let queryResponse;
const awsAccessService = new AWSAccessService();

let oldData: any | null = null;

console.log("operation", operation);

if (operation === "upsert" || operation === "update") {
oldData = await prisma[model.toLowerCase()].findUnique({ where: { id: args.where.id } });
console.log("oldData", oldData);
}

queryResponse = await query(args);

const snsSend = awsAccessService.sendSnsMessage(
{
type: model.charAt(0).toLowerCase() + model.slice(1) + "-changed",
before: oldData,
after: queryResponse,
},
serverEnvironment.DATA_CHANGED_SNS_TOPIC,
);

snsSend
.then((data) => {
console.log("Message sent to SNS", data);
})
.catch((err) => {
console.log("Error sending message to SNS", err);
});

return queryResponse;
}
This works fine unless I'm doing a transaction, but specifically, THIS type of transation.
await prisma.$transaction(async (tx) => {
...some code
}
await prisma.$transaction(async (tx) => {
...some code
}
This just hangs. I can do
prisma.$transaction([somePrismaQuery])
prisma.$transaction([somePrismaQuery])
And that works fine. How can I tweak the handleModelChanged function so that it doesn't cause the problematic transcations to hang?
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server