How can I assign id/slug related to the entity's parent using normalizr?
Example:
API Response for a user call:
{
id: '12345',
firstName: 'John',
images: [
{
url: 'https://www.domain.com/image0',
name: 'image0'
},
{
url: 'https://www.domain.com/image1',
name: 'image1'
}
]
}
I could define my schemas in the following way:
const image = new Schema('images');
const user = new Schema('users');
user.define({
images: arrayOf(image)
})
The problem is that images don't have an id
property, so normalizr will not be able to distinguish them unless we provide an id
property. Of course, we could do something like
const image = new Schema('images', { idAttribute: uuid.v4() });
and generate unique identifiers.
Suppose we receive a user update and an image's name has been updated. Because we generate unique identifiers in every normalization, we are not able to identify and update the existing image.
I need a way to reference the parent entity (user) in the image entity (either in its id/slug like 12345-image0
, 12345-image1
or as a separate property.
What would be the optimal way to achieve this?
processStrategy
doesn't have to mutate the value, you can return a new object with an id field. – Wertheimer