I am facing an issue with my database and updating student grades using Prisma transactions. When any ID is not found in the database, the transaction fails without indicating which record caused the error.
I want to be able to throw a custom error that specifies the ID of the record that wasn't found so I can inform the user.
Below is the code snippet:
const grades = [
{id: 1, grade: '100'},
{id: 45, grade: '98' }
]
prisma.$transaction(
grades.map((el) => prisma.student.update({
where: { id: el.id },
data: { grade: el.grade }
})
)
However, when an ID is not found in the database, it throws a generic error message like Record not found.
, without detailing which specific ID caused the issue.
I have tried adding a catch block to throw a custom error with the relevant information as follows:
grades.map((el) => prisma.student.update({
where: { id: el.id },
data: { grade: el.grade }
}).catch((e) => throw new Error(`ID not found ${el.id}`)
)
But this approach results in an error:
Argument of type 'Promise<Student>[]' is not assignable to parameter of type 'PrismaPromise<any>[]'.
Type 'Promise<Student>' is not assignable to type 'PrismaPromise<any>'.
Property '[prisma]' is missing in type 'Promise<Student>' but required in type '{ [prisma]: true; }'.
How can I modify the code to alert the user about which specific IDs are not found?