Firebase (NoSQL): Denormalization vs Indexing

Suppose I want to write a blogging application. Should I use one of the following two options? I would prefer to have as many โ€œone source of truthโ€ as possible, but I'm still not sure if this preference comes from my background in SQL.

Option 1 (Denormalization):

Posts: { post_1: { title: "hello", body: "hi there!", uid: "user_1", comments: { comment_1: { body: "hi I commented", uid: "user_2", }, comment_2: { body: "bye I commented", uid: "user_2", }, } } } Users: { user_1: { uid: "user_1", post_1: { title: "hello", body: "hi there!", uid: "user_1", comments: { comment_1: { body: "hi I commented", uid: "user_2", }, comment_2: { body: "bye I commented", uid: "user_2", }, } } } } 

Option 2 (indexing):

 Posts: { post_1: { title: "hello", body: "hi there!", uid: "user_1", authorName: "Richard", comments: { comment_1: true, comment_2: true } } } Users: { user_1: { uid: "user_1", displayName: "Richard", email: " richard@gmail.com ", posts: { post_1: true }, comments: { comment_1: true, comment_2: true } } } Comments: { comment_1: { body: "hi I commented", uid: "user_1", }, comment_2: { body: "bye I commented", uid: "user_1", }, } 

I think I should choose option 2.

The main problem that I see with option 1 is that there are too many sources for the same data. Let's say I want to expand the application so that each post belongs to a certain category or tag. Then I will have to write a post object in /categories/category_id in addition to /posts and /users/uid . When a post is updated, I have to remember changing the post object in three different places. If I go with option 2, I do not have this problem because there is only one data source.

Did I miss something?

Literature:

+2
source share
1 answer

The second option is better, because otherwise you would force the user to download all comments and messages (this can be a lot).

You can check the documentation here .

And you can handle duplication by atomic recording in several places .

-one
source

All Articles