Node, Sequelize and Promises: return the whole catalog / multiple queries at once
Hello all,
I wanted to share this god-like piece of pure beauty.
Hedonists will understand…
// sequelize models
var models = [
People,
Company,
Product,
...
];
// restify, express or whatever
exports.get = function(req, res) {
Promise.all(models.map(function(Model) {
return Model.findAll();
})).then(function(results) {
var response = {};
results.forEach(function(results, i) {
response[models[i].name] = results;
});
res.send(200, response);
}).catch(function(err) {
res.send(500, err);
});
};
So…. yes, I just used a Promise
in a decent way and I feel proud of it!
No comments yet.