Skip to content

Commit 8abe07c

Browse files
authored
Merge pull request #18 from ovos/fix-delegate-model-options-local
static query hooks fixes: `modelOptions` in DelegateOperation should forward to the one from the delegated operation, resolve `modelOptions.old` properly for graph operations
2 parents ff025a6 + ad9c72c commit 8abe07c

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

lib/queryBuilder/graph/patch/GraphPatchAction.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,10 @@ class GraphPatchAction extends GraphAction {
231231
}
232232

233233
_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();
235238
}
236239
}
237240

lib/queryBuilder/operations/DelegateOperation.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ class DelegateOperation extends QueryBuilderOperation {
1111
this.delegate = opt.delegate;
1212
}
1313

14+
get modelOptions() {
15+
return this.delegate.modelOptions;
16+
}
17+
1418
is(OperationClass) {
1519
return super.is(OperationClass) || this.delegate.is(OperationClass);
1620
}

tests/integration/staticHooks.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,32 @@ module.exports = (session) => {
11631163
expect(queries.length).to.equal(4);
11641164
});
11651165

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+
11661192
it('should be able to cancel the query', () => {
11671193
Movie.beforeUpdate = createHookSpy(({ cancelQuery }) => {
11681194
cancelQuery();

0 commit comments

Comments
 (0)