0

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

{
  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被视为与 a 相同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对象中。

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

4

1 回答 1

1

不幸的是,如果这是您的要求,Normalizr 不适合您。或者,如果您要按 ID 查找“谈话”列表,您可以使用 Object.keys(data.entities.talks)

于 2017-03-01T03:55:14.040 回答