Skip to content

Commit

Permalink
afterLiveQueryEvent Triggers (#773)
Browse files Browse the repository at this point in the history
* afterLiveQueryEvent

* Update cloud-code.md

* Update cloud-code.md

* Update cloud-code.md

* Apply suggestions from code review

* Context around database operations with LiveQuery

* add parse server version

Co-authored-by: Tom Fox <13188249+TomWFox@users.noreply.github.com>
  • Loading branch information
dblythy and TomWFox authored Nov 2, 2020
1 parent ca9e2f3 commit bd64ec8
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions _includes/cloudcode/cloud-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,68 @@ Parse.Cloud.beforeSubscribe('MyObject', request => {
});
```
## afterLiveQueryEvent
*Available only on parse-server cloud code starting 4.4.0*
In some cases you may want to manipulate the results of a Live Query before they are sent to the client. You can do so with the `afterLiveQueryEvent` trigger.
### Examples
```javascript
// Changing values on object and original
Parse.Cloud.afterLiveQueryEvent('MyObject', request => {
const object = request.object;
object.set('name', '***');

const original = request.original;
original.set('name', 'yolo');
});

// Prevent LiveQuery trigger unless 'foo' is modified
Parse.Cloud.afterLiveQueryEvent('MyObject', (request) => {
const object = request.object;
const original = request.original;
if (!original) {
return;
}
if (object.get('foo') != original.get('foo')) {
request.sendEvent = false;
}
});
```
By default, ParseLiveQuery does not perform queries that require additional database operations. This is to keep your Parse Server as fast and effient as possible. If you require this functionality, you can perform these in `afterLiveQueryEvent`.
```javascript
// Including an object on LiveQuery event, on update only.
Parse.Cloud.afterLiveQueryEvent('MyObject', async (request) => {
if (request.event != "update") {
request.sendEvent = false;
return;
}
const object = request.object;
const pointer = object.get("child");
await pointer.fetch();
});

// Extend matchesQuery functionality to LiveQuery
Parse.Cloud.afterLiveQueryEvent('MyObject', async (request) => {
if (request.event != "Create") {
return;
}
const query = request.object.relation('children').query();
query.equalTo('foo','bart');
const first = await query.first();
if (!first) {
request.sendEvent = false;
}
});
```
### Some considerations to be aware of
- Live Query events won't trigger until the `afterLiveQueryEvent` trigger has completed. Make sure any functions inside the trigger are efficient and restrictive to prevent bottlenecks.
## onLiveQueryEvent
*Available only on parse-server cloud code starting 2.6.2*
Expand Down

0 comments on commit bd64ec8

Please sign in to comment.