2

假设我有这种状态:

{
  "appointments": {
    "ids": [...],
    "entities": [
        "0": {
            ...
            mandatoryRecipients: ['0','3']
        },
        "1": {
            ...
            mandatoryRecipients: ['1','2','0']
        }
    ]
  },
  "recipients": {
    "ids": [...],
    "entities": [
        "0": {
            ...
            id: '0',
            name: 'foo'
        },
        "1": {
            ...
            id: '1',
            name: 'foobar'
        },
        "2": {
            ...
            id: '2',
            name: 'bar'
        }
    ]
  }
}

在某个时刻,我需要在约会实体的“mandatoryRecipients”数组中添加一个值。我知道这种状态没有被规范化,但我怎样才能将它规范化以完成多对多关系?

4

2 回答 2

1

你需要为此使用一个库。手动覆盖所有内容将花费太多精力。

您可以使用ngrx-entity-relationship

比如这样。

const fullAppointment = rootEntity(
  selectAppointments, // feature selector, EntityState<Appointment>
  relatedEntity(
    selectRecipients, // related feature selector, EntityState<Recipient>
    'mandatoryRecipients', // key with ids
    'mandatoryRecipientsEntities', // key to assign related entities
  ),
);

const fullAppointments = rootEntities(fullAppointment);

store.select(fullAppointments, ['0', '1']).subscribe(value => {
  // value[0].mandatoryRecipientsEntities[0].name works!
});

或者如果你使用 createSelector 然后

const selectFullAppointments = createSelector(
  selectIds,
  v => v,
  (ids, store) => fullAppointments(store, ids);
);
于 2020-05-21T13:38:23.417 回答
0
 interface Appointment {
  id: string;
}

 interface MandatoryRecepient {
   id: string;
   recepientId: string;
   appointmentId: string;
 }

 interface Recepient {
  id: string;
  name: string;
}
于 2020-05-26T12:25:16.570 回答