In this video to kick off the section we're going to explore a new tool that will be making heavy use
of as we create the task manager application.
This is the very popular NPM module called Mongoose now mongoose is directly related to mongo D.B. and
it's going to allow us to do some of the things that we don't know how to do so far.
For example we know how to use Mongo D.B. to create documents read a documents update and delete them.
But what about other equally important things.
For example how do I set up validation for my documents defining which fields are required and which
are optional and what types of data I expect for them.
For example I don't want someone to be able to set that completed field equal to a string.
I want to require that it's a boolean.
Now with Mongoose we can also do other things we're not able to do.
For example how do we say that a given task was created by a user.
Eventually we're going to add authentication to the task manager application.
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
data and see the private task I created mongoose is going to allow us to get all of those new things
done while still being able to perform our basic CRUD operations on the Mongo D.B. database.
Now before we integrate anything into our project let's view mongoose in the browser.
You can find it over at Mongoose J S dot com.
And the reason I'm bringing you here is to just show you where you can learn more.
They have a pretty decent set of documentation and for the moment what I want to do is just quickly
go over this five line example down below.
Now it's OK if this code is no longer here.
Maybe they updated the site and got rid of this example.
You can just watch as long as I quickly give you an overview of what mongoose is going to let us do.
So Mongoose right here is just connecting to the database.
And the next thing it does is it creates what's known as a model a model allows us to model something
in the real world that we want to be able to store in the database.
So this could be a user or a task or in this case a cat.
If we were creating an IKEA web site using Mongoose we could model the various pieces of furniture that
we're selling including their names their prices dimensions and any other actor buttes we might have.
So we create models for all of the collections that we want and then we use the model to describe the
data.
Now in this case for this basic example there's almost nothing to the model.
There's no validation and there's nothing fancy which we'll explore shortly.
All they're doing is saying that a cat has a name and the name should be a string.
We'll learn how to customize the models further as we progress through this section.
Now down below what do they do.
They create a new instance of a cat right here.
They have the kitty variable which stores that new instance and they set the name equal to the following.
Then down below they use various methods on that model to manipulate it.
So right here they use Kitty dot save to save the data.
And this provides a much nicer interface for interacting with Mongo D.B. instead of needing to use all
of the low level API methods which we explored.
We'll be able to use methods that make a bit more sense when we're writing code like save now among
goose falls into a broader category of tools no one as O.D. Ms.
And OTM stands for object document mapper.
And as that name suggests the goal is to allow you to map your objects in your code.
So for us objects in our no JSA application over two documents inside of the Mongo D.B. database.
And that's exactly what mongoose is going to let us do.
So let's go ahead and get started with Mongoose by heading over to visual studio code installing it
setting it up and working through a basic example.
I'm going to navigate over to V.S. C. And we're going to run the NPM install command from the terminal.
Once again make sure you have navigated to the task manager directory that's going to be NPM AI Mongoose
at the latest version five point three point sixteen.
Now we're gonna go ahead and run the installation command and while that's running we can start the
process of setting up the real director restructure.
We're going to end up using to explore Mongo D.B. at a basic level.
We did all of that in just a single file.
The task manager app though is gonna get much bigger than any of the applications we've built so far.
So we want to make sure that we do take into account a directory and file structure that allows us to
scale the application out to its complex large size without having everything in a single file.
So right here we're gonna start by creating a new folder that is going to be the source directory.
And this is where the application source code will live and for the moment we're going to create one
more directory and one file inside of the source directory.
We're going to create a D.B. folder which is where we'll store things related to the database and we're
going to add just a single file inside of there and we'll call that Mongoose dot J S inside of here
we're going to mess around with the basics of Mongoose and we'll continue to use Mongoose as we build
out the task manager API to start.
Let's go ahead and load mongoose in so I'm going to create a mongoose constant.
Then I'll go ahead and use require it to load in the Mongoose NPM library.
And once we have that require call in place the next thing we do is use Mongoose dot connect to connect
to the database now Mongoose dot connect is pretty similar to mongo client not connect.
We are going to provide the connection you are well as the first argument and we are also going to provide
that options object as the second argument.
So remember Mongoose uses the Mongo D.B. module behind the scenes so everything we do in mongoose is
playing off the abilities of this library.
So everything we learned about in the last video is completely relevant to Mongoose right here.
We'll provide our U.R.L. and we'll provide our options object.
Now for the U.R.L. it's gonna be just slightly different.
Everything starts off the exact same way.
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
for the port 2 7 0 1 7.
Now this is the exact U.R.L. we have over here.
The one change we're going to make is that we don't specify the database name separately.
Instead we provide to that right in here as part of the connection string for us that's gonna be forward
slash followed by the database name.
In this case I can use task manager API.
Now notice I did pick a different name than the database we already have in place as the Mongoose data
is going to look different.
So right here we have that connection string in place and now we can move on to the options and we are
going to configure use new you are all parser exactly like we had before.
And once again we will be setting that to the Boolean true.
Now there is one more option we're going to configure.
Also setting to true that is use create index.
This is going to make sure that when Mongoose works with Mongo D.B. our indexes are created allowing
us to quickly access the data we need to access.
So right here this is all that it takes to actually connect to the database.
Now we can focus on the fun stuff which is to define our very first model together we'll define a basic
version of the user model.
Then as your challenge for the video you'll be defining a basic version of the task model.
Now I'm saying basic version because as we progress through the next couple of sections we'll continue
to improve and build out these models.
Eventually they'll all live in their own files and there'll be a lot of code associated with them to
do all sorts of interesting things.
For now though we're going to start with just the bare bones minimum setup.
The first thing we need to do is define the model we're working with and as mentioned we'll be creating
the user model together.
So right here we'll make a new constant call that user with an uppercase you then to actually generate
this.
We use Mongoose dot model and Mongoose dot model accepts two arguments.
The first is the string name for your model.
And the second is the definition where we define all of the fields we want.
Now for our purposes we will call this a user once again with an uppercase you.
And now we can determine what fields we want to have for now.
Let's go ahead and set up something like a name and an age for each user.
That's exactly what we had.
Any last section when working with just the Mongo D.B. NPM library to do that.
What we do is we set up all of those fields as properties on this object.
So I'm going to set up a name whose value will be an object.
And I'm also going to set up age whose value is also going to be an object.
And in here we can figure things about that field.
We can configure validation.
We can set up custom validation but at its most basic we can set up the type for the fields we're working
with.
And that's exactly what we're going to end up starting with.
So to get this done what we do is we set up a type property on each object.
So right here for a name we're going to set the type equal to a string right here uppercase string.
Then down below for age we'll be setting this equal to a number.
So the type will be uppercase N number.
So here I'm just using the constructor functions from JavaScript as the value for type.
Now Mongoose supports plenty of other types as well for things like boolean dates arrays binary data
object I.D. and more but these are two basic types that we'll be using to get started with now that
we have our model defined we can create instances of that model to actually add documents to the database.
So down below I wanted to make a new user what I would do is first create a variable to store that user.
I could just use something like me any name would work.
And right here we use the new operator with our user variable which is a constructor function for that
model.
So it's new uppercase U user.
We call this as a function which it is and we pass to an object with all of the data for this particular
user.
So for me I would set name equal to my name.
A string matching up with the type for name up above.
And then I would also set up age right here setting that equal to a number.
Perfect.
Now we have an instance of our model created me.
But at this point nothing's actually getting saved to the database to save to the database.
What we do is we use methods on our instance.
So on here we have methods for doing all sorts of interesting things for those CRUD operations in order
to save the model instance.
What we do is we use the save method.
Now the save method doesn't take any arguments.
It's simply saving the data that we've stored and This returns a promise so we can use our two promise
methods which we've explored pretty extensively at this point to actually wait for the saving process
to finish right here and then to register a handler for when the promise is fulfilled and then down
below dot catch to register a handler for when the given promise is rejected in a minute will actually
trigger an error to make sure this catch handler does run when we have invalid data.
So I'll grab error and we'll make sure to dump that down below console log.
I'll print these string error to start then as the second argument I'll just dump the variable to the
terminal up above.
When things go well well we get access to is our model instance once again.
So that would be me from right here.
I can choose to name it but since it's the same thing I could always just access the existing me which
I already have right here.
Console dialogue and I'll just dump me to the terminal.
So what are we doing.
We're defining a model we're creating an instance of it and we are saving that instance to the database.
Let's go ahead and test our work.
I'm gonna save Mongoose dot J S and run it from the terminal.
Make sure you still have your Mongo DB database running in that other terminal tab.
Right here I'll kick things off by using clear to clear the terminal output.
Then I'll use node source forward slash D.B. forward slash Mongoose dot J S I'm going to run the file
it's going to go through the process of saving that user.
And what do I get back I get back our document.
I have my I.D. I have my name and I have my age all showing up.
Now we also have one additional property added and managed by mongoose.
This is underscore underscore V.
This store is the version of the document and right here we are starting at 0.
Next up we're gonna head over to robo 3 TI and view our data never member.
We created a brand new database when we're working with mongoose.
If I right click the connection and click refresh I can see that show up just down below.
I'm going to close the tabs for the other database.
I'll then right click it and we can actually drop that database removing it completely.
The only one we're going to need is the task manager api database.
If I crack that open we have our collections.
We have a user's collection and if I open that up what do we have.
We have the one document which we just explored right here.
I have the exact same fields and values which I saw over inside of the terminal.
Now that we've seen things go well let's watch things go poorly to show this off.
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
a number and we'll rerun the file right here.
I'm going to set up a string and I'll just put something in like the name Mike.
Perfect.
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
long object printing the long and the short of it though is that this is a validation object letting
us know that things went wrong and describing what exactly went wrong and we're going to explore validation
more as we progressed through the sections.
For now I just wanted to show you that when we do use Mongoose we do get a base level of validation
from the start and we can customize that as we progressed.
For example I could say that you need to be an adult so your age needs to be greater than or equal to
18.
Or we could validate your e-mail making sure it has all of the correct components.
So that's actually where we're going to stop for this video in robo 3 T.
If I do refresh the data we'll see we still just have our one document.
The other document we tried to create was invalid.
So Mongoose did not let us create it.
Now I said there was gonna be a challenge where you'll create the task model.
This video ran a little longer than I wanted it to.
So I'm going to push that back to the next video.
I'm very excited to get to it.
So let's go ahead and jump right in.