Merge pull request #3874 from maphubs/fix-session-error
fix prisma session race condition error
This commit is contained in:
@@ -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
|
||||||
where: {
|
const updateResult = await client.sessionData.updateMany({
|
||||||
id: record.id,
|
where: {
|
||||||
},
|
sessionId,
|
||||||
data: {
|
dataKey,
|
||||||
...props,
|
},
|
||||||
},
|
data: {
|
||||||
});
|
...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,
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user