@@ -141,6 +141,88 @@ describe(`Electric collection type resolution tests`, () => {
141141 > ( )
142142 } )
143143
144+ it ( `should correctly type mutations in transaction handlers when mapping over mutations array` , ( ) => {
145+ const schema = z . object ( {
146+ id : z . string ( ) ,
147+ title : z . string ( ) ,
148+ completed : z . boolean ( ) ,
149+ } )
150+
151+ type TodoType = z . infer < typeof schema >
152+
153+ const options = electricCollectionOptions ( {
154+ id : `todos` ,
155+ schema,
156+ getKey : ( item ) => item . id ,
157+ shapeOptions : {
158+ url : `/api/todos` ,
159+ params : { table : `todos` } ,
160+ } ,
161+ onDelete : ( params ) => {
162+ // Direct index access should be correctly typed
163+ expectTypeOf (
164+ params . transaction . mutations [ 0 ] . original
165+ ) . toEqualTypeOf < TodoType > ( )
166+
167+ // Non-null assertion on second element should be correctly typed
168+ expectTypeOf (
169+ params . transaction . mutations [ 1 ] ! . original
170+ ) . toEqualTypeOf < TodoType > ( )
171+
172+ // When mapping over mutations, each mutation.original should be correctly typed
173+ params . transaction . mutations . map ( ( mutation ) => {
174+ expectTypeOf ( mutation . original ) . toEqualTypeOf < TodoType > ( )
175+ return mutation . original . id
176+ } )
177+
178+ return Promise . resolve ( { txid : 1 } )
179+ } ,
180+ onInsert : ( params ) => {
181+ // Direct index access should be correctly typed
182+ expectTypeOf (
183+ params . transaction . mutations [ 0 ] . modified
184+ ) . toEqualTypeOf < TodoType > ( )
185+
186+ // When mapping over mutations, each mutation.modified should be correctly typed
187+ params . transaction . mutations . map ( ( mutation ) => {
188+ expectTypeOf ( mutation . modified ) . toEqualTypeOf < TodoType > ( )
189+ return mutation . modified . id
190+ } )
191+
192+ return Promise . resolve ( { txid : 1 } )
193+ } ,
194+ onUpdate : ( params ) => {
195+ // Direct index access should be correctly typed
196+ expectTypeOf (
197+ params . transaction . mutations [ 0 ] . original
198+ ) . toEqualTypeOf < TodoType > ( )
199+ expectTypeOf (
200+ params . transaction . mutations [ 0 ] . modified
201+ ) . toEqualTypeOf < TodoType > ( )
202+
203+ // When mapping over mutations, each mutation should be correctly typed
204+ params . transaction . mutations . map ( ( mutation ) => {
205+ expectTypeOf ( mutation . original ) . toEqualTypeOf < TodoType > ( )
206+ expectTypeOf ( mutation . modified ) . toEqualTypeOf < TodoType > ( )
207+ return mutation . modified . id
208+ } )
209+
210+ return Promise . resolve ( { txid : 1 } )
211+ } ,
212+ } )
213+
214+ // Verify that the handlers are properly typed
215+ expectTypeOf ( options . onDelete ) . parameters . toEqualTypeOf <
216+ [ DeleteMutationFnParams < TodoType > ]
217+ > ( )
218+ expectTypeOf ( options . onInsert ) . parameters . toEqualTypeOf <
219+ [ InsertMutationFnParams < TodoType > ]
220+ > ( )
221+ expectTypeOf ( options . onUpdate ) . parameters . toEqualTypeOf <
222+ [ UpdateMutationFnParams < TodoType > ]
223+ > ( )
224+ } )
225+
144226 it ( `should infer types from Zod schema through electric collection options to live query` , ( ) => {
145227 // Define a Zod schema for a user with basic field types
146228 const userSchema = z . object ( {
0 commit comments