Skip to content

Commit 095164b

Browse files
authored
Prevent afterFind with saving objects (#6127)
Fixes: #6088
1 parent 45bf4e6 commit 095164b

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

spec/CloudCode.spec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2450,4 +2450,58 @@ describe('beforeLogin hook', () => {
24502450
await Parse.User.logIn('tupac', 'shakur');
24512451
done();
24522452
});
2453+
2454+
it('afterFind should not be triggered when saving an object', async () => {
2455+
let beforeSaves = 0;
2456+
Parse.Cloud.beforeSave('SavingTest', () => {
2457+
beforeSaves++;
2458+
});
2459+
2460+
let afterSaves = 0;
2461+
Parse.Cloud.afterSave('SavingTest', () => {
2462+
afterSaves++;
2463+
});
2464+
2465+
let beforeFinds = 0;
2466+
Parse.Cloud.beforeFind('SavingTest', () => {
2467+
beforeFinds++;
2468+
});
2469+
2470+
let afterFinds = 0;
2471+
Parse.Cloud.afterFind('SavingTest', () => {
2472+
afterFinds++;
2473+
});
2474+
2475+
const obj = new Parse.Object('SavingTest');
2476+
obj.set('someField', 'some value 1');
2477+
await obj.save();
2478+
2479+
expect(beforeSaves).toEqual(1);
2480+
expect(afterSaves).toEqual(1);
2481+
expect(beforeFinds).toEqual(0);
2482+
expect(afterFinds).toEqual(0);
2483+
2484+
obj.set('someField', 'some value 2');
2485+
await obj.save();
2486+
2487+
expect(beforeSaves).toEqual(2);
2488+
expect(afterSaves).toEqual(2);
2489+
expect(beforeFinds).toEqual(0);
2490+
expect(afterFinds).toEqual(0);
2491+
2492+
await obj.fetch();
2493+
2494+
expect(beforeSaves).toEqual(2);
2495+
expect(afterSaves).toEqual(2);
2496+
expect(beforeFinds).toEqual(1);
2497+
expect(afterFinds).toEqual(1);
2498+
2499+
obj.set('someField', 'some value 3');
2500+
await obj.save();
2501+
2502+
expect(beforeSaves).toEqual(3);
2503+
expect(afterSaves).toEqual(3);
2504+
expect(beforeFinds).toEqual(1);
2505+
expect(afterFinds).toEqual(1);
2506+
});
24532507
});

src/RestQuery.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ function RestQuery(
2424
className,
2525
restWhere = {},
2626
restOptions = {},
27-
clientSDK
27+
clientSDK,
28+
runAfterFind = true
2829
) {
2930
this.config = config;
3031
this.auth = auth;
3132
this.className = className;
3233
this.restWhere = restWhere;
3334
this.restOptions = restOptions;
3435
this.clientSDK = clientSDK;
36+
this.runAfterFind = runAfterFind;
3537
this.response = null;
3638
this.findOptions = {};
3739

@@ -774,6 +776,9 @@ RestQuery.prototype.runAfterFindTrigger = function() {
774776
if (!this.response) {
775777
return;
776778
}
779+
if (!this.runAfterFind) {
780+
return;
781+
}
777782
// Avoid doing any setup for triggers if there is no 'afterFind' trigger for this class.
778783
const hasAfterFindHook = triggers.triggerExists(
779784
this.className,

src/rest.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,15 @@ function update(config, auth, className, restWhere, restObject, clientSDK) {
225225
const hasLiveQuery = checkLiveQuery(className, config);
226226
if (hasTriggers || hasLiveQuery) {
227227
// Do not use find, as it runs the before finds
228-
return new RestQuery(config, auth, className, restWhere).execute({
228+
return new RestQuery(
229+
config,
230+
auth,
231+
className,
232+
restWhere,
233+
undefined,
234+
undefined,
235+
false
236+
).execute({
229237
op: 'update',
230238
});
231239
}

0 commit comments

Comments
 (0)