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