【问题标题】:Maintaining history of model updates in SailsJS在 SailsJS 中维护模型更新的历史
【发布时间】:2016-08-08 19:52:57
【问题描述】:

在 Sails 中维护模型数据历史记录的有效方法是什么?例如,如果用户更新数据库并且我们希望保留版本以在数据更新时恢复数据,我们如何维护数据。

我见过很多使用“更改”标签将日期保留为子标签的示例。这是最有效的吗?

在模型更新时,我们可以复制以前的信息。有没有更好的建议或示例链接?

{
   text:"This is the latest paragraph",
   changes:{
      text:{
          "1470685677694":"This was the paragraph",
          "1470685577694":"This was the original paragraph"
      }
   }
}

我希望找到一个好的解决方案来查找/搜索和优化这些数据,以允许用户在必要时恢复。

【问题讨论】:

    标签: node.js mongodb mongoose sails.js sails-mongo


    【解决方案1】:

    你可以像下面这样定义你的模型,这样你就可以保留历史记录,你可以用它来恢复任何以前的值。

    api/models/Test.js

    module.exports = {
      connectionName:"someMongodbServer",
      tableName:"test",
      autoCreatedAt:false,
      autoUpdatedAt:false,
      autoPk:false,
      attributes: {
        id:{
          type:"integer",
          primaryKey:true,
          autoIncrement:true
        },
        name:"string",
        history:{
          type:'json',
          defaultsTo:[]
        }
      },
      beforeCreate:function(value,cb){
        Test.count().exec(function (err, cnt) {
          if(err)
            return cb(err);
          value.id = cnt + 1;
          cb();
        });
      },
      beforeUpdate:function(values,cb){
        Test.native(function(err,collection){
          if(err)
            return cb(err);
          collection.findOne({_id:values.id},{history:0,_id:0},function(err,data){
            if(err)
              return cb(err);
            collection.updateOne({_id:values.id},{$push:{history:data}},function(err,res){
              if(err)
                return cb(err);
              console.log(res);
              cb();
            });
          })
        });
      }
    };
    

    【讨论】:

      猜你喜欢
      • 2010-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多