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