File tree Expand file tree Collapse file tree 1 file changed +14
-22
lines changed
Expand file tree Collapse file tree 1 file changed +14
-22
lines changed Original file line number Diff line number Diff 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 } ) ;
You can’t perform that action at this time.
0 commit comments