I'm writing a react-redux application. At the beginning it calls a single endpoint, which returns a good amount of data as a heavily nested JSON. I then normalize it and put it into my redux-orm schema.
To me it seems silly to create the nested data on the backend just to loop through the nested data on the frontend in order to normalize it, considering it's coming from a normalized PostgreSQL database.
Database = Normalized -->
API = Denormalized -->
Frontend = Normalized
Is it best practice to just send back a normalized API response?
The basic concepts of normalizing data are: Each type of data gets its own "table" in the state. Each "data table" should store the individual items in an object, with the IDs of the items as keys and the items themselves as the values. Any references to individual items should be done by storing the item's ID.
I'm not sure there is a "good" way to do so. If you have to deal with an existing API, then deal with it, and use proxy/parsers in your frontend code to map your backend payload into your redux-orm store, and on the other side too.
I won't pretend here to give an answer, but rather a feedback, after nearly a year of production of our React/Redux/Redux-orm application Wisembly Jam.
When we started from scratch, we chose to use the JsonAPI spec for our API. We liked that as it exposed objects and relations in a way that fitted well with our PostgreSQL scheme, and also our redux-orm one.
That way, no model relation nesting needed both way, only plain objects handled in data
field, included relations in included
field. It appeared to work very nicely together.
You might inspect our application Network tab to look into our api payload responses, and also our redux-orm store (using Redux Chrome extension).
Hope that helped a bit, despite my english and not being properly an answer :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With