Skip to content

Commit

Permalink
Prevent afterFind with saving objects (parse-community#6127)
Browse files Browse the repository at this point in the history
  • Loading branch information
dplewis authored Oct 15, 2019
1 parent 0a5134c commit fa88041
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
54 changes: 54 additions & 0 deletions spec/CloudCode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2450,4 +2450,58 @@ describe('beforeLogin hook', () => {
await Parse.User.logIn('tupac', 'shakur');
done();
});

it('afterFind should not be triggered when saving an object', async () => {
let beforeSaves = 0;
Parse.Cloud.beforeSave('SavingTest', () => {
beforeSaves++;
});

let afterSaves = 0;
Parse.Cloud.afterSave('SavingTest', () => {
afterSaves++;
});

let beforeFinds = 0;
Parse.Cloud.beforeFind('SavingTest', () => {
beforeFinds++;
});

let afterFinds = 0;
Parse.Cloud.afterFind('SavingTest', () => {
afterFinds++;
});

const obj = new Parse.Object('SavingTest');
obj.set('someField', 'some value 1');
await obj.save();

expect(beforeSaves).toEqual(1);
expect(afterSaves).toEqual(1);
expect(beforeFinds).toEqual(0);
expect(afterFinds).toEqual(0);

obj.set('someField', 'some value 2');
await obj.save();

expect(beforeSaves).toEqual(2);
expect(afterSaves).toEqual(2);
expect(beforeFinds).toEqual(0);
expect(afterFinds).toEqual(0);

await obj.fetch();

expect(beforeSaves).toEqual(2);
expect(afterSaves).toEqual(2);
expect(beforeFinds).toEqual(1);
expect(afterFinds).toEqual(1);

obj.set('someField', 'some value 3');
await obj.save();

expect(beforeSaves).toEqual(3);
expect(afterSaves).toEqual(3);
expect(beforeFinds).toEqual(1);
expect(afterFinds).toEqual(1);
});
});
7 changes: 6 additions & 1 deletion src/RestQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ function RestQuery(
className,
restWhere = {},
restOptions = {},
clientSDK
clientSDK,
runAfterFind = true
) {
this.config = config;
this.auth = auth;
this.className = className;
this.restWhere = restWhere;
this.restOptions = restOptions;
this.clientSDK = clientSDK;
this.runAfterFind = runAfterFind;
this.response = null;
this.findOptions = {};

Expand Down Expand Up @@ -774,6 +776,9 @@ RestQuery.prototype.runAfterFindTrigger = function() {
if (!this.response) {
return;
}
if (!this.runAfterFind) {
return;
}
// Avoid doing any setup for triggers if there is no 'afterFind' trigger for this class.
const hasAfterFindHook = triggers.triggerExists(
this.className,
Expand Down
10 changes: 9 additions & 1 deletion src/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,15 @@ function update(config, auth, className, restWhere, restObject, clientSDK) {
const hasLiveQuery = checkLiveQuery(className, config);
if (hasTriggers || hasLiveQuery) {
// Do not use find, as it runs the before finds
return new RestQuery(config, auth, className, restWhere).execute({
return new RestQuery(
config,
auth,
className,
restWhere,
undefined,
undefined,
false
).execute({
op: 'update',
});
}
Expand Down

0 comments on commit fa88041

Please sign in to comment.