Created
September 5, 2015 18:22
-
-
Save rorlig/ba2beec36a8a435f8f0f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Parse cloud function to execute query from multiple tables | |
* Useful in number of scenarios - activity streams, exporting data out etc... | |
* | |
*/ | |
Parse.Cloud.define("aggregateQueryFunction", function(request, response){ | |
// I have 5 ParseObjects - Diaper, Sleep, Milestone, Growth, Feed. | |
var diapers = request.params.diapers!==undefined?request.params.diapers:"No"; | |
var sleep = request.params.sleep!==undefined?request.params.sleep:"No"; | |
var milestone = request.params.milestone!==undefined?request.params.milestone:"No" | |
var growth = request.params.growth!=undefined?request.params.growth:"No" | |
var feed = request.params.feed!=undefined?request.params.feed:"No" | |
// add the promises to an promise array | |
var funcs = []; | |
if (diapers=="yes") funcs.push(diaperQuery.find()) | |
if (sleep=="yes") funcs.push(sleepQuery.find()) | |
if (milestone=="yes") funcs.push(milestoneQuery.find()) | |
if (growth=="yes") funcs.push(growthQuery.find()) | |
if (feed=="yes") funcs.push(feedQuery.find()) | |
//kinda like Promise.all() | |
//Parse Documentation indicates the use of Parse.Promise as follows | |
// Parse.Promise.when(funcs).then(function(r1,r2,r3){}) but in most scenarios you may not know | |
// how many funcs are there in the func array. | |
Parse.Promise.when(funcs).then(function(){ | |
response.success(arguments); | |
}, function(error){ | |
response.error(error); | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment