IsaiahPacheco
11/11/2014 - 5:45 AM

Sails.js example of q - node.js library to execute multiple queries and combining results.

Sails.js example of q - node.js library to execute multiple queries and combining results.

// 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);
			})
		}
};