Hello, We have an API call that needs to fetch a database entry, get a column, and append to that column. The datatype is JSON so we are adding an object to a JSON array and saving it back to the that row.
We do that in two steps: prisma.findUnique and prisma.update. The problem occurs where the API is called back to back and findUnique grabs the same data twice and then only one value is saved and the other seems to get overwritten. We thought wrapping this in a transaction would fix the issue, but it still happens. Throwing a random sleep in the API call so that the prisma calls are spread out by a few milliseconds fixes the issue but feels like a hack. Essentially this is what seems to happen:
API call 1 - Gets JSON data - currently length 1
API call 2 - Gets JSON data - currently length 1
API call 1 - Saves JSON data - currently length 2
API call 2 - Overwrites JSON data - currently length 2
Expected behavior in a transaction
API call 1 - Gets JSON data - currently length 1
API call 1 - Saves JSON data - currently length 2
API call 2 - Gets JSON data - currently length 2
API call 2 - Save JSON data - currently length 3
Any ideas to solve this without using a setTimer() to space the API calls out and thus the prisma calls, would be greatly appreciated.