3

1 レベルのネストされたロットのリストを正規化しています。最初のレベルのロットがマスターと呼ばれる場合、ネストされたロットはスレーブです。

// my schema
const lot = new schema.Entity('lots');
const lots = new schema.Array(lot);
lot.define({slaves: lots});

// my data
const list = [
  {
    id: 1,
    name: 'Lot #1',
    slaves: [
      {
        id: 2,
        name: 'Lot #2'
      }
    ]
  }, {
    id: 4,
    name: 'Lot #4',
    slaves: []
  }
];

normalize(list, lots);

そして、私はこれを取得します:

{
  entities : {
    lots: {
      '1': {
        id: 1,
        name: 'Lot #1',
        slaves: [2]
      },
      '2': {
        id: 2,
        name: 'Lot #2'
      },
      '4': {
        id: 4,
        name: 'Lot #4',
        slaves: []
      }
    }
  },
  result : [1, 4]
}

それには何か問題があります。しかし、正規化された結果にさらに何かを追加したいのですが、その方法がわかりません。

  • 正規化されたスレーブにマスター ロット ID がある
  • スレーブ ID の配列も結果に表示されます

したがって、前の例は次のように正規化されます。

{
  entities : {
    lots: {
      '1': {
        id: 1,
        name: 'Lot #1',
        slaves: [2]
      },
      '2': {
        id: 2,
        name: 'Lot #2',
        master: 1
      },
      '4': {
        id: 4,
        name: 'Lot #4',
        slaves: []
      }
    }
  },
  result : {
    masters: [1, 4],
    slaves: [2],
  }
}

これはノーマライズで可能ですか?

4

1 に答える 1

2

正規化されたスレーブにマスター ロット ID がある

processEntityこれは、カスタム関数を使用して確実に可能です。ここに例があります。要するに:

const processStrategy = (value, parent, key) => ({
  ...value,
  master: key === 'slaves' ? parent.id : undefined
});
const lot = new schema.Entity('lots', { processStrategy });

スレーブ ID の配列も結果に表示されます

これは不可能です。はresult、 に渡されるエントリ スキーマに常に依存しますnormalize

于 2017-01-12T17:25:50.560 に答える