规范化共享当前模式的嵌套实体
Normalizing a nested entity that shares the current schema
我的目标是规范化这个对象:
{
talks: [
{
id: 1755,
speakers: [
{
id: 1487,
name: 'John Doe',
},
],
related_talks: [{
id: 14,
speakers: [{
id: 125,
name: 'Jane Doe',
}],
event: {
id: 181,
name: 'First Annual',
},
}],
event: {
id: 180,
name: 'July Party',
},
},
],
};
进入这个结果:
{
entities: {
events: {
181: {
id: 181,
name: 'First Annual'
},
180: {
id: 180,
name: 'July Party'
}
},
speakers: {
125: {
id: 125,
name: 'Jane Doe'
},
1487: {
id: 1487,
name: 'John Doe'
}
},
talks: {
1755: {
id: 1755,
event: 181,
speakers: [ 1487 ],
related_talks: [ 14 ],
},
14: {
id: 14,
speakers: [ 125 ],
event: 180,
}
},
},
result: {
talks: [ 1755, 14 ],
},
}
如果您注意到,related_talks
中的项目与 talk
中的项目相同。
我的模式遵循示例并设置如下:
const speaker = new schema.Entity('speakers');
const event = new schema.Entity('events');
export const talk = new schema.Entity('talks', {
speakers: [speaker],
event,
});
talk.define({ related_talks: [talk] });
无论我怎样尝试,我都无法将 related_talks
中的项目添加到 result.talks
数组中。但是,它位于 entities
对象中。
为了适应这个问题,我的架构配置缺少什么?
不幸的是,如果这是您的要求,Normalizr 不适合您。或者,如果您要按 ID 查找 "talks" 的列表,则可以使用 Object.keys(data.entities.talks)
我的目标是规范化这个对象:
{
talks: [
{
id: 1755,
speakers: [
{
id: 1487,
name: 'John Doe',
},
],
related_talks: [{
id: 14,
speakers: [{
id: 125,
name: 'Jane Doe',
}],
event: {
id: 181,
name: 'First Annual',
},
}],
event: {
id: 180,
name: 'July Party',
},
},
],
};
进入这个结果:
{
entities: {
events: {
181: {
id: 181,
name: 'First Annual'
},
180: {
id: 180,
name: 'July Party'
}
},
speakers: {
125: {
id: 125,
name: 'Jane Doe'
},
1487: {
id: 1487,
name: 'John Doe'
}
},
talks: {
1755: {
id: 1755,
event: 181,
speakers: [ 1487 ],
related_talks: [ 14 ],
},
14: {
id: 14,
speakers: [ 125 ],
event: 180,
}
},
},
result: {
talks: [ 1755, 14 ],
},
}
如果您注意到,related_talks
中的项目与 talk
中的项目相同。
我的模式遵循示例并设置如下:
const speaker = new schema.Entity('speakers');
const event = new schema.Entity('events');
export const talk = new schema.Entity('talks', {
speakers: [speaker],
event,
});
talk.define({ related_talks: [talk] });
无论我怎样尝试,我都无法将 related_talks
中的项目添加到 result.talks
数组中。但是,它位于 entities
对象中。
为了适应这个问题,我的架构配置缺少什么?
不幸的是,如果这是您的要求,Normalizr 不适合您。或者,如果您要按 ID 查找 "talks" 的列表,则可以使用 Object.keys(data.entities.talks)