Structuring Relations
Flatten data in your database
The official Firebase docs on 'Best practices for data structure' explicitly advise against nested data.
Instead, Firebase recommends 'flattened' (or 'denormalized') data structures.
- Nested structure
- Flattened equivalent
This is based off the nested data structure example in the Firebase docs.
{
// This is a poorly nested data architecture, because iterating the children
// of the "chats" node to get a list of conversation titles requires
// potentially downloading hundreds of megabytes of messages
"chats": {
"one": {
"title": "Historical Tech Pioneers",
"messages": {
"m1": { "sender": "ghopper", "message": "Relay malfunction found. Cause: moth." },
"m2": { ... },
// a very long list of messages
}
},
"two": { ... }
}
}
Inbuilt relation helpers
To make this easier, Fireactive provides relations to:
- encourage best practices in normalized data; and
- make it easier to fetch related data.
There are relation helpers for both one-to-one and one-to-many relationships.