I run a transaction on MongoDB but also include a Pinecone operation in this transaction. I’m aware that Pinecone will not be rolled back if the transaction fails. But this is why I made Pinecone the last operation in the transaction. If Pinecone fails, MongoDB is rolled back. If MongoDB fails, we don’t even reach the execution if Pinecone.
This this fine or should I not do this?
Here is the code. I’m using the Prisma ORM to execute my MongoDB transaction:
const newEntry = await prisma.$transaction(async (tx) => {
// MongoDB operations
if (draft) {
if (session.user.id !== draft?.userId) throw Error("Not authorized");
await tx.diaryEntryDraft.delete({ where: { id: draft?.id } });
}
const newEntry = await tx.diaryEntry.create({
data: entryData,
});
// Pinecone operation
await diaryEntriesIndex.upsert([
{
id: entryId,
values: embedding,
metadata: { userId: session.user.id },
},
]);
return newEntry;
});