Enough lecturing and talking.
Let's go increase our actual user model.
So back inside my code editor I'm going to find my off service directory.
It is right here inside the NRC folder.
I'm going to create another directory called models.
And inside there I'll create a file called user dot.
Yes.
So this is where we are going to define our Mongoose user model.
We're also going to write out some code inside of here to solve those two issues that we just discussed
to get started at the very top.
I'm going to import Mongoose from Mongoose after that.
I'm going to define something called a schema a schema is how we are going to tell Mongoose specifically
about all the different properties that a user is going to have.
I'm gonna write out to user schema is new Mongoose dot schema and then sort of here again we're going
to list out the different properties that a user is going to have.
First off they're going to have an email I'm going to markets type as being a string and I'm going to
say that this property is required required.
True that quick little note in those we put a type right here and we said that its type was going to
be string.
This is not in any way shape or form.
Not whatsoever tied to typescript the type that we are listing right here is 100 percent specific to
mongoose.
It does not tell typescript anything whatsoever.
The other thing I want to mention is that when you put a type of string right here it is capital S string
because we are referring to the built in string constructor that is available inside the javascript
language whenever we write out a type of string using typescript.
So for example if we wrote out an interface you're really quickly.
And when it had some imaginary property that was of type String we'd write out lowercase string.
So in typescript whenever we want to indicate a type of string it's lowercase with Mongoose when we
indicate a type of a property it is a capital because we are referring to an actual constructor.
So after that the other property we're going to eventually store on all these different users is a password.
This will also be of type String and it will be also required
all right.
So that looks good.
So now after creating that schema we can see the schema into Mongoose and mongoose is going to create
a new model out of it.
And remember this whole model thing is how we actually access a whole big set of data inside of our
Mongo DB database so I can stay down here.
The bottom line user is Mongoose dot model I'm gonna put in a string of user and then feed in the user
schema like so then finally at the very bottom I'm going to export user
okay.
So this time well we wrote out all this code and everything appears to be working no without any issue
whatsoever I don't see any typescript errors or anything like that.
However if we start to really play around with this user model that we've now created you can really
start to see where these two issues are going to start to cause us some problems.
For example let's just try to create a new user so after creating the user model down here I'm going
to say new user and I'll pass on some properties I want to assign to this user.
Well naturally we probably want to have an email and a password so I could put in something like an
email of test at test dot com and a password of whatever well that looks fine.
We still don't see any errors from typescript or anything like that.
However here's where stuff starts to go wrong.
There is no information about the arguments that are going to be provided to this constructor being
fed into typescript right now.
So even though you and I know that we want to give that constructor an email and a password there are
both strings because that's what we told Mongoose typescript has no idea that that is the case.
So if I start to mangle these properties if I accidentally put in like just yes typescript says hey
who cares.
No issue whatsoever.
We don't get any warnings going to get any errors.
I can add in extra properties.
No problem.
I can even change this from a string to a number.
Also no issue whatsoever so you can start to see it right away that typescript has no idea about what
is going on with the arguments you're passing to this constructor the whole reason that we are using
typescript is so that it can check stuff like this and make sure that we are not making silly typos
or anything like that.
So it would be great if we could somehow teach typescript about the different properties that were going
to pass into this thing.
So it can do some validation or something like that for us.
All right so now that we understand the issue here let's take a quick pause and make the next video
and start to discuss how we're going to get TypeScript and Mongoose to actually work together.