fractal icon indicating copy to clipboard operation
fractal copied to clipboard

Serializers that allow creation of "normalized" structures.

Open andrewmclagan opened this issue 9 years ago • 4 comments

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.

andrewmclagan avatar Jul 26 '16 01:07 andrewmclagan

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)

andrewmclagan avatar Jul 26 '16 07:07 andrewmclagan

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

FoxxMD avatar Mar 02 '17 19:03 FoxxMD

Updated the gist.

andrewmclagan avatar Mar 05 '17 23:03 andrewmclagan

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.

stale[bot] avatar Apr 16 '22 07:04 stale[bot]