对于多态模式(如Normalizr中的Union),对于模式定义和数据:
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 });
规范化数据采用以下形式:
{
entities: {
users: { '1': { id: 1, type: 'user', name: 'Anne' } }
},
result: { owner: { id: 1, schema: 'user' } }
}
实体在架构键上键入,在本例中为 users
,但结果对象仅包含UnionSchema定义中架构的键 . 这可能使得在没有完全非规范化的情况下稍后匹配元素变得困难 .
有没有更好的方法来使用normalizr对这些数据进行标准化,以便更容易从 entities
拉出实体,给定 result
?出于我的目的,理想情况下,数据可以通过以下方式进行标准化:
const data = { owner: { id: 1, type: 'users', name: 'Anne' } };
至
{
entities: {
users: { '1': { id: 1, type: 'users', name: 'Anne' } }
},
result: { owner: { id: 1, type: 'users' } }
}
请注意,类型与实体键匹配(这非常简单),结果中键的名称是 type
(如果您想要使用更复杂的数据,那么更多的是痛苦) . 我怀疑这种规范化会使反规范化变得更难,但我只对归一化感兴趣 .
1 回答
得到了一个答案:https://github.com/paularmstrong/normalizr/issues/281
显然,这种行为是有意的,不会改变 - 没有办法使用Normalizr来做我所要求的 .