If you have decided to denormalize/duplicate your data in Firestore to optimize for reads, what patterns (if any) are generally used to keep track of the duplicated data so that they can be updated correctly to avoid inconsistent data?
As an example, if I have a feature like a Pinterest Board where any user
on the platform can pin my post
to their own board
, how would you go about keeping track of the duplicated data in many locations?
What about creating a relational-like table for each unique location that the data can exist that is used to reconstruct the paths that require updating.
For example, creating a users_posts_boards
collection that is firstly a collection of userIDs
with a sub-collection of postIDs
that finally has another sub-collection of boardIDs
with a boardOwnerID
. Then you use those to reconstruct the paths of the duplicated data for a post
(eg. /users/[boardOwnerID]/boards/[boardID]/posts/[postID]
)?
Also if posts
can additionally be shared to groups
and lists
would you continue to make users_posts_groups
and users_posts_lists
collections and sub-collections to track duplicated data in the same way?
Alternatively, would you instead have a posts_denormalization_tracker
that is just a collection of unique postIDs
that includes a sub-collection of locations that the post
has been duplicated to?
{
postID: 'someID',
locations: ( <---- collection
"path/to/post/location1",
"path/to/post/location2",
...
)
}
This would mean that you would basically need to have all writes to Firestore done through Cloud Functions that can keep a track of this data for security reasons....unless Firestore security rules are sufficiently powerful to allow add operations to the /posts_denormalization_tracker/[postID]/locations
sub-collection without allowing reads or updates to the sub-collection or the parent postIDs
collection.
I'm basically looking for a sane way to track heavily denormalized data.
Edit: oh yeah, another great example would be the post
author's profile
information being embedded in every post
. Imagine the hellscape trying to keep all that up-to-date as it is shared across a platform and then a user
updates their profile
.