Dipu
Dipu

Reputation: 8279

How to update in one query in waterline

In SQL, I can update a field with inline calculation like this-

UPDATE user SET count=count+1 WHERE id=userID

It requires only one database operation.

But, in waterline, my only option now is to query the user, change the value, and save back into the database.

User.findOne(userID).exec(function(err, user) {
    if(user) {
        user.count++;
        user.save(function(err) {
             if(!err) console.log("success");
        });
    }
});

It requires double database access. How can I reduce the overhead?

Upvotes: 1

Views: 374

Answers (1)

munkee
munkee

Reputation: 769

You can execute native querys with waterline so I would go that route.

User.query("query string here", function (err,result){//Handle errors and result here});

http://sailsjs.org/documentation/reference/waterline-orm/models/query

Upvotes: 1

Related Questions