I have a Firebase Firestore with "Components" as a root collection. Each document (a "Component") in the collection may have an array called "children", with each item in the array being an "id" of another component, essentially, a one-to-many relationship. Since every child is also a component it may also have its own children and so forth.
Components Collection
Parent
1_Parent (document)
│ name: 'Parent'
│ id: '1_Parent'
└─children (array)
├─1.1_Child_one
└─1.2_Child_two
First Child
1.1_Child_one (document)
name: 'Child One'
id: '1.1_Child_one'
Second Child
1.2_Child_two (document)
│ name: 'Child Two'
│ id: '1.2_Child_two'
└─children (array)
├─1.2.1_Grandchild_one
└─1.2.2_Grandchild_two
First Grandchild
1.2.1_Grandchild_one (document)
name: 'Grandchild One'
id: '1.2.1_Grandchild_one'
Second Grandchild
1.2.2_Grandchild_two (document)
name: 'Grandchild Two'
id: '1.2.2_Grandchild_two'
In my code, I want to create an object for each component and if it has a children array then each of the id in the array is replaced by a fully fledged object retrieved from Firestore.
The output object tree should look like this
1_Parent
│ name: 'Parent'
│ id: '1_Parent'
└─children
├─1.1_Child_one
│ name: 'Child One'
│ id: '1.1_Child_one'
└─1.2_Child_two
│ name: 'Child Two'
│ id: '1.2_Child_two'
└─children
├─1.2.1_grandhild_one
│ name: 'Grandchild One'
│ id: '1.2.1_grandhild_one'
└─1.2.2_grandhild_two
name: 'Grandchild Two'
id: '1.2.2_grandhild_two'
The output object as JSON should look like this
{
"name": "Parent",
"id": "1_Parent",
"children": [
{
"name": "Child One",
"id": "1.1_Child_one"
},
{
"name": "Child Two",
"id": "1.2_Child_two",
"children": [
{
"name": "Grandchild One",
"id": "1.2.1_Grandchild_one"
},
{
"name": "Grandchild Two",
"id": "1.2.2_Grandchild_two"
}
]
}
]
}
It is obvious, we need recursion here, but I am at complete loss about how to create a recursive function using RxJS. I would appreciate some tips or example code for allowing to do so.
Note, I am using this in an Angular project and I am using AngularFire to access Firebase-Firestore.