规范化共享当前架构的嵌套实体

时间:2017-03-01 03:00:40

标签: normalizr

我的目标是规范化这个对象:

{
  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对象中。

为了适应这种情况,我的架构配置缺失了什么?

1 个答案:

答案 0 :(得分:1)

不幸的是,如果这是您的要求,那么Normalizr不适合您。或者,如果您正在寻找"会谈"通过ID,您可以使用Object.keys(data.entities.talks)