{"action":"created","issue":{"url":"https://api.github.com/repos/nielsgl/sequelize-paper-trail/issues/6","repository_url":"https://api.github.com/repos/nielsgl/sequelize-paper-trail","labels_url":"https://api.github.com/repos/nielsgl/sequelize-paper-trail/issues/6/labels{/name}","comments_url":"https://api.github.com/repos/nielsgl/sequelize-paper-trail/issues/6/comments","events_url":"https://api.github.com/repos/nielsgl/sequelize-paper-trail/issues/6/events","html_url":"https://github.com/nielsgl/sequelize-paper-trail/issues/6","id":174882642,"number":6,"title":"Discussion, previousVersion or currentVersion?","user":{"login":"mhaamann","id":1766107,"avatar_url":"https://avatars.githubusercontent.com/u/1766107?v=3","gravatar_id":"","url":"https://api.github.com/users/mhaamann","html_url":"https://github.com/mhaamann","followers_url":"https://api.github.com/users/mhaamann/followers","following_url":"https://api.github.com/users/mhaamann/following{/other_user}","gists_url":"https://api.github.com/users/mhaamann/gists{/gist_id}","starred_url":"https://api.github.com/users/mhaamann/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mhaamann/subscriptions","organizations_url":"https://api.github.com/users/mhaamann/orgs","repos_url":"https://api.github.com/users/mhaamann/repos","events_url":"https://api.github.com/users/mhaamann/events{/privacy}","received_events_url":"https://api.github.com/users/mhaamann/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-09-03T06:38:09Z","updated_at":"2016-09-03T06:57:56Z","closed_at":null,"body":"Right now the module saves the **currentVersion** in the document attribute of RevisionModel.\r\n\r\nWhy not save the **previousVersion** instead?\r\n\r\nConsider the following code example:\r\n```\r\n# Current User object in DB:\r\n{id: 4, firstName: 'Jonn', lastName: 'Dot'}\r\n\r\n# Update to the user:\r\nmodels.user.findOne({where: {id: 4}}).then(function (userFromDB) {\r\n  return userFromDB.update({firstName: 'JonA', lastName: 'Doe'});\r\n});\r\n--\r\n# Update to the user:\r\nmodels.user.findOne({where: {id: 4}}).then(function (userFromDB) {\r\n  return userFromDB.update({firstName: 'John'});\r\n});\r\n--\r\n# Update to the user:\r\nmodels.user.findOne({where: {id: 4}}).then(function (userFromDB) {\r\n  return userFromDB.update({firstName: 'JohnB'});\r\n});\r\n--\r\n# Update to the user:\r\nmodels.user.findOne({where: {id: 4}}).then(function (userFromDB) {\r\n  return userFromDB.update({lastName: 'DoT'});\r\n});\r\n```\r\nThe RevisionModel now contains ('currentVersions') in the document colum:\r\n{id: 4, firstName: 'Jonn', lastName: 'Dot', revision: 1} // Initial Create\r\n{id: 4, firstName: 'JonA', lastName: 'Doe', revision: 2} // Update\r\n{id: 4, firstName: 'John', revision: 3}\r\n{id: 4, firstName: 'JohnB', revision: 4}\r\n{id: 4, lastName: 'DoT', revision: 5}\r\n\r\nHaving identified my spelling mistake I would now like to revert the user object back when the user was called _John Doe_.\r\nWith the current implementation of paper-trail we would have to replay the revision log backwards until we reach revision 3.\r\nQuite a risky task.\r\n\r\nInstead I suggest that we instead store the **previousVersion** instead. Doing this the RevisionModel from the above example will end up looking like this:\r\n{} // Initial Create\r\n{id: 4, firstName: 'Jonn', lastName: 'Dot', revision: 1}\r\n{id: 4, firstName: 'JonA', lastName: 'Doe', revision: 2}\r\n{id: 4, firstName: 'John', lastName: 'Doe', revision: 3}\r\n{id: 4, firstName: 'JohnB', lastName: 'Doe', revision: 4}\r\n\r\nThis way it is very easy to revert back to a revision since it contains the full object. The only requirement is that a fully loaded object is used when updating something.\r\n\r\nDoes that make sense?\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/nielsgl/sequelize-paper-trail/issues/comments/244531203","html_url":"https://github.com/nielsgl/sequelize-paper-trail/issues/6#issuecomment-244531203","issue_url":"https://api.github.com/repos/nielsgl/sequelize-paper-trail/issues/6","id":244531203,"user":{"login":"mhaamann","id":1766107,"avatar_url":"https://avatars.githubusercontent.com/u/1766107?v=3","gravatar_id":"","url":"https://api.github.com/users/mhaamann","html_url":"https://github.com/mhaamann","followers_url":"https://api.github.com/users/mhaamann/followers","following_url":"https://api.github.com/users/mhaamann/following{/other_user}","gists_url":"https://api.github.com/users/mhaamann/gists{/gist_id}","starred_url":"https://api.github.com/users/mhaamann/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mhaamann/subscriptions","organizations_url":"https://api.github.com/users/mhaamann/orgs","repos_url":"https://api.github.com/users/mhaamann/repos","events_url":"https://api.github.com/users/mhaamann/events{/privacy}","received_events_url":"https://api.github.com/users/mhaamann/received_events","type":"User","site_admin":false},"created_at":"2016-09-03T06:57:56Z","updated_at":"2016-09-03T06:57:56Z","body":"The down side is that you loose track on who did the change and without the RevisionChangeModel you cannot see what was updated. This is also bad :(\r\n\r\nWhat if we introduce a new coloum 'snapshot' where the previousVersion is stored. The 'currentVersion' will still be stored in document. \r\nWe could furthermore improve paper-trail by loading the full object from db automatically for the snapshot instead of relying that previousVersion actually contains a fully loaded object. \r\n\r\nAt least in our codebase we have a lot of places where we load an object and only specify 1-2 attributes instead of fully loading all attributes."}}
{"actor":{"display_login":"mhaamann"}}