So!
Here I am after asking around.
As you well know, the forest admin UI is built around tables and flat forms, which don’t play well with nested data: it’s much easier to design an admin panel for SQL than mongo!
Three consecutive attempts were made in the product to deal with nested data.
First iteration: when using agents < forest-express-mongoose@8.1.0
Agents before that version did not have any support for nested data.
The UI however does the following:
When a model contains fields that are nested one level
{
firstName: 'john',
lastName: 'doe',
address: {
// like this
streetNumber: 1,
streetName: '5th avenue'
}
}
Those are displayed with an indentation in the forms
When a model contains a list of flat objects
{
firstName: 'john',
bills: [
// like this
{ title: 'sdf', amount: 234 },
}
}
Those get displayed in the related data section with the “white folder” logo.
Note that
- All of this is happening in the UI.
- The virtual relation is not visible in the
explorer
section
When a model contains anything else
{
firstName: 'john',
bills: [
// like this, but can be anything else that does not fit into the
// two previous cases
{ title: 'sdf', amount: 234, deep: { otherInfo: 2 } },
}
}
In that case, the UI can no longer display the table component, so it falls back to using the JSON editor
Second iteration: when using agents >= forest-express-mongoose@8.1.0
The flattener was added to the forest-express-mongoose at that version.
It is backend code (in the agent) that transforms the models before sending them to the UI so that the
UI sees the data as flat, and then performs the inverse transformation on updates / creations.
It has a strong limitation, which is that it stops exploring the schema when you reach the first array on any document
It basically knows how to transform this:
{
firstName: 'john',
lastName: 'doe',
address: {
streetNumber: 1,
streetName: '5th avenue',
country: { id: 'fr', name: 'France' }
// can nest more levels
},
nestedInfo: {
deep: [
{ id: 1, name: 'whatever', deepDeep: { mode: 1 }
]
}
}
into this:
{
firstName: 'john',
lastName: 'doe',
'address->streetNumber': 1,
'address->streetName': '5th avenue',
'address->streetName': '5th avenue',
'address->country->id': 'fr',
'address->country->name': 'France',
'nestedInfo->deep': [
// Note that it is not flattening the submodels in the array
{ id: 1, name: 'whatever', deepDeep: { mode: 1 }
]
}
We can see here, that it would not solve your issue, because the relations are still being handled
Third iteration: when using @forestadmin/agent
This was released around one year ago.
The mongoose driver is a complete rewrite.
It can do anything that the previous flattener did, but also get rid of the one-to-one mapping between mongoose collections and forest admin collections.
To take your case, you can have one collection on mongoose (lawyers, which contains PAs), but two collections on forest admin (lawyers and PAs).
With this new system, the UI is not doing any work: it thinks that the agent is exposing two flat tables and just behaves as if the underlying storage was all flat tables.
This allows to do charts on submodels, or display them all a once in a table view, without taking in consideration where they come from
So it goes from this:
users
{
firstName: 'john',
bills: [
{ title: 'sdf', amount: 234, deep: { otherInfo: 2 } },
{ title: 'sdf', amount: 234, deep: { otherInfo: 2 } },
}
}
to this:
users
{ _id: 1234, firstName: 'john' }
bill
{ _id: '1234.bills.0', billId: 1234, title: 'sdf', amount: 234, deep: { otherInfo: 2 } }
{ _id: '1234.bills.1', billId: 1234, title: 'sdf', amount: 234, deep: { otherInfo: 2 } }
(let me submit this reply, I’ll write the solutions for your issue on the next one as this is getting longer than I wanted)