From 77f0fb1e52d67235be2dc4c433f2d7b2f2f2a48e Mon Sep 17 00:00:00 2001 From: dblythy Date: Tue, 20 Oct 2020 14:54:04 +1100 Subject: [PATCH 1/7] afterLiveQueryEvent --- _includes/cloudcode/cloud-code.md | 45 +++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/_includes/cloudcode/cloud-code.md b/_includes/cloudcode/cloud-code.md index 83129f7e8..888be5a9e 100644 --- a/_includes/cloudcode/cloud-code.md +++ b/_includes/cloudcode/cloud-code.md @@ -636,6 +636,51 @@ Parse.Cloud.beforeSubscribe('MyObject', request => { }); ``` +##afterLiveQueryEvent + +*Available only on parse-server cloud code starting 4.-.-* + +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'); +}); + +// 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(); +}); + +// 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')) { + req.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* From 33480144daccd2c71c4a7118c1db13897f703f8f Mon Sep 17 00:00:00 2001 From: dblythy Date: Tue, 20 Oct 2020 14:54:47 +1100 Subject: [PATCH 2/7] Update cloud-code.md --- _includes/cloudcode/cloud-code.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_includes/cloudcode/cloud-code.md b/_includes/cloudcode/cloud-code.md index 888be5a9e..688a95e36 100644 --- a/_includes/cloudcode/cloud-code.md +++ b/_includes/cloudcode/cloud-code.md @@ -636,7 +636,7 @@ Parse.Cloud.beforeSubscribe('MyObject', request => { }); ``` -##afterLiveQueryEvent +## afterLiveQueryEvent *Available only on parse-server cloud code starting 4.-.-* From a2947a8733a593ad5f5efc836d57f53b0979f003 Mon Sep 17 00:00:00 2001 From: dblythy Date: Tue, 20 Oct 2020 15:02:28 +1100 Subject: [PATCH 3/7] Update cloud-code.md --- _includes/cloudcode/cloud-code.md | 33 +++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/_includes/cloudcode/cloud-code.md b/_includes/cloudcode/cloud-code.md index 688a95e36..ec0d66540 100644 --- a/_includes/cloudcode/cloud-code.md +++ b/_includes/cloudcode/cloud-code.md @@ -654,26 +654,39 @@ Parse.Cloud.afterLiveQueryEvent('MyObject', request => { 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')) { + req.sendEvent = false; + } +}); + // Including an object on LiveQuery event, on update only. -Parse.Cloud.afterLiveQueryEvent('MyObject', async request => { - if (request.event != 'update) { +Parse.Cloud.afterLiveQueryEvent('MyObject', async (request) => { + if (request.event != "update") { request.sendEvent = false; return; } const object = request.object; - const pointer = object.get('child'); + const pointer = object.get("child"); await pointer.fetch(); }); -// Prevent LiveQuery trigger unless 'foo' is modified -Parse.Cloud.afterLiveQueryEvent('MyObject', request => { - const object = request.object; - const original = request.original; - if (!original) { +//Extend matchesQuery functionality to LiveQuery +Parse.Cloud.afterLiveQueryEvent('MyObject', async (request) => { + if (req.event != "Create") { return; } - if (object.get('foo') != original.get('foo')) { - req.sendEvent = false; + const query = request.object.relation('children').query(); + query.equalTo('foo','bart'); + const first = await query.first(); + if (!first) { + request.sendEvent = false; } }); ``` From c1ffc04d053cd7db95cf905f592d51d22fe5e3b3 Mon Sep 17 00:00:00 2001 From: dblythy Date: Tue, 20 Oct 2020 15:03:52 +1100 Subject: [PATCH 4/7] Update cloud-code.md --- _includes/cloudcode/cloud-code.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_includes/cloudcode/cloud-code.md b/_includes/cloudcode/cloud-code.md index ec0d66540..9e52c235b 100644 --- a/_includes/cloudcode/cloud-code.md +++ b/_includes/cloudcode/cloud-code.md @@ -662,7 +662,7 @@ Parse.Cloud.afterLiveQueryEvent('MyObject', (request) => { return; } if (object.get('foo') != original.get('foo')) { - req.sendEvent = false; + request.sendEvent = false; } }); @@ -679,7 +679,7 @@ Parse.Cloud.afterLiveQueryEvent('MyObject', async (request) => { //Extend matchesQuery functionality to LiveQuery Parse.Cloud.afterLiveQueryEvent('MyObject', async (request) => { - if (req.event != "Create") { + if (request.event != "Create") { return; } const query = request.object.relation('children').query(); From 5f87d27b4b859105e16dc104ebf5af853ccdecf8 Mon Sep 17 00:00:00 2001 From: Tom Fox <13188249+TomWFox@users.noreply.github.com> Date: Sat, 24 Oct 2020 12:20:31 +0100 Subject: [PATCH 5/7] Apply suggestions from code review --- _includes/cloudcode/cloud-code.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_includes/cloudcode/cloud-code.md b/_includes/cloudcode/cloud-code.md index 9e52c235b..7122f4208 100644 --- a/_includes/cloudcode/cloud-code.md +++ b/_includes/cloudcode/cloud-code.md @@ -654,7 +654,7 @@ Parse.Cloud.afterLiveQueryEvent('MyObject', request => { original.set('name', 'yolo'); }); -// Prevent LiveQuery trigger unless 'foo' is modified +// Prevent LiveQuery trigger unless 'foo' is modified Parse.Cloud.afterLiveQueryEvent('MyObject', (request) => { const object = request.object; const original = request.original; @@ -677,7 +677,7 @@ Parse.Cloud.afterLiveQueryEvent('MyObject', async (request) => { await pointer.fetch(); }); -//Extend matchesQuery functionality to LiveQuery +// Extend matchesQuery functionality to LiveQuery Parse.Cloud.afterLiveQueryEvent('MyObject', async (request) => { if (request.event != "Create") { return; From 08f7eda4326a5998ce27172772989d73bee3ad4f Mon Sep 17 00:00:00 2001 From: dblythy Date: Sun, 25 Oct 2020 18:37:16 +1100 Subject: [PATCH 6/7] Context around database operations with LiveQuery --- _includes/cloudcode/cloud-code.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/_includes/cloudcode/cloud-code.md b/_includes/cloudcode/cloud-code.md index 7122f4208..f287d8c54 100644 --- a/_includes/cloudcode/cloud-code.md +++ b/_includes/cloudcode/cloud-code.md @@ -665,7 +665,11 @@ Parse.Cloud.afterLiveQueryEvent('MyObject', (request) => { 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") { From 17ffad53bcf46f320bfe74f8bf3f90bd27ef87e5 Mon Sep 17 00:00:00 2001 From: Tom Fox <13188249+TomWFox@users.noreply.github.com> Date: Mon, 2 Nov 2020 17:15:16 +0000 Subject: [PATCH 7/7] add parse server version --- _includes/cloudcode/cloud-code.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_includes/cloudcode/cloud-code.md b/_includes/cloudcode/cloud-code.md index f287d8c54..211bdc716 100644 --- a/_includes/cloudcode/cloud-code.md +++ b/_includes/cloudcode/cloud-code.md @@ -638,7 +638,7 @@ Parse.Cloud.beforeSubscribe('MyObject', request => { ## afterLiveQueryEvent -*Available only on parse-server cloud code starting 4.-.-* +*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.