Skip to content

Commit

Permalink
modelOptions in DelegateOperation should forward to the one from...
Browse files Browse the repository at this point in the history
the delegated operation
- to make `modelOptions` always available in static query hooks, also when using upsertGraph(AndFetch) and patchAndFetchById
- also fix the query builder being executed on the model built from a graph instead of the source(current) model - the modelOptions.old should contain the old values read from db, not the new ones from the graph.
  • Loading branch information
falkenhawk committed Dec 16, 2024
1 parent ff025a6 commit ad9c72c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/queryBuilder/graph/patch/GraphPatchAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ class GraphPatchAction extends GraphAction {
}

_createRootBuilder(node) {
return node.obj.$query();
const currentNode = this.currentGraph.nodeForNode(node);
const currentObj = currentNode && currentNode.obj;

return currentObj ? currentObj.$query() : node.obj.$query();
}
}

Expand Down
4 changes: 4 additions & 0 deletions lib/queryBuilder/operations/DelegateOperation.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class DelegateOperation extends QueryBuilderOperation {
this.delegate = opt.delegate;
}

get modelOptions() {
return this.delegate.modelOptions;
}

is(OperationClass) {
return super.is(OperationClass) || this.delegate.is(OperationClass);
}
Expand Down
26 changes: 26 additions & 0 deletions tests/integration/staticHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,32 @@ module.exports = (session) => {
expect(queries.length).to.equal(4);
});

it('should be able to access modelOptions in beforeUpdate when using patchAndFetchById', async () => {
Movie.beforeUpdate = createHookSpy(({ modelOptions }) => {
chaiExpect(modelOptions).to.deep.equal({ patch: true });
});

const hungerGames = await Movie.query().findOne('name', 'like', '%gam%');
expect(queries.length).to.equal(1);

await Movie.query().patchAndFetchById(hungerGames.id, { name: 'Updated' });
expect(Movie.beforeUpdate.calls.length).to.equal(1);
});

it('should populate modelOptions with old data when using upsertGraph', async () => {
Movie.beforeUpdate = createHookSpy(({ modelOptions }) => {
expect(modelOptions).to.have.property('old');

chaiExpect(modelOptions.old).containSubset({ name: 'Hungergames' });
});

const hungerGames = await Movie.query().findOne('name', 'like', '%gam%');
expect(queries.length).to.equal(1);

await Movie.query().upsertGraph({ id: hungerGames.id, name: 'Updated' });
expect(Movie.beforeUpdate.calls.length).to.equal(1);
});

it('should be able to cancel the query', () => {
Movie.beforeUpdate = createHookSpy(({ cancelQuery }) => {
cancelQuery();
Expand Down

0 comments on commit ad9c72c

Please sign in to comment.