We have a database that maps the following relationship:
tag
has many campaigns
campaign
has many tags
campaigns
and tags
is represented by an associative entity called a campaign_tag
campaign_tag
entity has a priority
attributeHow can we implement normalizr (or any similar library) to generate a flattened app state that accounts for our associative entities, when our associative entities do not have unique IDs / values?
Other examples I've seen only had the original entities mapped out and seem better suited for one-to-many or one-to-one relationships. They usually produced state tree such as this:
{
entities: {
campaigns: {
'1': { id: 1, name: 'Ace', tags: [1, 2, 3] },
...
},
tags: {
'1': { id: 1, name: 'Example Tag', campaigns: [1, 2, 3] },
...
}
},
...
}
Would our associative entities go in that entities
group? Should we just add unique IDs? That seems counter-productive. Is it even appropriate to flatten such relationships?
Any help is appreciated, thanks.
Scenarios:
You control the API: I'll send objects like this:
campaigns: [{
id: <id>,
name: <name>,
}],
tags: [{ id: <id>, name: <name> }],
campaign_tags: [{campaign_id, tag_id, priority}]
So this will be easy to manage and avoid duplication of information. When you mount your component using redux connect, you can create a selector to fill your campaign tags to add to your component's properties.
You don't control the API. In this case I'll need to know what type of response are you getting from the server in order to recommend something.
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