Skip to content

Commit bd64ec8

Browse files
dblythyTomWFox
andauthored
afterLiveQueryEvent Triggers (parse-community#773)
* 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 <[email protected]>
1 parent ca9e2f3 commit bd64ec8

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

_includes/cloudcode/cloud-code.md

+62
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,68 @@ Parse.Cloud.beforeSubscribe('MyObject', request => {
636636
});
637637
```
638638
639+
## afterLiveQueryEvent
640+
641+
*Available only on parse-server cloud code starting 4.4.0*
642+
643+
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.
644+
645+
### Examples
646+
647+
```javascript
648+
// Changing values on object and original
649+
Parse.Cloud.afterLiveQueryEvent('MyObject', request => {
650+
const object = request.object;
651+
object.set('name', '***');
652+
653+
const original = request.original;
654+
original.set('name', 'yolo');
655+
});
656+
657+
// Prevent LiveQuery trigger unless 'foo' is modified
658+
Parse.Cloud.afterLiveQueryEvent('MyObject', (request) => {
659+
const object = request.object;
660+
const original = request.original;
661+
if (!original) {
662+
return;
663+
}
664+
if (object.get('foo') != original.get('foo')) {
665+
request.sendEvent = false;
666+
}
667+
});
668+
```
669+
670+
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`.
671+
672+
```javascript
673+
// Including an object on LiveQuery event, on update only.
674+
Parse.Cloud.afterLiveQueryEvent('MyObject', async (request) => {
675+
if (request.event != "update") {
676+
request.sendEvent = false;
677+
return;
678+
}
679+
const object = request.object;
680+
const pointer = object.get("child");
681+
await pointer.fetch();
682+
});
683+
684+
// Extend matchesQuery functionality to LiveQuery
685+
Parse.Cloud.afterLiveQueryEvent('MyObject', async (request) => {
686+
if (request.event != "Create") {
687+
return;
688+
}
689+
const query = request.object.relation('children').query();
690+
query.equalTo('foo','bart');
691+
const first = await query.first();
692+
if (!first) {
693+
request.sendEvent = false;
694+
}
695+
});
696+
```
697+
698+
### Some considerations to be aware of
699+
- 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.
700+
639701
## onLiveQueryEvent
640702
641703
*Available only on parse-server cloud code starting 2.6.2*

0 commit comments

Comments
 (0)