Search code examples
web-applicationscrudundo

how to undo an action in a webapp?


Is there any standard method for implementing an undo feature for CRUD webapps?

I have a basic cms where the user logs in, makes changes to models of various complexities and navigates between pages.


Solution

  • There is no standard way. As TGH mentions, web applications are generally stateless.

    One way around this, though, is to keep two parallel models of your data alive: one is the live version, one is the historical record. So, for example, if you have a "post" relation that looks something like:

    post
      attributes
        author
        title     Text
        body      Text
        timestamp TimestampTZ
      conditions
        pk (author, title)
        fk author
    

    you could add another like:

    post_history
      attributes
        revision  Serial (per post)
        author
        title
        body      Text
        timestamp TimestampTZ
      conditions
        pk (revision, author, title)
        fk post
    

    This would track the history of the posts, and not be shown unless the user wanted to undo a version.

    This model can be applied to nearly any data (personal names, translation strings, account records, etc.), but can get complex when it comes to maintaining revisions of relation definitions (as in, when you are tracking changes in links to other relations, such as the history of which worker drove which car, or changing roster assignments in an organization or whatever).

    You have to be more careful with how you design data to implement a rollback or undo system of this type, but it is certainly doable. Ask yourself first whether the effort of implementing this as a feature is worth your time -- usually the answer is "yes, but only in case A or B, but not across the entire system".

    Note that the web application is still stateless, but now you have a historical record based on events as their own discreet slices of time.