what efficient approach maintaining data history models in sails? instance, how maintain data if user updates database , we'd keep versions reverting data updates.
i've seen numerous examples of using "changes" tag keeps date sub-tag. efficient?
on model update can copy previous information. there better suggestions or links examples?
{ text:"this latest paragraph", changes:{ text:{ "1470685677694":"this paragraph", "1470685577694":"this original paragraph" } } }
i hoping find solution find / search , optimize data allow user revert if necessary.
you can define model following , can preserve history can use restore previous values.
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(); }); }) }); } };
Comments
Post a Comment