Rattrap
Rattrap

Reputation: 315

How to perform multiple queries all at once in sails.js

For example, I have a model Wallet. And I need to perform several actions. Let's say:

Async.waterfall([
   Wallet.findOne(criteria1).exec(cb1);
   Wallet.update(criteria2).exec(cb2);
   Wallet.findOne(criteria3).exec(cb3);
   Wallet.update(criteria4).exec(cb4);
], ...);

Not exactly right syntax for ASync, no matter. As I understand it is going to be four connections to database server one by one. Is it possible to perform all actions by one connection?

Upvotes: 4

Views: 520

Answers (1)

Vishnu
Vishnu

Reputation: 12293

Use async parallel control flow.

async.parallel({
    one: function(cb1){
        Wallet.findOne(criteria1).exec(cb1);
    },
    two: function(cb2){
        Wallet.update(criteria2).exec(cb2);
    },
    three: function(cb3){
        Wallet.findOne(criteria3).exec(cb3); 
    }
    four: function(cb4){
        Wallet.update(criteria4).exec(cb4);
    }
},
function(err, results) {
    // results is now equals to: {one: {}, two: {},three:{},four:{}} 
});

Upvotes: 2

Related Questions