node.js - Soft delete in Sails/Waterline -
trying delete user model using:
//hard delete user.destroy({id:userid}, function(err, res){ //hard delete })
i need soft delete on user model , setting flag isdeleted true on delete , updating document:
updateuser.isdeleted = true; user.update({id:userid}, updateuser, function(err, res){ update project })
and while fetching documents doing check if isdeleted - true or not.
there in-built feature provided sails or waterline can configure perform soft delete , avoid updating , fetching based on isdeleted flag?
you can use beforefind() life cycle function filter of soft deleted records
model: parrot,js
module.exports = { attributes: { // e.g., "polly" name: { type: 'string' }, // e.g., 3.26 wingspan: { type: 'float', required: true }, // e.g., "cm" wingspanunits: { type: 'string', enum: ['cm', 'in', 'm', 'mm'], defaultsto: 'cm' }, // e.g., [{...}, {...}, ...] 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 empty." }); } }); } };
Comments
Post a Comment