Skip to content

Commit 6b2de6c

Browse files
authored
Merge 21c9289 into 95da5d6
2 parents 95da5d6 + 21c9289 commit 6b2de6c

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

spec/CloudCode.spec.js

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

204+
it('beforeFind can return object without DB operation', 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 newObj = await new Parse.Query('beforeFind').first();
212+
expect(newObj.className).toBe('TestObject');
213+
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
214+
await newObj.save();
215+
});
216+
217+
it('beforeFind can return array of objects without DB operation', async () => {
218+
Parse.Cloud.beforeFind('beforeFind', () => {
219+
return [new Parse.Object('TestObject', { foo: 'bar' })];
220+
});
221+
Parse.Cloud.afterFind('beforeFind', () => {
222+
throw 'afterFind should not run';
223+
});
224+
const newObj = await new Parse.Query('beforeFind').first();
225+
expect(newObj.className).toBe('TestObject');
226+
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
227+
await newObj.save();
228+
});
229+
230+
it('beforeFind can return object for get query without DB operation', async () => {
231+
Parse.Cloud.beforeFind('beforeFind', () => {
232+
return [new Parse.Object('TestObject', { foo: 'bar' })];
233+
});
234+
Parse.Cloud.afterFind('beforeFind', () => {
235+
throw 'afterFind should not run';
236+
});
237+
const newObj = await new Parse.Query('beforeFind').get('objId');
238+
expect(newObj.className).toBe('TestObject');
239+
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
240+
await newObj.save();
241+
});
242+
243+
it('beforeFind can return empty array without DB operation', async () => {
244+
Parse.Cloud.beforeFind('beforeFind', () => {
245+
return [];
246+
});
247+
Parse.Cloud.afterFind('beforeFind', () => {
248+
throw 'afterFind should not run';
249+
});
250+
const obj = new Parse.Object('beforeFind');
251+
await obj.save();
252+
const newObj = await new Parse.Query('beforeFind').first();
253+
expect(newObj).toBeUndefined();
254+
});
255+
204256
it('beforeSave rejection with custom error code', function (done) {
205257
Parse.Cloud.beforeSave('BeforeSaveFailWithErrorCode', function () {
206258
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)