Normalizr: Identifying entities by type rather than schema for polymorphic mappings
Asked Answered
S

1

6

For a polymorphic schema such as Union in Normalizr, for schema definitions 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 takes the form:

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

The entities are keyed on 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 up the elements later without full denormalization.

Is there some better way to normalize such data with normalizr to make it easier to pull the entity from the entities, given the result? For my purposes, ideally, 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 matches the entity key (that is pretty trivial), and the name of the key in result is type (more of a pain if you want to do it with more complex data). I suspect that that sort of normalization would make it harder to denormalize, but I'm interested in normalization only.

Salify answered 13/7, 2017 at 14:24 Comment(0)
S
1

Got an answer on this: https://github.com/paularmstrong/normalizr/issues/281

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

Salify answered 13/10, 2017 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.