Helper functions for traversing normalizr relationships.
Who is this for?
Anyone that uses normalizr, or normalized data that conforms to it.
What does it do?
NormalizedGetlets you access normalized data using a path as though it was denormalized.
GraphGetlets you denormalize multiple paths at once, perfect for using in components.
Say you have a schema like this:
const users = 'users';const comments = 'comments'user: users;const articles = 'articles'author: userscomments: comments;return comments articles users ;
bindNoramlizedGet like so:
const normalizedGet = ;
Now you can access deep relationships with a single line of code:
// With normalizedGetconst commenters = ;
It even follows Array entities. So the above example will map over the comments and return the authors.
For comparison, here's how you'd do it without
// Without normlaizedGet:const commenters = Object;
The more relationships your data has have the more complex this manual fetching gets. And you have to remember which property maps to which model.
normalizedGet is nice for getting a specific piece of data from your entity graph. What if you need more data? That's where
graphGet comes in handy. It returns a deep structure containing the data you specify.
First we bind
graphGet using the similar config method as before:
const graphGet = ;
Now we can retrieve the commenters as before, but also the article and comments too.
const models =
This will output nested data ready for use in a view:
articles:123:title: '…'comments:comment: 'Looks good to me'user: name: 'Jane'… …
GraphGet can accept multiple paths and merge them together.
Differences between graphGet and normalizedGet
Both functions use the same underlying recursive getter. Both map over array results.
normalizedGet only returns the "leaf" data, the last element of the path.
graphGet returns both the leaf and the data leading to it.
normalizedGet works only on a single path and has no merge strategy.
graphGet merges paths into an identity map at the root.
First bind your schemas. This makes it easy to use the getters without passing schemas and data in each time. Probably best to do this somewhere central in your project and export the bound getter.
const normalizedGet = ;const graphGet = ;
Now you have
graphGet functions ready for easy data denormalization.