Refreshing Denormalized Firebase Data

Some background:

My question is very similar to this clarifying question about denormalization , but I want to change things up a bit.

In the Considerations section of this blog post about denormalization , Firebase users have the following to say about updating data.

Let's discuss some of the implications of [a denormalized data structure]. You will need to make sure that every time some data is created (in this case, a comment), it is put in the right places.

The example includes three paths: one for storing comment data, and two paths for storing pointers to this comment.

...

     

Modifying comments is simple: just set the comment value in / comments to new content. To remove, simply remove the comment from / comments - and whenever you come across a comment ID elsewhere in your code that doesn't exist in / comments, you can assume it has been removed and works fine:

But this only works because, as the answer to another question says,

The structure detailed in the blog post does not store duplicate comments. We save the comments once under /comments

, then store the name of those comments in /links

and /users

. They act as pointers to the actual comment data.

Most of the content is stored in only one place.

Question:

What if the situation is such that you need to store duplicate data? In this case, what's the recommended way to update the data?

My attempt at an answer:

There is an answer to this question , but it targets MongoDB and I'm not sure if it completely solves the problem in Firebase.

The smartest way I could think of, just for reference, is as follows.

I have a helper class that I give a directory of paths in Firebase that somewhat resembles a schema. This class has methods that wrap Firebase methods, so I can write and update along all the paths given in my schema. The helper class iterates over every path that has a reference to an object and writes, updates, or deletes at every location. In my case, for any single operation like this, there are at most 4 paths and most of them have 2.

Example:

Imagine I have three top-level keys, Users, Events, and Events-Metadata. Users submit images to Events, and for both events and users, there is a nested entry for all matching images. Events-Metadata is a native top-level key for the case where I want to display a bunch of events on a page, but I don't want to pull potentially hundreds of image records along with them.

Images can have titles and thus when I update the title of the image I have to update these paths:

new Firebase("path/to/eventID/images/imageID/caption")

and
new Firebase("path/to/userID/images/imageID/caption")

I provide my helper class for both these paths and the wrapper method, so that anytime the header is updated, I can call helperclass.updateCaption (imageObj, newCaptionData) and it will iteratively update the data on each path.

Images are saved with attributes including eventID, userID, and imageID so that skeletons of these paths can be populated correctly.

Is this the recommended and / or appropriate way to solve this problem? Am I doing it wrong?

+3


source to share





All Articles