Soft removal in sails / waterlines

Trying to delete a user model with:

//Hard Delete User.destroy({id:userId}, function(err, res){ //Hard Delete }) 

I need to do a soft deletion on the user model and currently set the isDeleted flag to true when deleting and updating the document:

 updateUser.isDeleted = true; User.update({id:userId}, updateUser, function(err, res){ Update project }) 

and when I receive documents, I check If isDeleted is true or not.
Is there a built-in function provided by Sails or Waterline that I can configure to perform soft deletion and not update, and then get based on the isDeleted flag?

+5
source share
2 answers

you can use the beforeFind () life cycle function to filter soft deleted records

model: parrot js

 module.exports = { attributes: { // eg, "Polly" name: { type: 'string' }, // eg, 3.26 wingspan: { type: 'float', required: true }, // eg, "cm" wingspanUnits: { type: 'string', enum: ['cm', 'in', 'm', 'mm'], defaultsTo: 'cm' }, // eg, [{...}, {...}, ...] knownDialects: { collection: 'Dialect' }, isDeleted:{ type:'boolean' } }, beforeFind: function(values, cb) { values.isDeleted = false; cb(); } } 

ParrotController.js

 module.exports = { // getting default parrots isDeleted = true list: function (req, res) { Parrot .find() .exec(function(err, parrots) { if(err) return res.send({ flag:false, data:[], message:"Error." }); if(parrots && parrots.length){ return res.send({ flag:true, data:parrots, message:"Success." }); } else{ return res.send({ flag:false, data:[], message:"Parrot list is empty." }); } }); } }; 
+1
source

There is no soft-delete function in sails, and I doubt it will be.

Here's the challenge: why not write your own? Waterline supports class methods ! Of course, you will need to do this for each model or create a service ... which can be even more efficient.

0
source

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


All Articles