Ortharion
Ortharion
PPrisma
Created by Ortharion on 9/11/2024 in #help-and-questions
Bad error messages with array of transactions
I'm concatenating my transactions like this

if (!isNil(changes.systems))
transactions = transactions.concat(this.handleSystemChanges(changes.systems, lastPulledAt));
if (!isNil(changes.equipment_photos))
transactions = transactions.concat(this.handleEquipmentPhotoChanges(changes.equipment_photos, lastPulledAt));

if (!isNil(changes.systems))
transactions = transactions.concat(this.handleSystemChanges(changes.systems, lastPulledAt));
if (!isNil(changes.equipment_photos))
transactions = transactions.concat(this.handleEquipmentPhotoChanges(changes.equipment_photos, lastPulledAt));
My payload looks like this
{
"equipment_photos" : {
"created": [
{
"id": "123",
"unknownProperty": "test"
}
],
"updated" : [],
"deleted" : ["0e306bfd-19d4-4a5d-a236-8b6b0b6eb5e6"]
},
"systems": {
"created": [],
"updated" : [],
"deleted" : ["0e306bfd-19d4-4a5d-a236-8b6b0b6eb5e6"]
}

}
{
"equipment_photos" : {
"created": [
{
"id": "123",
"unknownProperty": "test"
}
],
"updated" : [],
"deleted" : ["0e306bfd-19d4-4a5d-a236-8b6b0b6eb5e6"]
},
"systems": {
"created": [],
"updated" : [],
"deleted" : ["0e306bfd-19d4-4a5d-a236-8b6b0b6eb5e6"]
}

}
But my error looks like this (it will be the first comment...my post was too long apparently) The import part is that last line there "unknown argument 'unknownProperty'" Great! ... "prisma.systems.upsert" wait what. No. That error is actually on prisma.equipmentPhotos.upsert Prisma appears to be taking the first element in the array of transactions and attributing the error to that when the actual error is somewhere else. Is this a bug with prisma or should I be handling my transaction differently?
9 replies
PPrisma
Created by Ortharion on 8/15/2024 in #help-and-questions
Invalid error message when using transactions
We have a sync endpoint for our mobile app (it's offline capable first). The endpoint is entirely transaction based. So we iterate through the changes and build up an array of prisma queries and then wrap all those in a prismaClient.$transaction(transactions).
if (!isNil(changes.systems))
transactions = transactions.concat(this.handleSystemChanges(changes.systems, lastPulledAt));
if (!isNil(changes.equipment_photos))
transactions = transactions.concat(this.handleEquipmentPhotoChanges(changes.equipment_photos, lastPulledAt));

...

const result = await this.prismaClient.$transaction(transactions);
if (!isNil(changes.systems))
transactions = transactions.concat(this.handleSystemChanges(changes.systems, lastPulledAt));
if (!isNil(changes.equipment_photos))
transactions = transactions.concat(this.handleEquipmentPhotoChanges(changes.equipment_photos, lastPulledAt));

...

const result = await this.prismaClient.$transaction(transactions);
This is all wrapped in a try catch, but the issue is when something breaks, we seem to be getting erroneous error messages out of prisma. As in completely false error messages. It will say something like "can't find userId. Did you mean user?" Except the data being pushed to the user schema doesn't contain any invalid properties and userId is a valid property on the user schema. It's typically one of the other transactions that had an error. Any thoughts on why it's giving erroneous errors?
5 replies
PPrisma
Created by Ortharion on 8/6/2024 in #help-and-questions
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?
1 replies
PPrisma
Created by Ortharion on 4/25/2024 in #help-and-questions
Transaction timeout
Why might a transaction work fine on local, but then get a timeout when deployed. Same DB. Same Node version. Using NextJS deployed in a vercel serverless env. Exact same user. Exact same auth system. Exact same DB. Exact same project. Only real difference is one is running locally and one is deployed to vercel. Even worse is the transaction succeeds. The data is saved and I still get that error.
7 replies