Merge pull request #3874 from maphubs/fix-session-error

fix prisma session race condition error
This commit is contained in:
Mike Cao 2026-01-06 18:01:09 -08:00 committed by GitHub
commit 767fda21cd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -46,31 +46,23 @@ export async function relationalQuery({
createdAt, createdAt,
})); }));
const existing = await client.sessionData.findMany({
where: {
sessionId,
},
select: {
id: true,
sessionId: true,
dataKey: true,
},
});
for (const data of flattenedData) { for (const data of flattenedData) {
const { sessionId, dataKey, ...props } = data; const { sessionId, dataKey, ...props } = data;
const record = existing.find(e => e.sessionId === sessionId && e.dataKey === dataKey);
if (record) { // Try to update existing record using compound where clause
await client.sessionData.update({ // This is safer than using id from a previous query due to race conditions
const updateResult = await client.sessionData.updateMany({
where: { where: {
id: record.id, sessionId,
dataKey,
}, },
data: { data: {
...props, ...props,
}, },
}); });
} else {
// If no record was updated, create a new one
if (updateResult.count === 0) {
await client.sessionData.create({ await client.sessionData.create({
data, data,
}); });