Get current values ​​inside Sails waterline before connecting

In Sailing Waternline, I need to be able to compare the previous value with a new one and assign a new attribute under certain conditions. For instance:

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

How can I access currentValues?

+4
source share
1 answer

I'm not sure if this is the best solution, but you can get the current record by running a simple query findOne:

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

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

      return callback();
    });
}
+2
source

Source: https://habr.com/ru/post/1598492/


All Articles