Skip to content

Adds examples for usage of beforeFind #376

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

Merged
merged 3 commits into from
Dec 20, 2016
Merged
Changes from all 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
54 changes: 54 additions & 0 deletions _includes/cloudcode/cloud-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,57 @@ The `afterDelete` handler can access the object that was deleted through `reques
The client will receive a successful response to the delete request after the handler terminates, regardless of how it terminates. For instance, the client will receive a successful response even if the handler throws an exception. Any errors that occurred while running the handler can be found in the Cloud Code log.

If you want to use `afterDelete` for a predefined class in the Parse JavaScript SDK (e.g. [Parse.User](https://parse.com/docs/js/api/symbols/Parse.User.html)), you should not pass a String for the first argument. Instead, you should pass the class itself.

# beforeFind Triggers

*Available only on parse-server cloud code starting 2.2.20*

In some cases you may want to transform an incoming query, adding an additional limit or increasing the default limit, adding extra includes or restrict the results to a subset of keys. You can do so with the `beforeFind` trigger.


## Examples

```javascript
// Properties available
Parse.Cloud.beforeFind('MyObject', function(req) {
let query = req.query; // the Parse.Query
let user = req.user; // the user
let triggerName = req.triggerName; // beforeFind
let isMaster = req.master; // if the query is run with masterKey
let logger = req.log; // the logger
let installationId = req.installationId; // The installationId
});

// Selecting keys
Parse.Cloud.beforeFind('MyObject', function(req) {
let query = req.query; // the Parse.Query
// Force the selection on some keys
query.select(['key1', 'key2']);
});

// Asynchronous support
Parse.Cloud.beforeFind('MyObject', function(req) {
let query = req.query;
return aPromise().then((results) => {
// do something with the results
query.containedIn('key', results);
});
});

// Returning a different query
Parse.Cloud.beforeFind('MyObject', function(req) {
let query = req.query;
let otherQuery = new Parse.Query('MyObject');
otherQuery.equalTo('key', 'value');
return Parse.Query.or(query, otherQuery);
});

// Rejecting a query
Parse.Cloud.beforeFind('MyObject', function(req) {
// throw an error
throw new Parse.Error(101, 'error');

// rejecting promise
return Promise.reject('error');
});
```