Hated everyone had their share.
And in this video, we're going to talk a little bit briefly about the database connections and what
all the things you have to worry about.
Now, if you go back on to the presentation, remember once we were discussing about all of this a little
while ago, I told you that database is always on different continent.
As long as you remember this, everything will be all good.
But you saw that it is so easy to actually forget all of this.
That's why I have created a dedicated video on this one.
What do I mean by database is on a different continent.
It means it is going to take some time while fulfilling the operation.
And can you see on the line 20 we saw that existing user.
We are grabbing this and we are able to just say user find one.
Now although this operation is going to be really faster, but there is no guarantee that this is going
to happen instantaneously.
Your database can be on another continent and it can take some time to work with that.
So such kind of requests should be actually followed by a wait.
Now, as soon as you are going to use Evade, that means it's not a synchronous operation, it's an
asynchronous operation.
But as soon as you save this, this will create a problem in the crashing of your app that, Hey, why
are you using a wait?
Where is the async functionality?
So this entire method should be a sync type.
So we're going to go ahead and we're going to use a sync.
There we go.
Now, is this okay?
Yes, for as of now, this is okay.
This is not going to bother you any further down the road.
But you can also see that this actually returns a promise.
So there is a promise issue here.
So whenever these kinds of request actually is being sent onto the database itself, a promise is being
received back.
Now, promise can go success and can go failure as well.
And in case you remember, we actually saw this in the documentation, but I kind of glimpse through
over it.
So if you look up here, so a little bit on to the query page, notice it says these execution.
So if you want to execute this query right now, you just want to use this one.
But if we'll dig up a little bit more onto these documentation, they're going to find that always and
always.
This actually fires up a promise.
Now you can actually configure the promise you like, whether you like any special type of promises
or you want to use the default one.
That's up to you.
Not going too much in detail of that.
But remember, a promise is written here because this should be promised or you have to wrap it up into
a tri catch block.
So we are definitely missing some of the optimization that we can do.
That is all.
Okay.
We will discuss that and figure it out as we move on into the next sections.
But another thing is right now we are not connected to the database, so how are we going to do that?
The connecting of the database, first thing we are going to do is we are going to configure our DMV
because this is where the the URL of the database should be written up.
So we're going to go ahead and say Mongo DB, underscore URL, call it UI or you are totally up to you.
Now let me bring up my MongoDB compass.
So we'll be using this one in case you are using Atlas, the online version of DB, then go ahead and
grab that URL in case which I personally recommend is install MongoDB locally, then you can just go
ahead and hit the connect and it will automatically connect for you.
The reason it was able to connect you so easily because in the settings all the settings are already
there.
And here you can see these are the only database we are going to call this one as AE ought to make it
a unique database.
So how we actually go ahead and define that.
So the URL for the local host database is mongo colon slash slash, just like we have HTTPS or HTTP.
And then you can go ahead and pass on either localhost or you can just simply say 1 to 7 .0.0.1, that's
again, a localhost URL.
And then we're going to call this one as at AE.
So this is all you have to do now at the time of deploying, we can go ahead and replace this with the
Atlas or whatever the URL is there, wherever your database is hosted.
That's it.
Okay.
Moving on now let's go into the config and database JS.
And you guessed it right, this is exactly where we'll write the connection of the database string.
Can we write it just like that?
No, of course we cannot.
We need to go in the documentation section and see how it is being done.
So let's go into the Quickstart and we can see that all this is going on.
But let's go ahead and see how we can connect.
So connections are there and there we go.
So it says Mongoose, Dot Connect, and you can just use the connection just like that, or you can
go ahead and work with that.
So this is pretty easy.
They say this is just a one liner Mongoose dot connect.
Technically, yes, this is one liner and they give you example of MongoDB call local localhost.
And again, I forgot that we need to put up this port as well.
I almost forgot that.
Okay, let's go back into the DMV and we have to say colon 27017.
This is a default port of MongoDB so don't scare get get scared off on that OC this is okay, we can
definitely use that or the link from the online version that is okay.
But let me tell you a little bit more on that, that how this should actually be done.
Let's go back and moving into the database is the first thing obviously we need.
Mongoose.
So let's go ahead and say, hey, mongoose, that is going to be required.
And we're going to say Mongoose.
There we go.
Once Mongoose is there, then we also need this variable that we have just declared in the environment
variable.
So let's also structure that.
So that will be coming up from process dot, E and V and what did we call our variable MongoDB URL?
Copy that and paste it up here.
Now the documentation says that you go ahead and simply say Mongoose, dot connect and there is no problem
in that.
This is exactly how we should be doing it.
So we're going to save MongoDB URL.
But in addition, if you're going to save this, you are going to notice that it asks you to provide
a couple of options, the connection options.
Now, these connection options are pretty common, known everywhere on every StackOverflow and every
one.
The first one is actually use new URL parser.
So previously MongoDB was using some kind of different parser.
They updated it and now they want you to pass on the flag because they are legacy compatible.
So that is why we need to use the modern parser that we are working on now.
Also similar one more feature got update or one more settings got update which is use unified.
Topology.
Now there are a couple of more settings that you can go with that probably like use the new index and
all of that.
We don't definitely need this in this one, but we are going to figure them out later on.
Now, again, sometimes it's a little bit easy to get a typo in that, so make sure you keep an eye
on that.
Hopefully I didn't made that.
Now, as I told you, everything that you do with the Mongo or mongoose, they are always a promise.
So promise.
What does it return?
It returns a dot, then.
There we go.
Just like that.
And it also returns a dot catch because there can be a problem here.
So what happens when we get a dot?
Then let's go ahead and separate them on the separate line so that it's easier for us.
So in the case of Dot catch, then obviously we are going to receive an error.
So let's go ahead and handle that part just like this.
And we're going to simply go ahead and first log this out and we are going to say that DB connection
failed.
There we go.
Also, we need to log these errors, so let's go ahead and log them properly.
So let's go ahead and dump them.
All of that.
Not a good strategy in the production.
We want to have a separate error between the production as well as in the development environment.
We can talk about that later on.
And finally, the most important is process dot exit.
Now again, the documentation doesn't give you in detail the depth of it because it is known.
It should be known that, hey, there's going to be a promise and you need to handle the promises accordingly.
We will take care of the things in the next project that how properly we can have these things and notice.
On top of that there needs to be a sync one here.
It is not required, but in all of the other cases it needs to be an async request with the promises
or the TRICARE wrapper.
Now, if everything goes right, then we are going to go ahead and simply, simply log a message that,
hey, database connection is all good.
So we're going to simply throw up that DB connected.
Successfully.
There we go.
And we don't need this.
Save this.
So this is all done.
But again, this is not all we have to do because we won't be using this into the DVD as we would be
rather throwing up into a separate folder.
So notice we are not holding this entire thing into available.
Now we need to do that.
So what we can do, easiest thing is let's just cut this out, all of this and we're going to go ahead
and directly write exports dot connect because there will be not too much of the variables into this
file.
We can directly say that, hey, this is a method connect that I'm throwing this out, this is how this
method looks like and we can go ahead and paste everything inside that.
So there we go.
Now anybody can import this connect and can start connecting with the database.
So let's go ahead and use it into app dogs now in the app.
JS One important thing, we don't need to hold this connect this connect into a variable.
We can just run it directly as soon as it comes in.
So let's go ahead and do that.
So once Express is required, all of that, we can actually do it at the top.
We're going to go ahead and say Hey required and make sure your dot inv is required as first because
it's going to bring in MongoDB URL in case it needs it.
So we're going to go ahead and save from the current directory.
Let's go into config and inside that we are going to have a database JS and it has a method dot connect
and just go ahead and run this.
Now again, there can be many other ways you can hold that into a variable and then say that, hey,
I am calling this XYZ and run this totally up to you how you want to do that.
Now notice here it says, hey, crashed because probably there is something wrong in the URL.
So it says invalid string which is mongo and notice it's not mongo.
It is also mentioned up there it is MongoDB.
So again referring to my env files again and this is MongoDB.
Save that and we need to restart because of an E and v files are not being monitored and notice now
it says DB connected successfully.
So again with a little bit of the error, now we are working on that.
Okay.
So now our database connection is done.
But still there are a couple of things you should always keep in mind that this always returns a promise.
In fact, whenever you make a query, this is going to return.
You promise and you need to handle that promise.
Here we are writing absolute the basic one because we need the US thing functionality as well.
So there are a couple of things we can definitely talk about, but that's all moving back onto the presentation
now that what else we have to do.
So we have talked to the database, we have already worked on, already registered, but how we're going
to take care of the password, that is the big question.
And the next question is token generation.
So in the next video, let's talk about what are the points where we can talk, take care of the password
and how we can actually take care of it.
Let's go ahead and catch up in the next video.