I'm creating an API and data model for a multi-user document-creation application and I'm having trouble with a particular part of the data model/API design. Below is my current line of thinking:
There are two similar actions that can happen while a user is writing a document:
- A user can manually save a document. This creates a revision and there can be many revisions for a single document.
- An autosave is triggered periodically while a user is editing the document. Because autosaves can happen fairly frequently and have redundant data, there can only be one autosave for a single document.
These actions are used to create a couple of features:
- Users can revert to an old revision of the document.
- Autosaves prevent the user from losing their work.
- Basic document locking - if a user creates an autosave for a document, then another user cannot begin editing that document. When a user triggers a manual save, creating a revision, the autosave associated with the document will be deleted, and therefore another user could start working on the document then.
Here's an outline of the data model I have:
_________ ______________________ |Documents| |DocumentRevisions | |---------| ------< |----------------------| |id: int | |id: int | |_________| |documentBody: string | | |createdAt: datetime | | |______________________| ____|________________ |DocumentDrafts | |---------------------| |id: int | |documentBody: string | |_____________________|
And here are some of the endpoints I have:
- GET /documents/
- POST /documents//revisions
- This is called on a manual save
- PUT /documents//draft
- This is called on autosave
These are the things I don't like about this current model:
POST /documents/<documentId>/revisionsendpoint has to also modify the DocumentDrafts table to reflect the fact that the document is now in a "clean" state and can be modified by another user
PUT /documents/<documentId>/draftendpoint breaks a few rules of REST including the use of the singular word "draft" (because there can only be one draft of a document)
I think the requirement that only one autosave can exist for a document at any point is really tripping me up and I'm not sure how to model it correctly. Is there a better way? Do I need to lift the requirement of a single autosave per document?