0 1 00:00:00,430 --> 00:00:04,510 Hi guys. Welcome to a new module. Now a few modules back 1 2 00:00:04,560 --> 00:00:06,950 we built those beautiful to do list. 2 3 00:00:07,170 --> 00:00:13,680 And in the process we learnt about a whole bunch of things such as how to use EJS templates and how 3 4 00:00:13,680 --> 00:00:19,800 to pass data between your server and your EJS files. 4 5 00:00:19,810 --> 00:00:24,820 Now there's just one problem with this app as it is though. 5 6 00:00:25,080 --> 00:00:31,720 Because if we added in a new item, let's say "Test" and it looks great 6 7 00:00:31,740 --> 00:00:34,410 and you know when we refresh it it's still there. 7 8 00:00:34,590 --> 00:00:38,700 But if I go ahead and just restart my server, 8 9 00:00:38,700 --> 00:00:42,450 so at the moment I'm running app.js using nodemon. 9 10 00:00:42,510 --> 00:00:51,060 So if I just type in "rs" into the command line and hit enter then it will restart my nodemon server. 10 11 00:00:51,660 --> 00:00:59,310 And this is equivalent to say if you are running your app using node app.js then every single time 11 12 00:00:59,310 --> 00:01:06,630 you make a change in your app.js then you would have to rerun node app.js and nodemon basically 12 13 00:01:06,630 --> 00:01:08,850 does that automatically behind the scenes. 13 14 00:01:08,910 --> 00:01:14,020 But you can also just get it to restart the server by just typing "rs". 14 15 00:01:14,040 --> 00:01:16,640 So now we've restarted our server. 15 16 00:01:16,890 --> 00:01:24,600 And if I now take a look at our localhost:3000 if I go ahead and refresh this website you can see 16 17 00:01:24,600 --> 00:01:27,760 that that item that I just added disappears. 17 18 00:01:27,900 --> 00:01:34,620 And the reason for this is because currently all of these items are stored inside an array and every 18 19 00:01:34,620 --> 00:01:41,610 single time I restart my app.js, I rerun it, then it wipes that array to the default items that 19 20 00:01:41,610 --> 00:01:42,780 we began with. 20 21 00:01:43,020 --> 00:01:50,460 And all of the new items I added gets wiped out and it's no longer saved in our app which is not a great 21 22 00:01:50,460 --> 00:01:52,190 user experience to be honest. 22 23 00:01:53,110 --> 00:02:00,790 Now that we've learned about databases and how to persist our data then this is a perfect use case. 23 24 00:02:00,790 --> 00:02:08,290 So we're going to update this app to start using Mongo D-B to save our data to a Mongar D-B database 24 25 00:02:08,560 --> 00:02:14,950 and you'll see that afterwards no matter how much I restart my server or whatever changes I decide to 25 26 00:02:14,950 --> 00:02:21,990 make to my app that G-S or the rest of my web site all of my data will be safely saved in our database. 26 27 00:02:22,000 --> 00:02:23,540 So let's get started. 27 28 00:02:24,760 --> 00:02:29,890 So in the last lesson you should have gotten hold of the starting files for the second version of our 28 29 00:02:29,890 --> 00:02:30,790 To-Do list. 29 30 00:02:30,850 --> 00:02:36,070 If you go ahead and just unzip it and place it on your desktop or into your development folder then 30 31 00:02:36,130 --> 00:02:39,610 we can drag it on to atom and we can get started. 31 32 00:02:39,880 --> 00:02:45,210 Now the first thing you'll notice is that we don't actually have a node modules folder. 32 33 00:02:45,580 --> 00:02:50,320 And that is of course because whenever we download something from the internet usually it won't come 33 34 00:02:50,320 --> 00:02:51,410 with the node modules. 34 35 00:02:51,490 --> 00:02:59,650 So we have to C.D over to the location of our folder and then just run NPM install and PM I in that 35 36 00:02:59,770 --> 00:03:00,790 folder. 36 37 00:03:01,390 --> 00:03:08,300 And then Ill go and look at all the dependencies we have in our package doc Jaison for example Paudie 37 38 00:03:08,380 --> 00:03:15,220 posit Ejay as an express and it will install it and create our node modules folder. 38 39 00:03:15,670 --> 00:03:22,300 So now that we're ready to upgrade to our to do list to a version that actually has a database on the 39 40 00:03:22,300 --> 00:03:28,810 back and then the first thing we should look at is how are we storing our data currently. 40 41 00:03:28,810 --> 00:03:35,610 Well we currently have this constant called items which has an array of default objects to begin with. 41 42 00:03:35,860 --> 00:03:44,650 And then when we add or post new items into our abduct Geass then we push those items into the relevant 42 43 00:03:44,980 --> 00:03:45,730 arrays. 43 44 00:03:45,730 --> 00:03:51,660 So for example the items array or the work items arraying. 44 45 00:03:51,730 --> 00:03:59,150 Now we're going to delete all of these arrays and we're going to be using Mongo and Mongoose instead. 45 46 00:03:59,440 --> 00:04:04,520 So the first thing we have to do is we have to install mongoose of course. 46 47 00:04:04,570 --> 00:04:09,890 So let's go ahead and run NPM install mongoose. 47 48 00:04:09,910 --> 00:04:12,880 Now that should add that to our package. 48 49 00:04:13,080 --> 00:04:18,130 Jason and ill add the relevant modules into our node modules folder. 49 50 00:04:18,130 --> 00:04:25,790 So now that we've got access to our node modules we're going to now require our mongoose. 50 51 00:04:25,870 --> 00:04:33,490 So we're going to create a constant cord mongoose and it's going to be requiring the mongoose package 51 52 00:04:33,490 --> 00:04:35,690 that we just installed. 52 53 00:04:36,130 --> 00:04:43,130 The next thing that we need to do is to go ahead and create a new database inside. 53 54 00:04:43,150 --> 00:04:50,860 DP The way that we do that as we've seen before in previous modules is Munger's stock connect and then 54 55 00:04:50,950 --> 00:04:57,880 the thing that we're going to connect to is the you are rle where Mongo D-B is hosted locally which 55 56 00:04:57,940 --> 00:04:58,760 is traditionally. 56 57 00:04:58,770 --> 00:05:08,240 Mongo D-B Kahlon to Fort's lashes localhost Colan 2 7 0 1 7. 57 58 00:05:08,440 --> 00:05:14,410 And then we're going to write one further forward slash and we're going to specify the name of our database 58 59 00:05:14,770 --> 00:05:19,440 which in this case I'm just going to call to do list D-B. 59 60 00:05:19,990 --> 00:05:26,140 And then finally remember if we wanted to not have that little warning show up if we want to avoid that 60 61 00:05:26,140 --> 00:05:33,650 deprecation warning then we have to add that little flag in there which is use New York or El Paso. 61 62 00:05:34,030 --> 00:05:35,430 Coal on true. 62 63 00:05:35,560 --> 00:05:40,510 And at some point in the future you won't have to do this anymore because mongoose and Mongo will update 63 64 00:05:40,570 --> 00:05:41,580 their card. 64 65 00:05:41,680 --> 00:05:47,290 But for now if you're seeing that deprecation warning when you don't have this in then this is the way 65 66 00:05:47,290 --> 00:05:49,240 to work around it. 66 67 00:05:50,200 --> 00:05:57,520 So now that we specified how we're going to connect to our Mongar D.B which database we wanted to create 67 68 00:05:57,550 --> 00:05:59,270 or connect to. 68 69 00:05:59,530 --> 00:06:02,630 Then the next step is to create a new schema. 69 70 00:06:02,650 --> 00:06:03,180 Right. 70 71 00:06:03,340 --> 00:06:06,620 And we want to create a new items schema. 71 72 00:06:06,970 --> 00:06:09,580 I'm going to leave this up to you as a challenge. 72 73 00:06:09,790 --> 00:06:15,460 I want you to create something called items schema and this is only going to have one field which is 73 74 00:06:15,460 --> 00:06:22,480 called name and the names of each of our items are simply going to be of data type string. 74 75 00:06:22,540 --> 00:06:26,100 Remember this is roughly what a mongoose schema would look like. 75 76 00:06:26,110 --> 00:06:34,240 We have the Konski word then we specify the schema name and then we set it to equal a new javascript 76 77 00:06:34,300 --> 00:06:37,100 object with a key value field. 77 78 00:06:37,360 --> 00:06:44,060 And the key is going to be the name of the field and the value is going to be the data type of field. 78 79 00:06:44,170 --> 00:06:45,890 So you've done this before. 79 80 00:06:45,910 --> 00:06:48,470 Post the video and try to complete the challenge. 80 81 00:06:50,910 --> 00:06:52,440 So this is pretty simple. 81 82 00:06:52,440 --> 00:06:57,250 All we have to do is create our Konst and then we create our item's schema. 82 83 00:06:57,540 --> 00:07:03,630 And this is going to be equal to whatever field it is that we want into our schema and we're only going 83 84 00:07:03,630 --> 00:07:09,800 to have one field which is going to be the name of the item and the name is going to be of course a 84 85 00:07:09,810 --> 00:07:12,030 string data type. 85 86 00:07:12,030 --> 00:07:14,270 Now we have our item schema. 86 87 00:07:14,280 --> 00:07:22,760 The next thing we need to create is a new mongoose model based on this schema again as a quick reminder 87 88 00:07:22,850 --> 00:07:24,940 this is what mongoose models look like. 88 89 00:07:24,980 --> 00:07:32,890 We create a new constant and we use the mongoose dart model initialiser to create that new model. 89 90 00:07:33,020 --> 00:07:40,350 And in order to create it we have to specify two things the singular name of our collection. 90 91 00:07:40,370 --> 00:07:46,820 So if we had a collection called people then the singular form would be person and that has to be in 91 92 00:07:46,820 --> 00:07:48,440 the form of a string. 92 93 00:07:48,710 --> 00:07:55,260 And then we have our schema which we provide pose a video and try to complete that challenge. 93 94 00:07:57,480 --> 00:07:57,770 All right. 94 95 00:07:57,770 --> 00:08:03,440 So we've already created our schema and we now have to create our model based on the schema. 95 96 00:08:03,660 --> 00:08:10,030 Let's create a new constant and we'll call our model item with a capital. 96 97 00:08:10,080 --> 00:08:14,930 So remember whenever you have a mongoose model it's usually capitalized. 97 98 00:08:15,180 --> 00:08:21,480 And this is going to be equal to a mongoose dot model and we're going to create the model passing in 98 99 00:08:21,600 --> 00:08:22,860 two arguments. 99 100 00:08:22,950 --> 00:08:27,600 The first thing is the singular version of our collection name. 100 101 00:08:27,690 --> 00:08:33,690 So we're going to have a collection called items and that means that the singular version is going to 101 102 00:08:33,690 --> 00:08:41,490 be item and then the second argument is going to be the schema that we're going to use to create this 102 103 00:08:41,550 --> 00:08:49,030 item's collection which is of course our items schema that we created a little bit earlier on. 103 104 00:08:49,310 --> 00:08:53,800 So now we have a schema and we have a model created using that schema. 104 105 00:08:53,840 --> 00:08:59,620 The next thing I want to do is I'm going to delete everything that's related to this day and get date. 105 106 00:08:59,630 --> 00:09:06,080 I'm just going to simplify our code a little bit instead of having to use our data module and instead 106 107 00:09:06,080 --> 00:09:14,630 of having this data file We're going to just simply render the default list with a title that's called 107 108 00:09:14,690 --> 00:09:15,890 today. 108 109 00:09:16,490 --> 00:09:23,360 And this way we'll cut down the complexity of working with the date and the external modules and also 109 110 00:09:23,360 --> 00:09:28,060 trying to understand how Mongar D-B is going to be plugged in to our app. 110 111 00:09:28,070 --> 00:09:33,770 So this just makes it a little bit easier to understand now that we've replaced the list title with 111 112 00:09:33,830 --> 00:09:35,570 a simple string. 112 113 00:09:35,570 --> 00:09:38,390 The second thing we're passing over to our list. 113 114 00:09:38,420 --> 00:09:45,460 E.J. this is our items array which if you remember we delete it so it no longer exists. 114 115 00:09:45,590 --> 00:09:53,950 And instead we want to pass over the items that are inside our items collection to do that we need some 115 116 00:09:53,950 --> 00:09:55,640 default items right. 116 117 00:09:55,660 --> 00:10:03,490 So let's go ahead and create three new documents using our brand new item mongoose model just around 117 118 00:10:03,490 --> 00:10:03,920 him. 118 119 00:10:03,970 --> 00:10:10,360 And I'm again going to leave that up to you as a challenge just as a reminder the way that we create 119 120 00:10:10,360 --> 00:10:13,970 new documents using Mongoose is something like this. 120 121 00:10:13,990 --> 00:10:19,960 So we would create a new constant then we would bind it to a new model name. 121 122 00:10:19,960 --> 00:10:28,960 So in this case that would be item and then we specify the values for each of the fields in our schema. 122 123 00:10:29,020 --> 00:10:30,150 We've done this before. 123 124 00:10:30,190 --> 00:10:36,790 And so I want you to pause the video and try to create three new documents which will be the default 124 125 00:10:36,820 --> 00:10:40,830 items in our To-Do list and they can say whatever it is you want them to say 125 126 00:10:45,020 --> 00:10:46,250 following that format. 126 127 00:10:46,280 --> 00:10:49,260 We're going to create our item one. 127 128 00:10:49,580 --> 00:10:52,040 And this is going to be a new item. 128 129 00:10:52,040 --> 00:11:00,500 So we're saying that we're creating a new document from our item model and then we pass in our fields. 129 130 00:11:00,530 --> 00:11:00,850 Right. 130 131 00:11:00,890 --> 00:11:08,170 So the field that is called name is going to say welcome to your to do list. 131 132 00:11:09,500 --> 00:11:19,400 And that's one document out of the way now I'm just going to go ahead and create two more. 132 133 00:11:19,580 --> 00:11:27,910 So now I've created three documents item one item to an item 3 and they have some arbitrary names. 133 134 00:11:27,920 --> 00:11:31,560 So the first item is just going to say welcome to your to do list. 134 135 00:11:31,580 --> 00:11:36,910 The second one is going to say hit the plus button to add a new item. 135 136 00:11:37,100 --> 00:11:43,220 And the third one's just going to say hit this check box or hit this thing over here to delete an item. 136 137 00:11:43,220 --> 00:11:47,710 Now that we've got our three documents let's go ahead and put them all into an array. 137 138 00:11:47,960 --> 00:11:52,670 And I'm just going to call this the default items. 138 139 00:11:52,670 --> 00:12:01,220 It's going to be a new array and it's just going to contain item one item 2 and item 3. 139 140 00:12:01,640 --> 00:12:10,310 Now that we have our array then we could use one of the methods in mongoose called insert many so that 140 141 00:12:10,310 --> 00:12:19,790 we get to insert all of these items in one go into our items collection again just as a quick reminder 141 142 00:12:19,790 --> 00:12:22,780 of how the syntax for insert many looks like. 142 143 00:12:22,850 --> 00:12:25,820 Then you can see it looks something kind of like this. 143 144 00:12:25,850 --> 00:12:32,840 We provide the model name which refers to the collection that we want to insert data into and then we 144 145 00:12:32,840 --> 00:12:37,000 provide the actual array of documents that we want to insert. 145 146 00:12:37,130 --> 00:12:41,390 And finally get a callback which may or may not contain an error. 146 147 00:12:41,420 --> 00:12:44,990 Depending on how the process went. 147 148 00:12:45,110 --> 00:12:51,110 So while I'm showing you a lot of syntax using slides you can also of course just simply read the docs 148 149 00:12:51,140 --> 00:12:52,650 inside mongers right. 149 150 00:12:52,730 --> 00:13:00,650 So if you go to the docs and you go over to the API and find the model API docs then you can find these 150 151 00:13:00,650 --> 00:13:08,750 things such as insert money and see the full explanation and example as well so don't feel like you 151 152 00:13:08,750 --> 00:13:12,740 have to rely on just the hints I've given him. 152 153 00:13:12,740 --> 00:13:18,740 Coding is essentially an open book exam you can refer to as many sources as you like in order to get 153 154 00:13:18,740 --> 00:13:19,790 to the answer. 154 155 00:13:20,690 --> 00:13:28,550 Pose the video and try to insert all of our items into our items collection. 155 156 00:13:28,550 --> 00:13:28,920 All right. 156 157 00:13:28,910 --> 00:13:37,160 So in order to insert it we're going to specify that we want to insert items into the items collection 157 158 00:13:37,280 --> 00:13:44,540 so we'll refer to the item model and then on that model we're going to call insert money and we're going 158 159 00:13:44,540 --> 00:13:46,600 to provide two arguments. 159 160 00:13:46,610 --> 00:13:48,830 One is what do we want to insert. 160 161 00:13:48,830 --> 00:13:53,310 Well we want to insert our array of default items. 161 162 00:13:53,930 --> 00:13:59,980 And the second thing is we have to provide a callback just in case there's any errors. 162 163 00:14:00,140 --> 00:14:11,300 So we could say something like if there's an error then log the error else simply alog success 163 164 00:14:13,900 --> 00:14:18,670 fully saved difficult items to database 164 165 00:14:22,360 --> 00:14:24,470 just before we run our abduct G-S. 165 166 00:14:24,610 --> 00:14:31,180 Just make sure that you check your code that there's no errors anywhere that you've got everything all 166 167 00:14:31,180 --> 00:14:34,750 set up with your model your schema and your documents. 167 168 00:14:34,810 --> 00:14:36,140 Over here I've just spotted it. 168 169 00:14:36,160 --> 00:14:39,350 I've actually got a bit of a typo instead of use. 169 170 00:14:39,370 --> 00:14:44,480 It should be use new Jarrell parser now that I'm happy with my code. 170 171 00:14:44,560 --> 00:14:52,640 Then it's time to head over to my console and make sure that you've got a man-God process running. 171 172 00:14:52,780 --> 00:14:56,650 So you should have this set up from the last module. 172 173 00:14:56,860 --> 00:15:02,140 And we should also have a mongo shell that's enabled on another tab as well. 173 174 00:15:02,170 --> 00:15:07,130 So now make sure that you've ceded over to where your app doc is. 174 175 00:15:07,240 --> 00:15:09,630 And we're finally ready to run our opt out. 175 176 00:15:09,660 --> 00:15:16,690 JS So now if I hit enter you can see that we get the service started but also we get the message that 176 177 00:15:16,690 --> 00:15:21,290 says successfully saved our default items to database. 177 178 00:15:22,300 --> 00:15:29,620 If we switch over to our Mungo's shell and we say show D-B use then we now have this extra database 178 179 00:15:29,620 --> 00:15:31,330 code to do list B. 179 180 00:15:31,520 --> 00:15:40,120 And if we go ahead and say use to do list D-B And now we show collections you can see we've now created 180 181 00:15:40,120 --> 00:15:47,200 this collections called items and this is all done when we created our mongoose model and specify the 181 182 00:15:47,200 --> 00:15:49,770 singular name of our collection. 182 183 00:15:50,200 --> 00:15:58,610 So now if we say the dot items dot find basically find all. 183 184 00:15:58,610 --> 00:16:06,190 Now we've got three documents saved in them which correspond to our default list that we want to show 184 185 00:16:06,190 --> 00:16:09,080 when we create a new to do list right. 185 186 00:16:09,730 --> 00:16:12,520 So that was pretty successful. 186 187 00:16:12,520 --> 00:16:18,730 Now if you have any problems with starting up your Mango's shell or with your Man-God server then just 187 188 00:16:18,730 --> 00:16:25,300 have a look inside the course resources for this lesson where you can see some ways of debugging what 188 189 00:16:25,300 --> 00:16:27,840 the issue is and fixing it if needed. 189 190 00:16:28,210 --> 00:16:30,710 But hopefully you're on the same page as me. 190 191 00:16:30,760 --> 00:16:36,550 And you've got your server started and you've successfully saved your iTunes. 191 192 00:16:36,640 --> 00:16:41,770 The next step is to read from our database inside our apt. 192 193 00:16:41,790 --> 00:16:45,580 Just so for all of that and we'll see you on the next lesson.