@@ -145,60 +145,61 @@ export class NestedWriteVisitor {
145145 return ;
146146 }
147147
148- const context = { parent, field, nestingPath : [ ...nestingPath ] } ;
149148 const toplevel = field == undefined ;
150149
150+ const context = { parent, field, nestingPath : [ ...nestingPath ] } ;
151+ const pushNewContext = ( field : FieldInfo | undefined , model : string , where : any , unique = false ) => {
152+ return { ...context , nestingPath : [ ...context . nestingPath , { field, model, where, unique } ] } ;
153+ } ;
154+
151155 // visit payload
152156 switch ( action ) {
153157 case 'create' :
154- context . nestingPath . push ( { field, model, where : { } , unique : false } ) ;
155158 for ( const item of enumerate ( data ) ) {
159+ const newContext = pushNewContext ( field , model , { } ) ;
156160 let callbackResult : any ;
157161 if ( this . callback . create ) {
158- callbackResult = await this . callback . create ( model , item , context ) ;
162+ callbackResult = await this . callback . create ( model , item , newContext ) ;
159163 }
160164 if ( callbackResult !== false ) {
161165 const subPayload = typeof callbackResult === 'object' ? callbackResult : item ;
162- await this . visitSubPayload ( model , action , subPayload , context . nestingPath ) ;
166+ await this . visitSubPayload ( model , action , subPayload , newContext . nestingPath ) ;
163167 }
164168 }
165169 break ;
166170
167171 case 'createMany' :
168172 if ( data ) {
169- context . nestingPath . push ( { field, model, where : { } , unique : false } ) ;
173+ const newContext = pushNewContext ( field , model , { } ) ;
170174 let callbackResult : any ;
171175 if ( this . callback . createMany ) {
172- callbackResult = await this . callback . createMany ( model , data , context ) ;
176+ callbackResult = await this . callback . createMany ( model , data , newContext ) ;
173177 }
174178 if ( callbackResult !== false ) {
175179 const subPayload = typeof callbackResult === 'object' ? callbackResult : data . data ;
176- await this . visitSubPayload ( model , action , subPayload , context . nestingPath ) ;
180+ await this . visitSubPayload ( model , action , subPayload , newContext . nestingPath ) ;
177181 }
178182 }
179183 break ;
180184
181185 case 'connectOrCreate' :
182- context . nestingPath . push ( { field, model, where : data . where , unique : false } ) ;
183186 for ( const item of enumerate ( data ) ) {
187+ const newContext = pushNewContext ( field , model , item . where ) ;
184188 let callbackResult : any ;
185189 if ( this . callback . connectOrCreate ) {
186- callbackResult = await this . callback . connectOrCreate ( model , item , context ) ;
190+ callbackResult = await this . callback . connectOrCreate ( model , item , newContext ) ;
187191 }
188192 if ( callbackResult !== false ) {
189193 const subPayload = typeof callbackResult === 'object' ? callbackResult : item . create ;
190- await this . visitSubPayload ( model , action , subPayload , context . nestingPath ) ;
194+ await this . visitSubPayload ( model , action , subPayload , newContext . nestingPath ) ;
191195 }
192196 }
193197 break ;
194198
195199 case 'connect' :
196200 if ( this . callback . connect ) {
197201 for ( const item of enumerate ( data ) ) {
198- const newContext = {
199- ...context ,
200- nestingPath : [ ...context . nestingPath , { field, model, where : item , unique : true } ] ,
201- } ;
202+ const newContext = pushNewContext ( field , model , item , true ) ;
202203 await this . callback . connect ( model , item , newContext ) ;
203204 }
204205 }
@@ -210,31 +211,25 @@ export class NestedWriteVisitor {
210211 // if relation is to-one, the payload can only be boolean `true`
211212 if ( this . callback . disconnect ) {
212213 for ( const item of enumerate ( data ) ) {
213- const newContext = {
214- ...context ,
215- nestingPath : [
216- ...context . nestingPath ,
217- { field, model, where : item , unique : typeof item === 'object' } ,
218- ] ,
219- } ;
214+ const newContext = pushNewContext ( field , model , item , typeof item === 'object' ) ;
220215 await this . callback . disconnect ( model , item , newContext ) ;
221216 }
222217 }
223218 break ;
224219
225220 case 'set' :
226221 if ( this . callback . set ) {
227- context . nestingPath . push ( { field, model, where : { } , unique : false } ) ;
228- await this . callback . set ( model , data , context ) ;
222+ const newContext = pushNewContext ( field , model , { } ) ;
223+ await this . callback . set ( model , data , newContext ) ;
229224 }
230225 break ;
231226
232227 case 'update' :
233- context . nestingPath . push ( { field, model, where : data . where , unique : false } ) ;
234228 for ( const item of enumerate ( data ) ) {
229+ const newContext = pushNewContext ( field , model , item . where ) ;
235230 let callbackResult : any ;
236231 if ( this . callback . update ) {
237- callbackResult = await this . callback . update ( model , item , context ) ;
232+ callbackResult = await this . callback . update ( model , item , newContext ) ;
238233 }
239234 if ( callbackResult !== false ) {
240235 const subPayload =
@@ -243,38 +238,38 @@ export class NestedWriteVisitor {
243238 : typeof item . data === 'object'
244239 ? item . data
245240 : item ;
246- await this . visitSubPayload ( model , action , subPayload , context . nestingPath ) ;
241+ await this . visitSubPayload ( model , action , subPayload , newContext . nestingPath ) ;
247242 }
248243 }
249244 break ;
250245
251246 case 'updateMany' :
252- context . nestingPath . push ( { field, model, where : data . where , unique : false } ) ;
253247 for ( const item of enumerate ( data ) ) {
248+ const newContext = pushNewContext ( field , model , item . where ) ;
254249 let callbackResult : any ;
255250 if ( this . callback . updateMany ) {
256- callbackResult = await this . callback . updateMany ( model , item , context ) ;
251+ callbackResult = await this . callback . updateMany ( model , item , newContext ) ;
257252 }
258253 if ( callbackResult !== false ) {
259254 const subPayload = typeof callbackResult === 'object' ? callbackResult : item ;
260- await this . visitSubPayload ( model , action , subPayload , context . nestingPath ) ;
255+ await this . visitSubPayload ( model , action , subPayload , newContext . nestingPath ) ;
261256 }
262257 }
263258 break ;
264259
265260 case 'upsert' : {
266- context . nestingPath . push ( { field, model, where : data . where , unique : false } ) ;
267261 for ( const item of enumerate ( data ) ) {
262+ const newContext = pushNewContext ( field , model , item . where ) ;
268263 let callbackResult : any ;
269264 if ( this . callback . upsert ) {
270- callbackResult = await this . callback . upsert ( model , item , context ) ;
265+ callbackResult = await this . callback . upsert ( model , item , newContext ) ;
271266 }
272267 if ( callbackResult !== false ) {
273268 if ( typeof callbackResult === 'object' ) {
274- await this . visitSubPayload ( model , action , callbackResult , context . nestingPath ) ;
269+ await this . visitSubPayload ( model , action , callbackResult , newContext . nestingPath ) ;
275270 } else {
276- await this . visitSubPayload ( model , action , item . create , context . nestingPath ) ;
277- await this . visitSubPayload ( model , action , item . update , context . nestingPath ) ;
271+ await this . visitSubPayload ( model , action , item . create , newContext . nestingPath ) ;
272+ await this . visitSubPayload ( model , action , item . update , newContext . nestingPath ) ;
278273 }
279274 }
280275 }
@@ -284,13 +279,7 @@ export class NestedWriteVisitor {
284279 case 'delete' : {
285280 if ( this . callback . delete ) {
286281 for ( const item of enumerate ( data ) ) {
287- const newContext = {
288- ...context ,
289- nestingPath : [
290- ...context . nestingPath ,
291- { field, model, where : toplevel ? item . where : item , unique : false } ,
292- ] ,
293- } ;
282+ const newContext = pushNewContext ( field , model , toplevel ? item . where : item ) ;
294283 await this . callback . delete ( model , item , newContext ) ;
295284 }
296285 }
@@ -300,13 +289,7 @@ export class NestedWriteVisitor {
300289 case 'deleteMany' :
301290 if ( this . callback . deleteMany ) {
302291 for ( const item of enumerate ( data ) ) {
303- const newContext = {
304- ...context ,
305- nestingPath : [
306- ...context . nestingPath ,
307- { field, model, where : toplevel ? item . where : item , unique : false } ,
308- ] ,
309- } ;
292+ const newContext = pushNewContext ( field , model , toplevel ? item . where : item ) ;
310293 await this . callback . deleteMany ( model , item , newContext ) ;
311294 }
312295 }
0 commit comments