@@ -233,6 +233,8 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
233233 } )
234234 }
235235
236+ const adapterToDelete : Parameters < typeof adapter . deleteWhere > [ 0 ] [ ] = [ ]
237+ const adapterToInsert : Parameters < typeof adapter . insert > [ 0 ] [ ] = [ ]
236238 const localesToInsert : Record < string , unknown > [ ] = [ ]
237239 const relationsToInsert : Record < string , unknown > [ ] = [ ]
238240 const textsToInsert : Record < string , unknown > [ ] = [ ]
@@ -313,14 +315,14 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
313315 const localeTable = adapter . tables [ `${ tableName } ${ adapter . localesSuffix } ` ]
314316
315317 if ( operation === 'update' ) {
316- await adapter . deleteWhere ( {
318+ adapterToDelete . push ( {
317319 db,
318320 tableName : localeTableName ,
319321 where : eq ( localeTable . _parentID , insertedRow . id ) ,
320322 } )
321323 }
322324
323- await adapter . insert ( {
325+ adapterToInsert . push ( {
324326 db,
325327 tableName : localeTableName ,
326328 values : localesToInsert ,
@@ -352,7 +354,7 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
352354 }
353355
354356 if ( relationsToInsert . length > 0 ) {
355- await adapter . insert ( {
357+ adapterToInsert . push ( {
356358 db,
357359 tableName : relationshipsTableName ,
358360 values : relationsToInsert ,
@@ -476,7 +478,7 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
476478
477479 // Insert only non-duplicate relationships
478480 if ( relationshipsToActuallyInsert . length > 0 ) {
479- await adapter . insert ( {
481+ adapterToInsert . push ( {
480482 db,
481483 tableName : relationshipsTableName ,
482484 values : relationshipsToActuallyInsert ,
@@ -529,7 +531,7 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
529531 }
530532
531533 // Execute DELETE using Drizzle query builder
532- await adapter . deleteWhere ( {
534+ adapterToDelete . push ( {
533535 db,
534536 tableName : relationshipsTableName ,
535537 where : and ( ...conditions ) ,
@@ -559,7 +561,7 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
559561 }
560562
561563 if ( textsToInsert . length > 0 ) {
562- await adapter . insert ( {
564+ adapterToInsert . push ( {
563565 db,
564566 tableName : textsTableName ,
565567 values : textsToInsert ,
@@ -586,7 +588,7 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
586588 }
587589
588590 if ( numbersToInsert . length > 0 ) {
589- await adapter . insert ( {
591+ adapterToInsert . push ( {
590592 db,
591593 tableName : numbersTableName ,
592594 values : numbersToInsert ,
@@ -602,7 +604,7 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
602604 if ( operation === 'update' ) {
603605 for ( const tableName of rowToInsert . blocksToDelete ) {
604606 const blockTable = adapter . tables [ tableName ]
605- await adapter . deleteWhere ( {
607+ adapterToDelete . push ( {
606608 db,
607609 tableName,
608610 where : eq ( blockTable . _parentID , insertedRow . id ) ,
@@ -648,7 +650,7 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
648650 } , [ ] )
649651
650652 if ( blockLocaleRowsToInsert . length > 0 ) {
651- await adapter . insert ( {
653+ adapterToInsert . push ( {
652654 db,
653655 tableName : `${ tableName } ${ adapter . localesSuffix } ` ,
654656 values : blockLocaleRowsToInsert ,
@@ -694,7 +696,7 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
694696 for ( const [ selectTableName , tableRows ] of Object . entries ( selectsToInsert ) ) {
695697 const selectTable = adapter . tables [ selectTableName ]
696698 if ( operation === 'update' ) {
697- await adapter . deleteWhere ( {
699+ adapterToDelete . push ( {
698700 db,
699701 tableName : selectTableName ,
700702 where : eq ( selectTable . parent , insertedRow . id ) ,
@@ -710,14 +712,18 @@ export const upsertRow = async <T extends Record<string, unknown> | TypeWithID>(
710712 }
711713
712714 if ( tableRows . length ) {
713- await adapter . insert ( {
715+ adapterToInsert . push ( {
714716 db,
715717 tableName : selectTableName ,
716718 values : tableRows ,
717719 } )
718720 }
719721 }
720722
723+ // Run updates in parallel
724+ await Promise . all ( adapterToDelete . map ( ( value ) => adapter . deleteWhere ( value ) ) )
725+ await Promise . all ( adapterToInsert . map ( ( value ) => adapter . insert ( value ) ) )
726+
721727 // //////////////////////////////////
722728 // Error Handling
723729 // //////////////////////////////////
0 commit comments