Context
We have a large Django application with a complex frontend that mixes several technologies. We are thinking about a big redesign and want to avoid some organic growth complexity from before, i.e. a lot of manual change tracking and individual endpoints in the REST API.
The data model spans a dozen Django applications and 156 tables in PostgreSQL. Some resources are very large and contain lists of mappings to other, smaller resources. As an example, an AirplaneRepair, a data structure describing a to-be-planned operation contains hardware and personnel requirements, both with constraints regarding when they are needed.
The frontend has a few common features that challenge the current architecture:
- auto-save on changes (similar to Google Drive)
- undo / redo (should work nicely with redux)
- versioning of operation-describing components (e.g. a version of the AirplaneRepair operation with all of its crew and hardware constrains)
- stored as JSON blobs in the DB for now
- created on explicit "create version" call by user
Otherwise it's mostly data manipulation with backend enforced constrains / consistency checks.
The proposed new architecture
We defined all our models in Django. Those should stay. ORM maps them onto the DB.
API
The REST API should consist of two root parts
/api/v2/resources/is supposed to give generic CRUD access to all models. Updates should be supplied using the JSON-Patch principles (i.e. just diffs instead of whole JSON blobs)?fields=field1,field3,field5should allow filtering of the returned data to return only requested subfields?expand=field.childrenshould triggerprefetch_relatedor some other way to auto expand relations in the models and place them in the returned JSON
/api/v2/services/holds any specific RPC calls that trigger complex action in the backend which don't map onto the resources
Frontend
The frontend is based on a combination of static "frames" and react components inside of those frames. We use react-redux to keep track of our data. Reading through this discussion on reddit, it seems others have tried marrying json-patch documents and redux/react. Generally, the action/reducers pattern of redux should allow for easy implementation of a few concepts mentioned above.
- UI displays data, based on the URL (i.e. resource and current view)
- edits to the data are wrapped in actions and reducers
- use redux middleware to dispatch any data manipulation actions to the backend
- update the redux store on ajax responses
- update the redux store through a websocket "notify" channel that allows the server to update its clients about changed resources
Open questions
- What are ways to generically expose all defined models in django via CRUD rest APIs (preferably with DRF)
- How do we hook into the lifecycle here to handle the mentioned request parameters
- How should we map redux actions to PATCH / POST / PUT requests? Partial updates are especially interesting, as we shouldn't send the whole JSON blob forth and back simply to update a single field
- Is JSON-Patch a widely used standard?