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: