@@ -85,6 +85,46 @@ const checkExistentEventTypes = async ({
8585 }
8686} ;
8787
88+ function connectUsersToEventTypesQueryWithParams (
89+ eventPairs : {
90+ userId : number | null ;
91+ id : number ;
92+ } [ ]
93+ ) : { sqlQuery : string ; parameters : ( number | string ) [ ] } {
94+ if ( eventPairs . length === 0 ) {
95+ throw new Error ( "No event pairs provided for bulk connection." ) ;
96+ }
97+
98+ // Array to hold the SQL placeholders: ($1, $2), ($3, $4), ...
99+ const valuePlaceholders : string [ ] = [ ] ;
100+
101+ // Flat array of all IDs to be inserted as raw SQL parameters
102+ const parameters : ( number | string ) [ ] = [ ] ;
103+
104+ let paramIndex = 1 ;
105+
106+ // --- Build the parameterized SQL values ---
107+ for ( const pair of eventPairs ) {
108+ // Construct the pair of placeholders for the current row
109+ valuePlaceholders . push ( `($${ paramIndex ++ } , $${ paramIndex ++ } )` ) ;
110+
111+ if ( pair . userId != null ) {
112+ parameters . push ( pair . id ) ;
113+ parameters . push ( pair . userId ) ;
114+ }
115+ }
116+
117+ const sqlQuery = `
118+ INSERT INTO "_user_eventtype" ("A", "B")
119+ VALUES
120+ ${ valuePlaceholders . join ( ",\n" ) }
121+ ON CONFLICT DO NOTHING;
122+ ` ;
123+
124+ // sqlQuery and its parameters for execution
125+ return { sqlQuery, parameters } ;
126+ }
127+
88128export default async function handleChildrenEventTypes ( {
89129 eventTypeId : parentId ,
90130 oldEventType,
@@ -214,18 +254,12 @@ export default async function handleChildrenEventTypes({
214254
215255 // Connect users to their event types (many-to-many relation)
216256 // This is needed because createMany doesn't support nested relations
217- await Promise . all (
218- createdEvents . map ( ( event ) =>
219- tx . eventType . update ( {
220- where : { id : event . id } ,
221- data : {
222- users : {
223- connect : [ { id : event . userId ! } ] ,
224- } ,
225- } ,
226- } )
227- )
228- ) ;
257+ if ( createdEvents . length ) {
258+ const bulkQueryAndParams = connectUsersToEventTypesQueryWithParams ( createdEvents ) ;
259+ if ( bulkQueryAndParams ) {
260+ await tx . $executeRawUnsafe ( bulkQueryAndParams . sqlQuery , ...bulkQueryAndParams . parameters ) ;
261+ }
262+ }
229263
230264 // Link workflows if any exist
231265 if ( currentWorkflowIds && currentWorkflowIds . length > 0 ) {
0 commit comments