Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current values inside Sails' Waterline beforeUpdate hook

In Sails' Waternline I need to be able to compare the previous value with the new one and assign a new attribute under some condition. For example:

beforeUpdate: function(newValues, callback) {
   if(/* currentValues */.age > newValues.age) {
     newValues.permission = false;
   }
}

How could I access the currentValues ?

like image 822
Talysson Avatar asked Oct 19 '22 07:10

Talysson


1 Answers

I'm not sure this is the best solution but you can get the current record by doing a simple findOne request:

beforeUpdate: function(newValues, callback) {
  Model
    .findOne(newValues.id)
    .exec(function (err, currentValues) {
      // Handle errors
      // ...

      if(currentValues.age > newValues.age) {
        newValues.permission = false;
      }

      return callback();
    });
}
like image 145
Yann Bertrand Avatar answered Oct 22 '22 03:10

Yann Bertrand