Last active
August 29, 2015 14:09
-
-
Save IsaiahPacheco/e692f19b47ba72d92b94 to your computer and use it in GitHub Desktop.
Sails.js example of q - node.js library to execute multiple queries and combining results.
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
// Sails.js 0.5.0 - ? | |
// Assumes you have a "Some" and "Other" models and | |
// associating the two does not make sense. | |
// SearchController.js | |
module.exports = { | |
list: function (req, res) { | |
Search.getSearchableList(null, function(err, values){ | |
if (values) { | |
return res.send(values); | |
} else { | |
return res.status(err.status).send(err.message); | |
} | |
}); | |
} | |
}; | |
// Search.js - Model | |
module.exports = { | |
attributes: { | |
}, | |
getSearchableList: function (options, cb) { | |
return Other.find() | |
.then(function(other){ | |
var some = Some.find() | |
.then(function(some){ | |
return some; | |
}); | |
return [other, some]; | |
}) | |
.spread(function (one, two) { | |
return cb(null, one.concat(two)); | |
}) | |
.fail(function(err){ | |
err.status = 500; | |
err.message = 'Search.js getSearchableList query error'; | |
return cb(err); | |
}) | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment