1
00:00:00,180 --> 00:00:06,180
Let's look at how we are going to model our data for this application.

2
00:00:06,870 --> 00:00:13,830
The most difficult part when it comes to building applications as a Fullstack developer or as a back

3
00:00:13,830 --> 00:00:18,570
end developer is how the data is going to be modular.

4
00:00:18,600 --> 00:00:21,010
That is based on my experience.

5
00:00:21,030 --> 00:00:26,190
So let's have a look at the logic behind the data flow.

6
00:00:26,550 --> 00:00:30,090
We are going to build a block application.

7
00:00:30,090 --> 00:00:37,170
So for block application, first step is we need to ask ourselves what are the users, who is going

8
00:00:37,170 --> 00:00:40,930
to use our application and what kind of data that we need.

9
00:00:40,950 --> 00:00:42,990
So let's look at the data flow.

10
00:00:43,020 --> 00:00:51,420
First one is we need users to use the application and for users we're going to have two types or three

11
00:00:51,420 --> 00:00:54,190
types based on your business logic.

12
00:00:54,210 --> 00:01:03,960
One is we can have the admin and then the super admin or any normal user, but for our case we are going

13
00:01:03,960 --> 00:01:07,310
to have two categories of users.

14
00:01:07,320 --> 00:01:11,970
One is authenticated user and then normal user.

15
00:01:11,970 --> 00:01:18,840
When we say authenticated, meaning that a user who has logged in and then the person can create and

16
00:01:18,840 --> 00:01:20,400
then delete a post.

17
00:01:20,400 --> 00:01:24,840
But for a normal user, the person can only view the post.

18
00:01:24,840 --> 00:01:32,710
So the next entity or data that we need to handle is what we call post, because the entire logic about

19
00:01:32,730 --> 00:01:35,520
application is about creating posts.

20
00:01:36,000 --> 00:01:43,740
The next concept is what we call comment because a user can comment on your post.

21
00:01:43,740 --> 00:01:49,530
So we need to have a separate entity or data for comment.

22
00:01:49,530 --> 00:01:53,850
And lastly, we are going to have what is called categories.

23
00:01:54,120 --> 00:02:00,060
These entities is what we call model and mongoose.

24
00:02:00,180 --> 00:02:07,170
So let's say how these models or entities or data are related.

25
00:02:07,170 --> 00:02:16,530
And this is what we call database relationship, meaning how comment is being related to post and how

26
00:02:16,530 --> 00:02:20,880
post is being related to categories, users and comment.

27
00:02:21,710 --> 00:02:27,890
And for Mongoose, we have different types of database relationship.

28
00:02:27,920 --> 00:02:35,330
One is called 1 to 1 relationship, one to many or many to many relationships.

29
00:02:35,780 --> 00:02:41,000
So let's see how these models are going to relate to each other.

30
00:02:41,270 --> 00:02:46,190
First step is let's focus on the users and then the post.

31
00:02:46,220 --> 00:02:51,560
Is it going to be 1 to 1 or one to many or many to many?

32
00:02:52,100 --> 00:03:01,520
In this case, it's going to be one to many relationships, meaning that a user can create many posts.

33
00:03:01,880 --> 00:03:10,880
So if a user view my profile or if I view my profile, I'll be able to see all the posts that I have

34
00:03:10,880 --> 00:03:11,810
created.

35
00:03:12,020 --> 00:03:17,520
The next one going to be the users and then the comment.

36
00:03:17,540 --> 00:03:21,540
Again, a user can create many comments.

37
00:03:21,560 --> 00:03:26,510
So in this case it's going to be one to many relationships.

38
00:03:26,810 --> 00:03:33,770
A user can create many comments, but one comment belongs to a single user.

39
00:03:33,800 --> 00:03:40,850
You can see that this one represents this user and then the red represents this one as well.

40
00:03:41,060 --> 00:03:50,150
So this show is that a comment belongs to a single user and for post and then the user is like I said,

41
00:03:50,150 --> 00:03:53,450
is going to be one to many relationships.

42
00:03:53,480 --> 00:04:02,450
So if I view a single post or be able to see a single user who created that post, I won't be able to

43
00:04:02,450 --> 00:04:07,370
see two or more users who has created a single post.

44
00:04:07,550 --> 00:04:16,820
The next one going to be the users and category for this one to a user can create many categories,

45
00:04:16,820 --> 00:04:21,180
but one category belongs to a single user.

46
00:04:21,200 --> 00:04:25,460
So this is what we call one to many relationship.

47
00:04:25,640 --> 00:04:29,240
So let's look at the comments and posts.

48
00:04:29,240 --> 00:04:32,410
What kind of relationship are you going to use?

49
00:04:32,420 --> 00:04:42,410
Definitely is going to be one to many relationships, meaning that a single post can have many comments,

50
00:04:42,410 --> 00:04:47,050
but one comment belongs to a single post.

51
00:04:47,060 --> 00:04:48,980
So what about post?

52
00:04:48,980 --> 00:04:56,780
And then the category is going to be 1 to 1 relationship, or it can be one to many relationships,

53
00:04:56,780 --> 00:05:06,450
meaning that a post can belongs to two or more categories, but ideally a post belongs to a single category.

54
00:05:06,470 --> 00:05:08,810
For this one, it's up to you.

55
00:05:08,810 --> 00:05:13,520
If you want a post to belong to two or more categories.

56
00:05:13,760 --> 00:05:15,640
So this is the flow.

57
00:05:15,650 --> 00:05:18,440
This is the relationship that we are going to use.

58
00:05:18,830 --> 00:05:27,920
Next is let's talk about how we are going to say the document into the model, for example, users and

59
00:05:27,920 --> 00:05:28,670
posts.

60
00:05:28,850 --> 00:05:32,150
For this one too, we have ways of doing it.

61
00:05:32,150 --> 00:05:40,700
When it comes to MongoDB, one is called using the embedded way, meaning that we are going to save

62
00:05:40,700 --> 00:05:49,550
the entire post into the user's field and this is what we call embedded, meaning we are using the entire

63
00:05:49,580 --> 00:05:59,060
post into the arrays of user field and we have the nest type and that is what you call by referencing

64
00:05:59,060 --> 00:06:06,470
meaning that instead of using the entire object, we are going to use only the idea of the post and

65
00:06:06,470 --> 00:06:09,110
save it into the user's field.

66
00:06:09,380 --> 00:06:15,950
And this is the recommended way of doing it that is using the reference way.

67
00:06:16,130 --> 00:06:23,960
Before we continue for our purpose, we are not going to create a separate model for categories, but

68
00:06:23,960 --> 00:06:30,660
instead we are going to create a field and then pass in the accepted categories.

69
00:06:30,680 --> 00:06:34,750
Well, as we proceed, all this concept will be clear to you.

70
00:06:34,760 --> 00:06:37,460
All right, Let's continue in the next video.

