Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow short circuit of beforeFind #8694

Open
wants to merge 7 commits into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions spec/CloudCode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,61 @@ describe('Cloud Code', () => {
}
});

it('beforeFind can return object without DB operation', async () => {
Parse.Cloud.beforeFind('beforeFind', () => {
return new Parse.Object('TestObject', { foo: 'bar' });
});
Parse.Cloud.afterFind('beforeFind', () => {
throw 'afterFind should not run';
});
const obj = new Parse.Object('beforeFind');
await obj.save();
const newObj = await new Parse.Query('beforeFind').first();
expect(newObj.className).toBe('TestObject');
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
});

it('beforeFind can return array of objects without DB operation', async () => {
Parse.Cloud.beforeFind('beforeFind', () => {
return [new Parse.Object('TestObject', { foo: 'bar' })];
});
Parse.Cloud.afterFind('beforeFind', () => {
throw 'afterFind should not run';
});
const obj = new Parse.Object('beforeFind');
await obj.save();
const newObj = await new Parse.Query('beforeFind').first();
expect(newObj.className).toBe('TestObject');
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
});

it('beforeFind can return object for get query without DB operation', async () => {
Parse.Cloud.beforeFind('beforeFind', () => {
return [new Parse.Object('TestObject', { foo: 'bar' })];
});
Parse.Cloud.afterFind('beforeFind', () => {
throw 'afterFind should not run';
});
const obj = new Parse.Object('beforeFind');
await obj.save();
const newObj = await new Parse.Query('beforeFind').get(obj.id);
expect(newObj.className).toBe('TestObject');
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
});

it('beforeFind can return empty array without DB operation', async () => {
Parse.Cloud.beforeFind('beforeFind', () => {
return [];
});
Parse.Cloud.afterFind('beforeFind', () => {
throw 'afterFind should not run';
});
const obj = new Parse.Object('beforeFind');
await obj.save();
const newObj = await new Parse.Query('beforeFind').first();
expect(newObj).toBeUndefined();
});

it('beforeSave rejection with custom error code', function (done) {
Parse.Cloud.beforeSave('BeforeSaveFailWithErrorCode', function () {
throw new Parse.Error(999, 'Nope');
Expand Down
10 changes: 10 additions & 0 deletions src/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ function find(config, auth, className, restWhere, restOptions, clientSDK, contex
.then(result => {
restWhere = result.restWhere || restWhere;
restOptions = result.restOptions || restOptions;
if (result?.objects) {
return {
results: result.objects.map(row => row._toFullJSON()),
};
}
const query = new RestQuery(
config,
auth,
Expand Down Expand Up @@ -71,6 +76,11 @@ const get = (config, auth, className, objectId, restOptions, clientSDK, context)
.then(result => {
restWhere = result.restWhere || restWhere;
restOptions = result.restOptions || restOptions;
if (result?.objects) {
return {
results: result.objects.map(row => row._toFullJSON()),
};
}
const query = new RestQuery(
config,
auth,
Expand Down
10 changes: 10 additions & 0 deletions src/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,9 +586,19 @@ export function maybeRunQueryTrigger(
restOptions = restOptions || {};
restOptions.subqueryReadPreference = requestObject.subqueryReadPreference;
}
let objects = undefined;
if (result instanceof Parse.Object) {
objects = [result];
} else if (
Array.isArray(result) &&
(!result.length || result.some(obj => obj instanceof Parse.Object))
) {
objects = result;
}
return {
restWhere,
restOptions,
objects,
};
},
err => {
Expand Down
Loading