Skip to content

Commit 6f65ec5

Browse files
committed
feat: allow short circuit of beforeFind
1 parent 95da5d6 commit 6f65ec5

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

spec/CloudCode.spec.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,61 @@ describe('Cloud Code', () => {
201201
}
202202
});
203203

204+
it('beforeFind can short circuit', async () => {
205+
Parse.Cloud.beforeFind('beforeFind', () => {
206+
return new Parse.Object('TestObject', { foo: 'bar' });
207+
});
208+
Parse.Cloud.afterFind('beforeFind', () => {
209+
throw 'afterFind should not run';
210+
});
211+
const obj = new Parse.Object('beforeFind');
212+
await obj.save();
213+
const newObj = await new Parse.Query('beforeFind').first();
214+
expect(newObj.className).toBe('TestObject');
215+
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
216+
});
217+
218+
it('beforeFind can short circuit arrays', async () => {
219+
Parse.Cloud.beforeFind('beforeFind', () => {
220+
return [new Parse.Object('TestObject', { foo: 'bar' })];
221+
});
222+
Parse.Cloud.afterFind('beforeFind', () => {
223+
throw 'afterFind should not run';
224+
});
225+
const obj = new Parse.Object('beforeFind');
226+
await obj.save();
227+
const newObj = await new Parse.Query('beforeFind').first();
228+
expect(newObj.className).toBe('TestObject');
229+
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
230+
});
231+
232+
it('beforeFind can short circuit get', async () => {
233+
Parse.Cloud.beforeFind('beforeFind', () => {
234+
return [new Parse.Object('TestObject', { foo: 'bar' })];
235+
});
236+
Parse.Cloud.afterFind('beforeFind', () => {
237+
throw 'afterFind should not run';
238+
});
239+
const obj = new Parse.Object('beforeFind');
240+
await obj.save();
241+
const newObj = await new Parse.Query('beforeFind').get(obj.id);
242+
expect(newObj.className).toBe('TestObject');
243+
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
244+
});
245+
246+
it('beforeFind can short circuit empty array', async () => {
247+
Parse.Cloud.beforeFind('beforeFind', () => {
248+
return [];
249+
});
250+
Parse.Cloud.afterFind('beforeFind', () => {
251+
throw 'afterFind should not run';
252+
});
253+
const obj = new Parse.Object('beforeFind');
254+
await obj.save();
255+
const newObj = await new Parse.Query('beforeFind').first();
256+
expect(newObj).toBeUndefined();
257+
});
258+
204259
it('beforeSave rejection with custom error code', function (done) {
205260
Parse.Cloud.beforeSave('BeforeSaveFailWithErrorCode', function () {
206261
throw new Parse.Error(999, 'Nope');

src/rest.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ function find(config, auth, className, restWhere, restOptions, clientSDK, contex
3939
.then(result => {
4040
restWhere = result.restWhere || restWhere;
4141
restOptions = result.restOptions || restOptions;
42+
if (result?.objects) {
43+
return {
44+
results: result.objects.map(row => row._toFullJSON()),
45+
};
46+
}
4247
const query = new RestQuery(
4348
config,
4449
auth,
@@ -71,6 +76,11 @@ const get = (config, auth, className, objectId, restOptions, clientSDK, context)
7176
.then(result => {
7277
restWhere = result.restWhere || restWhere;
7378
restOptions = result.restOptions || restOptions;
79+
if (result?.objects) {
80+
return {
81+
results: result.objects.map(row => row._toFullJSON()),
82+
};
83+
}
7484
const query = new RestQuery(
7585
config,
7686
auth,

src/triggers.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,19 @@ export function maybeRunQueryTrigger(
586586
restOptions = restOptions || {};
587587
restOptions.subqueryReadPreference = requestObject.subqueryReadPreference;
588588
}
589+
let objects = undefined;
590+
if (result instanceof Parse.Object) {
591+
objects = [result];
592+
} else if (
593+
Array.isArray(result) &&
594+
(!result.length || result.some(obj => obj instanceof Parse.Object))
595+
) {
596+
objects = result;
597+
}
589598
return {
590599
restWhere,
591600
restOptions,
601+
objects,
592602
};
593603
},
594604
err => {

0 commit comments

Comments
 (0)