File tree 3 files changed +34
-1
lines changed
3 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -231,7 +231,10 @@ class GraphPatchAction extends GraphAction {
231
231
}
232
232
233
233
_createRootBuilder ( node ) {
234
- return node . obj . $query ( ) ;
234
+ const currentNode = this . currentGraph . nodeForNode ( node ) ;
235
+ const currentObj = currentNode && currentNode . obj ;
236
+
237
+ return currentObj ? currentObj . $query ( ) : node . obj . $query ( ) ;
235
238
}
236
239
}
237
240
Original file line number Diff line number Diff line change @@ -11,6 +11,10 @@ class DelegateOperation extends QueryBuilderOperation {
11
11
this . delegate = opt . delegate ;
12
12
}
13
13
14
+ get modelOptions ( ) {
15
+ return this . delegate . modelOptions ;
16
+ }
17
+
14
18
is ( OperationClass ) {
15
19
return super . is ( OperationClass ) || this . delegate . is ( OperationClass ) ;
16
20
}
Original file line number Diff line number Diff line change @@ -1163,6 +1163,32 @@ module.exports = (session) => {
1163
1163
expect ( queries . length ) . to . equal ( 4 ) ;
1164
1164
} ) ;
1165
1165
1166
+ it ( 'should be able to access modelOptions in beforeUpdate when using patchAndFetchById' , async ( ) => {
1167
+ Movie . beforeUpdate = createHookSpy ( ( { modelOptions } ) => {
1168
+ chaiExpect ( modelOptions ) . to . deep . equal ( { patch : true } ) ;
1169
+ } ) ;
1170
+
1171
+ const hungerGames = await Movie . query ( ) . findOne ( 'name' , 'like' , '%gam%' ) ;
1172
+ expect ( queries . length ) . to . equal ( 1 ) ;
1173
+
1174
+ await Movie . query ( ) . patchAndFetchById ( hungerGames . id , { name : 'Updated' } ) ;
1175
+ expect ( Movie . beforeUpdate . calls . length ) . to . equal ( 1 ) ;
1176
+ } ) ;
1177
+
1178
+ it ( 'should populate modelOptions with old data when using upsertGraph' , async ( ) => {
1179
+ Movie . beforeUpdate = createHookSpy ( ( { modelOptions } ) => {
1180
+ expect ( modelOptions ) . to . have . property ( 'old' ) ;
1181
+
1182
+ chaiExpect ( modelOptions . old ) . containSubset ( { name : 'Hungergames' } ) ;
1183
+ } ) ;
1184
+
1185
+ const hungerGames = await Movie . query ( ) . findOne ( 'name' , 'like' , '%gam%' ) ;
1186
+ expect ( queries . length ) . to . equal ( 1 ) ;
1187
+
1188
+ await Movie . query ( ) . upsertGraph ( { id : hungerGames . id , name : 'Updated' } ) ;
1189
+ expect ( Movie . beforeUpdate . calls . length ) . to . equal ( 1 ) ;
1190
+ } ) ;
1191
+
1166
1192
it ( 'should be able to cancel the query' , ( ) => {
1167
1193
Movie . beforeUpdate = createHookSpy ( ( { cancelQuery } ) => {
1168
1194
cancelQuery ( ) ;
You can’t perform that action at this time.
0 commit comments