I have a database containing a table of organisations with properties orgref, parentorgref, name.
{ orgref: 1, name: "Level 0 org" }
{ orgref: 2, parentorgref: 1, name: "Level 1 org A" }
{ orgref: 3, parentorgref: 1, name: "Level 1 org B" }
{ orgref: 4, parentorgref: 3, name: "Level 2 org" }
I store these organisations in an rxjs entity state.
In most places in my application, I want to use these hierarchically (data trees etc) so want them arranged as:
{
orgref: 1,
name: "Level 0 org",
children: [{
orgref: 2,
parentorgref: 1,
name: "Level 1 org A"
}, {
orgref: 3,
parentorgref: 1,
name: "Level 1 org B",
children: [{
orgref: 4,
parentorgref: 3,
name: "Level 2 org",
}]
}]
}
Of course, if I add the sub-organisations to the children
collection when I load the organisations via
{
...org,
children: getChildrenRecursive()
}
then the reference to the entity is broken - that is, if I edit the entity with orgref=3, it won't update the organisation within the hierarchy because it it a copy of the object, rather than a reference to it.
Is there an accepted solution for dealing with hierarchical entities in an ngrx EntityState?
Thanks