mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
Merge pull request #3874 from maphubs/fix-session-error
fix prisma session race condition error
This commit is contained in:
commit
767fda21cd
1 changed files with 14 additions and 22 deletions
|
|
@ -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,
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue