Я пытаюсь нормализовать и денормализовать древовидную структуру.Нормализация выглядит хорошо, но когда я пытаюсь денормализовать с помощью той же схемы, я получаю только идентификаторы.
То же самое с элементом результата, денормализация хороша для этого элемента, но она не денормализует дочерние элементы.
Repro: https://repl.it/@DDeis/Normalizr-nested-Union-Array-denormalization
Input
Вот как я использую 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
);
Output
Вот то, что я ожидаю увидеть, когда я запускаю вышеописанное:
[
{
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"
}
]
}
Существует ли решение для правильной денормализации, кроме написания пользовательской функции денормализации?