在Sails/Waterline中进行软删除 [英] Soft delete in Sails/Waterline

查看:240
本文介绍了在Sails/Waterline中进行软删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用以下方法删除用户模型:

Trying to delete a user model using:

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

我需要在用户模型上进行软删除,并且当前在删除和更新文档时将标志isDeleted设置为true:

I need to do a soft delete on User model and currently setting a flag isDeleted to true on delete and updating document:

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

在获取文档时,我正在检查是否为isDeleted-是否为真.
是否可以通过Sails或Waterline提供的任何内置功能将其配置为执行软删除,并避免基于isDeleted标志进行更新然后获取?

and while fetching documents I am doing a check If isDeleted - true or not.
Is there any In-built feature provided by Sails or Waterline which I can configure to perform a soft delete and avoid updating and then fetching based on isDeleted flag?

推荐答案

您可以使用beforeFind()生命周期函数来过滤软删除的记录

you can use beforeFind() life cycle function for filter of soft deleted records

模型:鹦鹉,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

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." });
            }

        });

    }
};

这篇关于在Sails/Waterline中进行软删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆