Normalizr:嵌套联合数组反规范化(树状结构)

时间:2019-03-14 11:57:29

标签: normalizr

我正在尝试规范化和非规范化树状结构。规范化看起来不错,但是当我尝试使用相同的架构进行规范化时,我只会得到ID。

与结果元素相同,反规范化对该元素很有用,但不会对子元素进行反规范化。

复制:https://repl.it/@DDeis/Normalizr-nested-Union-Array-denormalization

输入

这是我使用normalizr的方式:

const data =  [
  {
    id: "node_1",
    type: "node",
    items: [
      {
        id: "node_1-item_1",
        type: "leaf"
      },
      {
        id: "node_1-item_2",
        type: "leaf"
      }
    ]
  }
];

const node = new schema.Entity("nodes");
const leaf = new schema.Entity("leafs");
const item = new schema.Union(
  {
    leaf,
    node
  },
  "type"
);
const items = new schema.Array(item);
node.define({ items });

const normalizedData = normalize(data, items);
const denormalizedData = denormalize(
  normalizedData.result,
  items,
  normalizedData.entities
);

输出

当我运行上述代码时,这就是我期望看到的:

 [
  {
    id: "node_1",
    type: "node",
    items: [
      {
        id: "node_1-leaf_1",
        type: "leaf"
      },
      {
        id: "node_1-leaf_2",
        type: "leaf"
      }
    ]
  }
]

当我运行上述代码时,这是我实际上看到的内容:

{
  "id": "node_1",
  "type": "node",
  "items": [
    "node_1-leaf_1",
    "node_1-leaf_2"
  ]
}

具有标准化数据:

{
  "entities": {
    "leafs": {
      "node_1-leaf_1": {
        "id": "node_1-leaf_1",
        "type": "leaf"
      },
      "node_1-leaf_2": {
        "id": "node_1-leaf_2",
        "type": "leaf"
      }
    },
    "nodes": {
      "node_1": {
        "id": "node_1",
        "type": "node",
        "items": [
          {
            "id": "node_1-leaf_1",
            "schema": "leaf"
          },
          {
            "id": "node_1-leaf_2",
            "schema": "leaf"
          }
        ]
      }
    }
  },
  "result": [
    {
      "id": "node_1",
      "schema": "node"
    }
  ]
}

除了编写自定义的非规范化函数以外,是否有解决方案以正确地进行规范化?

0 个答案:

没有答案
相关问题