At the end of last video I mentioned that getting TypeScript and Mongoose to work together is sometimes
just a little bit awkward.
So in this video I want to outline two big issues that we're going to run into just so you are aware
of why we're going to write out some slightly complicated code in just a moment.
So here's the big issues that are going to start to run into.
First I want to give you a quick review on some terminology around Mongoose and Mongo DV inside of Mongo
DV.
We organize information into different collections.
We might have a user collection that will store all the different users who have signed up for our application.
So in this case users on through for each of these can be thought of as little objects that store some
information about a very particular user to access all this data.
We're going to use Mongoose and in particular we are going to create two different things inside a mongoose.
We are first going to create something called a mongoose user model.
This is a class that is going to represent the entire collection of users.
So we will use this user model class to run queries to create new users or to do anything that modifies
or queries the overall collection.
We are also gonna have something called a mongoose user document a user document is also a class.
It is meant to represent one singular user so we might create an instance of a user document to represent
user number 2 or create a second instance or another instance to represent just user number for.
So again we've got a user model that represents all of our data and user document that represents just
one little thing.
So that quick refresher on terminology.
Here are the two issues that are going to start to run into when we are getting Mongoose and typescript
to interact with each other so issue number one is going to come up when we start to create a new user
document.
So this is going to be how we eventually try to create a new user document or a new instance of a user
will say new user and then pass in some different properties such as email and password and whenever
we tried to pass in properties to anything typescript wants to understand the properties we are passing
in and somehow make sure that you're passing in the correct type of data the typescript wants to understand
the different arguments that calling new user is going to accept.
However Mongoose does not make this easy behind the scenes or more specifically with the Mongoose type
definition file that we just installed a little bit ago.
Whenever we try to create an instance of a user as you're doing right here it is not going to kind of
tell typescript about the different properties that we are trying to pass in.
And so for example we could make a huge typo inside of here we could accidentally write in hard instead
of password and typescript is not going to complain because typescript is not going to be given enough
information by Mongoose by default to understand the different types of arguments or the names of these
properties we are supposed to pass in but this is issue number one that we are going to Ryan too.
We have a workaround for this.
We're going to essentially somehow teach typescript about the different properties that this constructor
expects.
So we are going to solve this.
I just want you to be aware that this is something we need to be aware of.
OK.
Issue number two.
So with issue number two at some point time we are going to create a new user.
And we might assign it some properties like say email and password that's been going to give us back
a user document.
So that's let's imagine right here a user variable or something like that at some point time later.
We're going to want to access some of the data within this user.
Let's imagine for a second that we add in a console log of user if we did that we might end up seeing
that this user document has more properties than what we provided to the constructor.
So for example we might take that email and password and assign them as properties to user.
But behind the scenes Mongoose might decide to add in a couple of additional properties as well.
Properties like let's say a created at timestamp or in updated that timestamp so the properties that
we pass into the constructor don't necessarily match up with all the properties that are created on
the user documents.
This is also an issue with typescript because typescript wants to know about all the properties that
exist on the user.
So we need to somehow tell typescript hey look we're going to pass some properties into the constructor
typescript but don't worry there are going to be some other properties that get created behind the scenes
and assigned to the user as well.
So we really need to teach typescript that there are kind of two different sets of properties.
There's one set of properties that will be passed to the constructor and there's gonna be a second set
of properties that we should be able to access on the user document that gets created.
That is issues number one and number two.
And again this is some stuff that we're going to have to fight with around Mongoose and typescript.
Now of course if we were just writing javascript yeah we would not really have to worry about this but
there are some very good reason.
So you're using typescript on this micro services project.
We haven't really dove into them just yet but trust me you're going to start to see why we're using
typescript very quickly as soon as we start to create our second service.
All right.
Now we understand these two big challenges.
Another quick pause.
We're gonna start to write out some code that's going to solve both these challenges.
We're going to create our user model and all that good stuff.
So I'll see you in just a minute.