Question

I just read the Firebase blog post titled Denormalizing Your Data Is Normal, and I have request for clarification.

I was with it until the Considerations paragraph. Specifically, the following:

"Modification of comments is easy: just set the value of the comment under /comments to the new content. For deletion, simply delete 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 was deleted and proceed normally"

For modifications, why don't I have to modify the duplicate comments stored under /links and /users?

For deletions, am I correct in my understanding that once I delete a comment I have to have logic in all my read logic to cross-check /comments in case it was deleted?

Thanks!

Was it helpful?

Solution

The structure detailed in the blog post does not store duplicate comments. We store comments once under /comments then store the name of those comments under /links and /users. These function as pointers to the actual comment data.

Consider the example structure from the post...

{
  users: {
    user1: {
      name: "Alice",
      comments: {
        comment1: true
      }
    },
  },
  comments: {
    comment1: {
      body: "This is awesome!",
      author: "user1"
    }
  }
}

Note that the actual comment data is only stored once.

If we modify /comments/comment1, we don't need to update anything else because we only store the name of the comment under /links and /users, not the actual comment contents.

If we were to remove /comments/comment1, that would remove the only existence of the comment data. However, we still have these "dangling" references to comment1 under /users/user1/comments.

Imagine we delete /comments/comment1, when we try to load Alice's comments, we can look and see that comment1 doesn't exist anymore. Then our application can react accordingly by either a) deleting the reference or b) ignoring the reference and not trying to display the deleted comment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top