Our Express API is now aware of some information that's going to be sent from.
In theory our phone even though right now we're just testing the API from postmen.
So now that our Express API has that email and password that user is trying to sign up with the express
API.
Now it needs to reach out to Mongo DB and see if anyone has ever signed up with an email of test at
test dot com before.
So in this video we're going to get a better idea of how our Express API is going to work with Mongo
DB.
Okay.
So quick diagram.
So inside of Mongo DB We're going to eventually store some information related to all the users who
create an account with our app.
We're going to store this information inside of something called the user's collection.
You can essentially imagine users collection as being like a array with a ton of objects inside of it
where each object represents one user was created an account.
Now behind the scenes there's not actually an array of objects.
A very different storage mechanism is used in Mongo DB.
Nonetheless we can kind of imagine that is what is happening.
So again inside of our users collection we're gonna have a bunch of different objects that all have
an email and password and those two properties are both going to be strings each of those objects represents
one single user account so that's what's going on inside of Mongo.
That's where our actual information lives.
But in order to work with that data we're making use the library called mongoose.
When we make use of Mongoose we have to instruct it about the different data or the different collections
that we've stored inside of Mongo DB.
So we have to do a little bit of configuration with mongoose before we ever work with Mongo.
Specifically we need to tell Mongoose that there's something called a user's collection and we're going
to do that by creating something called a user's model.
This user's model is going to essentially represent kind of a tie to that collection of data inside
of Mongo DB.
So when we create this user model we're going to tell Mongoose that every user that is stored inside
of our collection up here is going to have an email that's a string and a password that's a string as
well.
Once we create this user model we can then use it to manipulate all the different users we've got stored
inside of Mongo DB So let's flip back over to our code editor.
We're going to write out a little bit of code to tell Mongoose that we want to have this user collection
thing or this mechanism of storing user's so going to flip back over to my editor inside my source directory.
I'll make a new folder called models then inside there I'll make a new file called user dot J.S. then
inside this file where we will first begin by requiring in the Mongoose library.
So I'll say const mongoose is require Mongoose
or then use the Mongoose object to create the idea of a user model.
So going to write out const user schema is new Mongoose start schema and take notice of the capitalization
I have inside of here so lowercase im on Mongoose capital S schema the schema object is where we tell
Mongoose about the different properties that every user inside the user's collection is going to have.
So for you and I like we were just discussing a user is always going to have an email.
Well then assign that property a object to configure some information about what this email property
is supposed to be like.
So again right now we are attempting to tell Mongoose about what different properties every user inside
of our users collection is going to have.
So inside this object we're going to specify the type of data that email is going to reference.
So email is always going to be of type String will then pass on in other configuration flag of unique
is true by adding in this flag mongoose is going to understand that we want every email that is tied
to a user inside of our users collection to always be unique.
And if someone ever tries to save two users with duplicate emails we're gonna get an error message back
and then finally we'll also add in one last flag here of required is true.
So that just means that any time that we tried to save a new user to our users collection inside of
Mongo D.B. if the user does not have an email then they are invalid and we should not be allowed to
save them.
So every user we create has to have an email property now we can go through the same process with a
password property as well.
So I to say that the password must be of type String.
It doesn't have to be unique so to users can have identical password or so identical passwords and then
required is true.
So we want to make sure that every user is created with a password.
So now that we've created this object that essentially defines what a user is we're going to associate
it with mongoose.
It's down to about in the file.
I'll say Mongoose dot model will pass in a string of user and then pass in the user schema.
So again these schema right here tells Mongoose about the kind of structure or data that every user
is going to have.
And this call right here actually associates that with our Mongoose library OK.
So we can now save this file and then going to go back into my index dot J.S. file and at the very top
of this file I'm going to requiring that user file we've just created.
So going to say require we're going gonna go into the models directory and then require in user.
You'll notice that we did not actually assign this required to anything.
We also did not really export anything from user dot J us as well.
The reason for that is that Mongoose really expects to only see that line of code right there executed
exactly one time if we import require this file user dot J.S. into multiple other files inside of our
project every time we require it.
That line right there is going to be executed a second or third or fourth time and you're going to very
quickly see an error message where Mongoose says something like hey you already defined a model called
user you can't define it again.
So to get around that we only require in user dot J.S. into index dot J.S. Right here if we ever then
need to get a reference to our user collection or user model that we just created which we definitely
need inside of auth routes.
Let's go over to all throughout.
Right now I'll show you how to do that.
So inside of here if we need to get access to that user model tied to Mongoose and somehow manipulate
our collection of users we can then import or require in Mongoose at the top
and then to get access to the user model will say const user with the capital you is Mongoose dot model
user like so it's now anywhere inside this file we can get access to that user model which allows us
to access the underlying collection of data that's stored inside of Mongo DB Okay.
So that's pretty much it.
So we've now got our user setup so we can now use this object right here to somehow manipulate that
collection of data so we can use it to see if a given email is already in use we can also use it to
create a new instance of a user as well.
So let's do both those things in the next video.
So a quick pause and I'll see you in just a minute.