fractal
fractal copied to clipboard
Serializers that allow creation of "normalized" structures.
With the advent and proliferation of SPA driven by javascript on the client side, response serializations has never been more critical.
Its extremely advantageous to have the ability for creating normalized responses that flatten nested structures into a database like syntax.
e.g.
[{
id: 1,
title: 'Some Article',
author: {
id: 1,
name: 'Dan'
}
}, {
id: 2,
title: 'Other Article',
author: {
id: 1,
name: 'Dan'
}
}]
Normalised into
{
result: [1, 2],
entities: {
articles: {
1: {
id: 1,
title: 'Some Article',
author: 1
},
2: {
id: 2,
title: 'Other Article',
author: 1
}
},
users: {
1: {
id: 1,
name: 'Dan'
}
}
}
}
There are huge advantages to this and at present is often performed on the client side with libraries such as https://github.com/paularmstrong/normalizr
At present its not really possible to use Fractal Serializers to define this kind of structure.
From a brief overview we would need the ability to manage and create a custom Scope class? Or a complete re-think of how Fractal serializations occur (better IMO). Currently it is only possible to structure data in a nested hierarchy rather then having the ability to structure it in a "normalized" fashion as described above.
Well, its possible after all. If anyone is interested.. I could create a PR and cleanup the serializer...
https://gist.github.com/andrewmclagan/0b352aa21c48d912609a85a5b09e61b6
its somewhat messy. needs tests.
- Built specifically for React/Redux
- Reduces redundant data (bandwidth savings)
- Perfect for react, ember, angular SPA
- Feels far clearer then the official JSON-API specs (optioned yes)
I'm about to tackle this issue myself in an SPA I'm working on. I'd be interested in having this functionality.
P.S. I wasn't able to get your gist to work
Updated the gist.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 4 weeks if no further activity occurs. Thank you for your contributions.