2016-08-08 8 views
3

Sailsのモデルのデータ履歴を維持する効率的なアプローチは何ですか?たとえば、ユーザーがデータベースを更新し、データが更新されたときにデータを元に戻すためのバージョンを保持したい場合は、データをどのように維持するのですか。SailsJSのモデル更新の履歴を維持

私は、日付をサブタグとして保持する「変更」タグを使用する例を多数見てきました。これが最も効率的ですか?

モデルの更新時に、以前の情報をコピーできます。例とのより良い提案やリンクはありますか?

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

私は/検索を見つけ、必要に応じて、ユーザが復帰できるようにするには、このデータを最適化するための良い解決策を見つけることを期待しています。

答えて

0

モデルを次のように定義することができます。履歴を保存して以前の値を復元することもできます。

API /モデル/

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(); 
     }); 
     }) 
    }); 
    } 
}; 
Test.js
関連する問題