Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 488
Karptonite Avatar asked Jul 13 '17 14:07

Karptonite


1 Answers

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.

like image 172
Karptonite Avatar answered Sep 22 '22 01:09

Karptonite