1 00:00:00,210 --> 00:00:05,700 In this video to kick off the section we're going to explore a new tool that will be making heavy use 2 00:00:05,700 --> 00:00:09,390 of as we create the task manager application. 3 00:00:09,420 --> 00:00:17,400 This is the very popular NPM module called Mongoose now mongoose is directly related to mongo D.B. and 4 00:00:17,400 --> 00:00:22,190 it's going to allow us to do some of the things that we don't know how to do so far. 5 00:00:22,200 --> 00:00:29,340 For example we know how to use Mongo D.B. to create documents read a documents update and delete them. 6 00:00:29,370 --> 00:00:31,870 But what about other equally important things. 7 00:00:31,890 --> 00:00:38,700 For example how do I set up validation for my documents defining which fields are required and which 8 00:00:38,760 --> 00:00:42,650 are optional and what types of data I expect for them. 9 00:00:42,720 --> 00:00:48,720 For example I don't want someone to be able to set that completed field equal to a string. 10 00:00:48,900 --> 00:00:51,440 I want to require that it's a boolean. 11 00:00:51,480 --> 00:00:55,020 Now with Mongoose we can also do other things we're not able to do. 12 00:00:55,050 --> 00:01:00,490 For example how do we say that a given task was created by a user. 13 00:01:00,510 --> 00:01:05,160 Eventually we're going to add authentication to the task manager application. 14 00:01:05,160 --> 00:01:13,320 Now if me User 1 creates a new task I don't want some other user like user 34 to be able to read that 15 00:01:13,320 --> 00:01:19,410 data and see the private task I created mongoose is going to allow us to get all of those new things 16 00:01:19,410 --> 00:01:26,260 done while still being able to perform our basic CRUD operations on the Mongo D.B. database. 17 00:01:26,280 --> 00:01:31,800 Now before we integrate anything into our project let's view mongoose in the browser. 18 00:01:31,890 --> 00:01:36,140 You can find it over at Mongoose J S dot com. 19 00:01:36,240 --> 00:01:39,970 And the reason I'm bringing you here is to just show you where you can learn more. 20 00:01:40,020 --> 00:01:45,630 They have a pretty decent set of documentation and for the moment what I want to do is just quickly 21 00:01:45,630 --> 00:01:49,230 go over this five line example down below. 22 00:01:49,230 --> 00:01:52,010 Now it's OK if this code is no longer here. 23 00:01:52,110 --> 00:01:55,200 Maybe they updated the site and got rid of this example. 24 00:01:55,230 --> 00:02:00,860 You can just watch as long as I quickly give you an overview of what mongoose is going to let us do. 25 00:02:00,870 --> 00:02:04,340 So Mongoose right here is just connecting to the database. 26 00:02:04,350 --> 00:02:11,190 And the next thing it does is it creates what's known as a model a model allows us to model something 27 00:02:11,190 --> 00:02:14,970 in the real world that we want to be able to store in the database. 28 00:02:14,970 --> 00:02:19,290 So this could be a user or a task or in this case a cat. 29 00:02:19,320 --> 00:02:26,010 If we were creating an IKEA web site using Mongoose we could model the various pieces of furniture that 30 00:02:26,010 --> 00:02:32,430 we're selling including their names their prices dimensions and any other actor buttes we might have. 31 00:02:32,430 --> 00:02:38,280 So we create models for all of the collections that we want and then we use the model to describe the 32 00:02:38,280 --> 00:02:39,180 data. 33 00:02:39,180 --> 00:02:44,080 Now in this case for this basic example there's almost nothing to the model. 34 00:02:44,070 --> 00:02:47,890 There's no validation and there's nothing fancy which we'll explore shortly. 35 00:02:48,270 --> 00:02:53,830 All they're doing is saying that a cat has a name and the name should be a string. 36 00:02:53,850 --> 00:02:58,880 We'll learn how to customize the models further as we progress through this section. 37 00:02:59,040 --> 00:03:00,590 Now down below what do they do. 38 00:03:00,750 --> 00:03:04,050 They create a new instance of a cat right here. 39 00:03:04,080 --> 00:03:10,620 They have the kitty variable which stores that new instance and they set the name equal to the following. 40 00:03:10,710 --> 00:03:15,650 Then down below they use various methods on that model to manipulate it. 41 00:03:15,660 --> 00:03:19,380 So right here they use Kitty dot save to save the data. 42 00:03:19,620 --> 00:03:26,340 And this provides a much nicer interface for interacting with Mongo D.B. instead of needing to use all 43 00:03:26,340 --> 00:03:29,880 of the low level API methods which we explored. 44 00:03:29,880 --> 00:03:35,290 We'll be able to use methods that make a bit more sense when we're writing code like save now among 45 00:03:35,290 --> 00:03:39,580 goose falls into a broader category of tools no one as O.D. Ms. 46 00:03:39,720 --> 00:03:43,600 And OTM stands for object document mapper. 47 00:03:43,680 --> 00:03:49,920 And as that name suggests the goal is to allow you to map your objects in your code. 48 00:03:50,010 --> 00:03:58,750 So for us objects in our no JSA application over two documents inside of the Mongo D.B. database. 49 00:03:58,830 --> 00:04:02,070 And that's exactly what mongoose is going to let us do. 50 00:04:02,070 --> 00:04:07,890 So let's go ahead and get started with Mongoose by heading over to visual studio code installing it 51 00:04:07,980 --> 00:04:11,000 setting it up and working through a basic example. 52 00:04:11,130 --> 00:04:17,760 I'm going to navigate over to V.S. C. And we're going to run the NPM install command from the terminal. 53 00:04:17,760 --> 00:04:25,740 Once again make sure you have navigated to the task manager directory that's going to be NPM AI Mongoose 54 00:04:25,890 --> 00:04:30,770 at the latest version five point three point sixteen. 55 00:04:30,780 --> 00:04:35,490 Now we're gonna go ahead and run the installation command and while that's running we can start the 56 00:04:35,490 --> 00:04:38,410 process of setting up the real director restructure. 57 00:04:38,430 --> 00:04:43,100 We're going to end up using to explore Mongo D.B. at a basic level. 58 00:04:43,170 --> 00:04:45,790 We did all of that in just a single file. 59 00:04:45,810 --> 00:04:52,000 The task manager app though is gonna get much bigger than any of the applications we've built so far. 60 00:04:52,050 --> 00:04:58,350 So we want to make sure that we do take into account a directory and file structure that allows us to 61 00:04:58,350 --> 00:05:05,590 scale the application out to its complex large size without having everything in a single file. 62 00:05:05,600 --> 00:05:11,720 So right here we're gonna start by creating a new folder that is going to be the source directory. 63 00:05:11,960 --> 00:05:17,390 And this is where the application source code will live and for the moment we're going to create one 64 00:05:17,390 --> 00:05:22,170 more directory and one file inside of the source directory. 65 00:05:22,280 --> 00:05:28,790 We're going to create a D.B. folder which is where we'll store things related to the database and we're 66 00:05:28,790 --> 00:05:36,440 going to add just a single file inside of there and we'll call that Mongoose dot J S inside of here 67 00:05:36,470 --> 00:05:42,470 we're going to mess around with the basics of Mongoose and we'll continue to use Mongoose as we build 68 00:05:42,470 --> 00:05:45,130 out the task manager API to start. 69 00:05:45,170 --> 00:05:50,020 Let's go ahead and load mongoose in so I'm going to create a mongoose constant. 70 00:05:50,120 --> 00:05:55,700 Then I'll go ahead and use require it to load in the Mongoose NPM library. 71 00:05:55,700 --> 00:06:03,920 And once we have that require call in place the next thing we do is use Mongoose dot connect to connect 72 00:06:03,920 --> 00:06:10,280 to the database now Mongoose dot connect is pretty similar to mongo client not connect. 73 00:06:10,280 --> 00:06:15,950 We are going to provide the connection you are well as the first argument and we are also going to provide 74 00:06:15,950 --> 00:06:19,330 that options object as the second argument. 75 00:06:19,340 --> 00:06:26,420 So remember Mongoose uses the Mongo D.B. module behind the scenes so everything we do in mongoose is 76 00:06:26,420 --> 00:06:29,270 playing off the abilities of this library. 77 00:06:29,390 --> 00:06:34,580 So everything we learned about in the last video is completely relevant to Mongoose right here. 78 00:06:34,580 --> 00:06:39,250 We'll provide our U.R.L. and we'll provide our options object. 79 00:06:39,350 --> 00:06:42,650 Now for the U.R.L. it's gonna be just slightly different. 80 00:06:42,650 --> 00:06:45,310 Everything starts off the exact same way. 81 00:06:45,620 --> 00:06:56,930 So it's Mongo D.B. colon forward slash forward slash then R IP 1 2 7 0 0 dot one followed by a colon 82 00:06:56,990 --> 00:07:00,420 for the port 2 7 0 1 7. 83 00:07:00,440 --> 00:07:04,560 Now this is the exact U.R.L. we have over here. 84 00:07:04,640 --> 00:07:10,200 The one change we're going to make is that we don't specify the database name separately. 85 00:07:10,220 --> 00:07:16,730 Instead we provide to that right in here as part of the connection string for us that's gonna be forward 86 00:07:16,730 --> 00:07:19,700 slash followed by the database name. 87 00:07:19,700 --> 00:07:24,910 In this case I can use task manager API. 88 00:07:24,920 --> 00:07:31,040 Now notice I did pick a different name than the database we already have in place as the Mongoose data 89 00:07:31,070 --> 00:07:32,550 is going to look different. 90 00:07:32,600 --> 00:07:38,690 So right here we have that connection string in place and now we can move on to the options and we are 91 00:07:38,690 --> 00:07:45,970 going to configure use new you are all parser exactly like we had before. 92 00:07:46,010 --> 00:07:49,660 And once again we will be setting that to the Boolean true. 93 00:07:49,670 --> 00:07:52,100 Now there is one more option we're going to configure. 94 00:07:52,100 --> 00:07:57,510 Also setting to true that is use create index. 95 00:07:57,530 --> 00:08:04,400 This is going to make sure that when Mongoose works with Mongo D.B. our indexes are created allowing 96 00:08:04,400 --> 00:08:08,160 us to quickly access the data we need to access. 97 00:08:08,180 --> 00:08:12,760 So right here this is all that it takes to actually connect to the database. 98 00:08:12,770 --> 00:08:20,090 Now we can focus on the fun stuff which is to define our very first model together we'll define a basic 99 00:08:20,090 --> 00:08:22,100 version of the user model. 100 00:08:22,100 --> 00:08:28,580 Then as your challenge for the video you'll be defining a basic version of the task model. 101 00:08:28,580 --> 00:08:34,640 Now I'm saying basic version because as we progress through the next couple of sections we'll continue 102 00:08:34,640 --> 00:08:37,330 to improve and build out these models. 103 00:08:37,370 --> 00:08:42,830 Eventually they'll all live in their own files and there'll be a lot of code associated with them to 104 00:08:42,830 --> 00:08:45,020 do all sorts of interesting things. 105 00:08:45,020 --> 00:08:49,550 For now though we're going to start with just the bare bones minimum setup. 106 00:08:49,550 --> 00:08:54,980 The first thing we need to do is define the model we're working with and as mentioned we'll be creating 107 00:08:55,010 --> 00:08:56,940 the user model together. 108 00:08:56,960 --> 00:09:03,860 So right here we'll make a new constant call that user with an uppercase you then to actually generate 109 00:09:03,890 --> 00:09:04,240 this. 110 00:09:04,260 --> 00:09:10,570 We use Mongoose dot model and Mongoose dot model accepts two arguments. 111 00:09:10,580 --> 00:09:13,730 The first is the string name for your model. 112 00:09:13,730 --> 00:09:18,470 And the second is the definition where we define all of the fields we want. 113 00:09:18,470 --> 00:09:24,190 Now for our purposes we will call this a user once again with an uppercase you. 114 00:09:24,230 --> 00:09:28,510 And now we can determine what fields we want to have for now. 115 00:09:28,550 --> 00:09:33,460 Let's go ahead and set up something like a name and an age for each user. 116 00:09:33,470 --> 00:09:35,030 That's exactly what we had. 117 00:09:35,030 --> 00:09:41,390 Any last section when working with just the Mongo D.B. NPM library to do that. 118 00:09:41,390 --> 00:09:46,690 What we do is we set up all of those fields as properties on this object. 119 00:09:46,730 --> 00:09:50,800 So I'm going to set up a name whose value will be an object. 120 00:09:50,930 --> 00:09:56,300 And I'm also going to set up age whose value is also going to be an object. 121 00:09:56,420 --> 00:09:59,900 And in here we can figure things about that field. 122 00:09:59,900 --> 00:10:01,790 We can configure validation. 123 00:10:01,790 --> 00:10:08,840 We can set up custom validation but at its most basic we can set up the type for the fields we're working 124 00:10:08,840 --> 00:10:09,350 with. 125 00:10:09,350 --> 00:10:12,130 And that's exactly what we're going to end up starting with. 126 00:10:12,140 --> 00:10:17,410 So to get this done what we do is we set up a type property on each object. 127 00:10:17,420 --> 00:10:24,920 So right here for a name we're going to set the type equal to a string right here uppercase string. 128 00:10:25,130 --> 00:10:29,720 Then down below for age we'll be setting this equal to a number. 129 00:10:29,720 --> 00:10:33,050 So the type will be uppercase N number. 130 00:10:33,410 --> 00:10:39,230 So here I'm just using the constructor functions from JavaScript as the value for type. 131 00:10:39,320 --> 00:10:46,670 Now Mongoose supports plenty of other types as well for things like boolean dates arrays binary data 132 00:10:46,760 --> 00:10:52,810 object I.D. and more but these are two basic types that we'll be using to get started with now that 133 00:10:52,820 --> 00:11:00,020 we have our model defined we can create instances of that model to actually add documents to the database. 134 00:11:00,020 --> 00:11:07,830 So down below I wanted to make a new user what I would do is first create a variable to store that user. 135 00:11:07,850 --> 00:11:11,510 I could just use something like me any name would work. 136 00:11:11,510 --> 00:11:18,440 And right here we use the new operator with our user variable which is a constructor function for that 137 00:11:18,440 --> 00:11:19,280 model. 138 00:11:19,280 --> 00:11:22,390 So it's new uppercase U user. 139 00:11:22,520 --> 00:11:29,270 We call this as a function which it is and we pass to an object with all of the data for this particular 140 00:11:29,300 --> 00:11:29,840 user. 141 00:11:30,140 --> 00:11:33,620 So for me I would set name equal to my name. 142 00:11:33,650 --> 00:11:37,670 A string matching up with the type for name up above. 143 00:11:37,670 --> 00:11:42,950 And then I would also set up age right here setting that equal to a number. 144 00:11:42,950 --> 00:11:43,540 Perfect. 145 00:11:44,090 --> 00:11:47,470 Now we have an instance of our model created me. 146 00:11:47,540 --> 00:11:53,120 But at this point nothing's actually getting saved to the database to save to the database. 147 00:11:53,120 --> 00:11:57,080 What we do is we use methods on our instance. 148 00:11:57,080 --> 00:12:03,320 So on here we have methods for doing all sorts of interesting things for those CRUD operations in order 149 00:12:03,320 --> 00:12:06,010 to save the model instance. 150 00:12:06,080 --> 00:12:09,610 What we do is we use the save method. 151 00:12:09,610 --> 00:12:12,100 Now the save method doesn't take any arguments. 152 00:12:12,110 --> 00:12:18,860 It's simply saving the data that we've stored and This returns a promise so we can use our two promise 153 00:12:18,860 --> 00:12:25,070 methods which we've explored pretty extensively at this point to actually wait for the saving process 154 00:12:25,070 --> 00:12:32,240 to finish right here and then to register a handler for when the promise is fulfilled and then down 155 00:12:32,240 --> 00:12:39,140 below dot catch to register a handler for when the given promise is rejected in a minute will actually 156 00:12:39,140 --> 00:12:44,480 trigger an error to make sure this catch handler does run when we have invalid data. 157 00:12:44,480 --> 00:12:50,380 So I'll grab error and we'll make sure to dump that down below console log. 158 00:12:50,510 --> 00:12:57,030 I'll print these string error to start then as the second argument I'll just dump the variable to the 159 00:12:57,030 --> 00:12:58,930 terminal up above. 160 00:12:58,950 --> 00:13:04,560 When things go well well we get access to is our model instance once again. 161 00:13:04,560 --> 00:13:06,760 So that would be me from right here. 162 00:13:06,810 --> 00:13:12,300 I can choose to name it but since it's the same thing I could always just access the existing me which 163 00:13:12,300 --> 00:13:14,400 I already have right here. 164 00:13:14,400 --> 00:13:18,420 Console dialogue and I'll just dump me to the terminal. 165 00:13:18,420 --> 00:13:19,370 So what are we doing. 166 00:13:19,380 --> 00:13:25,260 We're defining a model we're creating an instance of it and we are saving that instance to the database. 167 00:13:25,260 --> 00:13:27,330 Let's go ahead and test our work. 168 00:13:27,330 --> 00:13:31,150 I'm gonna save Mongoose dot J S and run it from the terminal. 169 00:13:31,200 --> 00:13:36,600 Make sure you still have your Mongo DB database running in that other terminal tab. 170 00:13:36,720 --> 00:13:41,340 Right here I'll kick things off by using clear to clear the terminal output. 171 00:13:41,340 --> 00:13:51,510 Then I'll use node source forward slash D.B. forward slash Mongoose dot J S I'm going to run the file 172 00:13:51,690 --> 00:13:54,870 it's going to go through the process of saving that user. 173 00:13:54,960 --> 00:13:58,170 And what do I get back I get back our document. 174 00:13:58,170 --> 00:14:03,270 I have my I.D. I have my name and I have my age all showing up. 175 00:14:03,270 --> 00:14:08,200 Now we also have one additional property added and managed by mongoose. 176 00:14:08,220 --> 00:14:10,940 This is underscore underscore V. 177 00:14:10,980 --> 00:14:16,230 This store is the version of the document and right here we are starting at 0. 178 00:14:16,230 --> 00:14:21,290 Next up we're gonna head over to robo 3 TI and view our data never member. 179 00:14:21,330 --> 00:14:25,400 We created a brand new database when we're working with mongoose. 180 00:14:25,410 --> 00:14:31,830 If I right click the connection and click refresh I can see that show up just down below. 181 00:14:31,830 --> 00:14:35,190 I'm going to close the tabs for the other database. 182 00:14:35,190 --> 00:14:40,410 I'll then right click it and we can actually drop that database removing it completely. 183 00:14:40,440 --> 00:14:45,010 The only one we're going to need is the task manager api database. 184 00:14:45,030 --> 00:14:48,010 If I crack that open we have our collections. 185 00:14:48,060 --> 00:14:52,110 We have a user's collection and if I open that up what do we have. 186 00:14:52,140 --> 00:14:55,890 We have the one document which we just explored right here. 187 00:14:55,890 --> 00:15:01,910 I have the exact same fields and values which I saw over inside of the terminal. 188 00:15:01,950 --> 00:15:06,800 Now that we've seen things go well let's watch things go poorly to show this off. 189 00:15:06,810 --> 00:15:13,500 What I'm going to do is mess up the age value so age needs to be a number we're going to make it not 190 00:15:13,500 --> 00:15:16,560 a number and we'll rerun the file right here. 191 00:15:16,560 --> 00:15:21,300 I'm going to set up a string and I'll just put something in like the name Mike. 192 00:15:21,300 --> 00:15:21,840 Perfect. 193 00:15:22,230 --> 00:15:29,490 Now we can save Mongoose dot J S we can rerun the file down below and what we're going to see is a very 194 00:15:29,490 --> 00:15:36,360 long object printing the long and the short of it though is that this is a validation object letting 195 00:15:36,360 --> 00:15:43,080 us know that things went wrong and describing what exactly went wrong and we're going to explore validation 196 00:15:43,080 --> 00:15:45,800 more as we progressed through the sections. 197 00:15:45,840 --> 00:15:52,620 For now I just wanted to show you that when we do use Mongoose we do get a base level of validation 198 00:15:52,620 --> 00:15:55,800 from the start and we can customize that as we progressed. 199 00:15:55,800 --> 00:16:01,830 For example I could say that you need to be an adult so your age needs to be greater than or equal to 200 00:16:01,860 --> 00:16:02,780 18. 201 00:16:02,820 --> 00:16:08,040 Or we could validate your e-mail making sure it has all of the correct components. 202 00:16:08,040 --> 00:16:12,450 So that's actually where we're going to stop for this video in robo 3 T. 203 00:16:12,540 --> 00:16:17,650 If I do refresh the data we'll see we still just have our one document. 204 00:16:17,660 --> 00:16:20,770 The other document we tried to create was invalid. 205 00:16:20,820 --> 00:16:23,500 So Mongoose did not let us create it. 206 00:16:23,700 --> 00:16:27,750 Now I said there was gonna be a challenge where you'll create the task model. 207 00:16:27,750 --> 00:16:30,660 This video ran a little longer than I wanted it to. 208 00:16:30,690 --> 00:16:33,500 So I'm going to push that back to the next video. 209 00:16:33,510 --> 00:16:35,230 I'm very excited to get to it. 210 00:16:35,250 --> 00:16:37,590 So let's go ahead and jump right in.