Skip to content

Commit 767fda2

Browse files
authored
Merge pull request #3874 from maphubs/fix-session-error
fix prisma session race condition error
2 parents c122fb7 + b088a2e commit 767fda2

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

src/queries/sql/sessions/saveSessionData.ts

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,31 +46,23 @@ export async function relationalQuery({
4646
createdAt,
4747
}));
4848

49-
const existing = await client.sessionData.findMany({
50-
where: {
51-
sessionId,
52-
},
53-
select: {
54-
id: true,
55-
sessionId: true,
56-
dataKey: true,
57-
},
58-
});
59-
6049
for (const data of flattenedData) {
6150
const { sessionId, dataKey, ...props } = data;
62-
const record = existing.find(e => e.sessionId === sessionId && e.dataKey === dataKey);
6351

64-
if (record) {
65-
await client.sessionData.update({
66-
where: {
67-
id: record.id,
68-
},
69-
data: {
70-
...props,
71-
},
72-
});
73-
} else {
52+
// Try to update existing record using compound where clause
53+
// This is safer than using id from a previous query due to race conditions
54+
const updateResult = await client.sessionData.updateMany({
55+
where: {
56+
sessionId,
57+
dataKey,
58+
},
59+
data: {
60+
...props,
61+
},
62+
});
63+
64+
// If no record was updated, create a new one
65+
if (updateResult.count === 0) {
7466
await client.sessionData.create({
7567
data,
7668
});

0 commit comments

Comments
 (0)