mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
fix prisma session race condition error
This commit is contained in:
parent
81e27fc18c
commit
b088a2ee6e
1 changed files with 14 additions and 22 deletions
|
|
@ -46,31 +46,23 @@ export async function relationalQuery({
|
|||
createdAt,
|
||||
}));
|
||||
|
||||
const existing = await client.sessionData.findMany({
|
||||
where: {
|
||||
sessionId,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
sessionId: true,
|
||||
dataKey: true,
|
||||
},
|
||||
});
|
||||
|
||||
for (const data of flattenedData) {
|
||||
const { sessionId, dataKey, ...props } = data;
|
||||
const record = existing.find(e => e.sessionId === sessionId && e.dataKey === dataKey);
|
||||
|
||||
if (record) {
|
||||
await client.sessionData.update({
|
||||
where: {
|
||||
id: record.id,
|
||||
},
|
||||
data: {
|
||||
...props,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
// Try to update existing record using compound where clause
|
||||
// This is safer than using id from a previous query due to race conditions
|
||||
const updateResult = await client.sessionData.updateMany({
|
||||
where: {
|
||||
sessionId,
|
||||
dataKey,
|
||||
},
|
||||
data: {
|
||||
...props,
|
||||
},
|
||||
});
|
||||
|
||||
// If no record was updated, create a new one
|
||||
if (updateResult.count === 0) {
|
||||
await client.sessionData.create({
|
||||
data,
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue