Normalizr: Identifying entities by type, not schema for polymorphic mappings

For a polymorphic circuit, such as Union in Normalizr, to define the circuit and data:

const data = { owner: { id: 1, type: 'user', name: 'Anne' } }; const user = new schema.Entity('users'); const group = new schema.Entity('groups'); const unionSchema = new schema.Union({ user: user, group: group }, 'type'); const normalizedData = normalize(data, { owner: unionSchema }); 

normalized data are:

 { entities: { users: { '1': { id: 1, type: 'user', name: 'Anne' } } }, result: { owner: { id: 1, schema: 'user' } } } 

The objects are tied to the schema key, in this case users , but the result object includes only the key for the schema in the UnionSchema definition. This can make it difficult to match elements later without full denormalization.

Is there a better way to normalize such data with normalization to make it easier to pull an object from entities , given the result ? For my purposes, ideally, the data could be normalized from something like:

 const data = { owner: { id: 1, type: 'users', name: 'Anne' } }; 

to

 { entities: { users: { '1': { id: 1, type: 'users', name: 'Anne' } } }, result: { owner: { id: 1, type: 'users' } } } 

Note that the type corresponds to the entity key (this is pretty trivial), and the key name as a result is type (more painful if you want to do this with more complex data). I suspect that such normalization will make denormalization more difficult, but I am only interested in normalization.

+5
source share
1 answer

Got a response to this: https://github.com/paularmstrong/normalizr/issues/281

Apparently, the behavior is intentional and not going to change - there is no way to use Normalizr to accomplish what I requested.

0
source

Source: https://habr.com/ru/post/1269799/


All Articles