diff --git a/_includes/cloudcode/cloud-code.md b/_includes/cloudcode/cloud-code.md index 83129f7e8..211bdc716 100644 --- a/_includes/cloudcode/cloud-code.md +++ b/_includes/cloudcode/cloud-code.md @@ -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*