WEBVTT

00:03.760 --> 00:05.230
Hated everyone had their share.

00:05.230 --> 00:09.430
And in this video, we're going to talk a little bit briefly about the database connections and what

00:09.430 --> 00:11.290
all the things you have to worry about.

00:11.380 --> 00:16.030
Now, if you go back on to the presentation, remember once we were discussing about all of this a little

00:16.030 --> 00:21.190
while ago, I told you that database is always on different continent.

00:21.190 --> 00:24.010
As long as you remember this, everything will be all good.

00:24.010 --> 00:27.340
But you saw that it is so easy to actually forget all of this.

00:27.340 --> 00:30.040
That's why I have created a dedicated video on this one.

00:30.040 --> 00:32.710
What do I mean by database is on a different continent.

00:32.710 --> 00:36.190
It means it is going to take some time while fulfilling the operation.

00:36.190 --> 00:39.250
And can you see on the line 20 we saw that existing user.

00:39.250 --> 00:42.790
We are grabbing this and we are able to just say user find one.

00:42.790 --> 00:47.800
Now although this operation is going to be really faster, but there is no guarantee that this is going

00:47.800 --> 00:49.300
to happen instantaneously.

00:49.300 --> 00:53.590
Your database can be on another continent and it can take some time to work with that.

00:53.590 --> 00:56.890
So such kind of requests should be actually followed by a wait.

00:56.920 --> 01:01.150
Now, as soon as you are going to use Evade, that means it's not a synchronous operation, it's an

01:01.150 --> 01:02.230
asynchronous operation.

01:02.230 --> 01:07.240
But as soon as you save this, this will create a problem in the crashing of your app that, Hey, why

01:07.240 --> 01:08.410
are you using a wait?

01:08.770 --> 01:10.240
Where is the async functionality?

01:10.240 --> 01:13.300
So this entire method should be a sync type.

01:13.300 --> 01:16.210
So we're going to go ahead and we're going to use a sync.

01:17.260 --> 01:18.130
There we go.

01:18.160 --> 01:19.390
Now, is this okay?

01:19.630 --> 01:21.550
Yes, for as of now, this is okay.

01:21.580 --> 01:24.340
This is not going to bother you any further down the road.

01:24.340 --> 01:28.000
But you can also see that this actually returns a promise.

01:28.000 --> 01:30.070
So there is a promise issue here.

01:30.070 --> 01:36.130
So whenever these kinds of request actually is being sent onto the database itself, a promise is being

01:36.130 --> 01:36.970
received back.

01:36.970 --> 01:40.810
Now, promise can go success and can go failure as well.

01:40.900 --> 01:45.550
And in case you remember, we actually saw this in the documentation, but I kind of glimpse through

01:45.550 --> 01:46.120
over it.

01:46.150 --> 01:52.360
So if you look up here, so a little bit on to the query page, notice it says these execution.

01:52.360 --> 01:55.960
So if you want to execute this query right now, you just want to use this one.

01:55.960 --> 02:00.700
But if we'll dig up a little bit more onto these documentation, they're going to find that always and

02:00.700 --> 02:01.480
always.

02:01.660 --> 02:03.400
This actually fires up a promise.

02:03.400 --> 02:08.740
Now you can actually configure the promise you like, whether you like any special type of promises

02:08.740 --> 02:09.880
or you want to use the default one.

02:09.880 --> 02:10.630
That's up to you.

02:10.660 --> 02:12.250
Not going too much in detail of that.

02:12.250 --> 02:18.430
But remember, a promise is written here because this should be promised or you have to wrap it up into

02:18.430 --> 02:19.660
a tri catch block.

02:19.660 --> 02:23.170
So we are definitely missing some of the optimization that we can do.

02:23.740 --> 02:24.220
That is all.

02:24.220 --> 02:24.550
Okay.

02:24.580 --> 02:28.240
We will discuss that and figure it out as we move on into the next sections.

02:28.600 --> 02:33.070
But another thing is right now we are not connected to the database, so how are we going to do that?

02:33.070 --> 02:38.620
The connecting of the database, first thing we are going to do is we are going to configure our DMV

02:38.620 --> 02:42.850
because this is where the the URL of the database should be written up.

02:42.850 --> 02:49.300
So we're going to go ahead and say Mongo DB, underscore URL, call it UI or you are totally up to you.

02:49.330 --> 02:52.300
Now let me bring up my MongoDB compass.

02:52.300 --> 02:57.940
So we'll be using this one in case you are using Atlas, the online version of DB, then go ahead and

02:57.940 --> 03:03.820
grab that URL in case which I personally recommend is install MongoDB locally, then you can just go

03:03.820 --> 03:06.460
ahead and hit the connect and it will automatically connect for you.

03:06.460 --> 03:10.810
The reason it was able to connect you so easily because in the settings all the settings are already

03:10.810 --> 03:11.290
there.

03:11.500 --> 03:16.690
And here you can see these are the only database we are going to call this one as AE ought to make it

03:16.690 --> 03:17.800
a unique database.

03:17.800 --> 03:20.140
So how we actually go ahead and define that.

03:20.140 --> 03:27.160
So the URL for the local host database is mongo colon slash slash, just like we have HTTPS or HTTP.

03:27.190 --> 03:34.090
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

03:34.090 --> 03:35.500
again, a localhost URL.

03:35.500 --> 03:38.230
And then we're going to call this one as at AE.

03:38.230 --> 03:42.910
So this is all you have to do now at the time of deploying, we can go ahead and replace this with the

03:42.910 --> 03:46.660
Atlas or whatever the URL is there, wherever your database is hosted.

03:46.660 --> 03:47.470
That's it.

03:47.770 --> 03:48.210
Okay.

03:48.880 --> 03:52.140
Moving on now let's go into the config and database JS.

03:52.150 --> 03:56.980
And you guessed it right, this is exactly where we'll write the connection of the database string.

03:57.010 --> 03:58.330
Can we write it just like that?

03:58.330 --> 03:59.290
No, of course we cannot.

03:59.290 --> 04:02.770
We need to go in the documentation section and see how it is being done.

04:02.770 --> 04:07.420
So let's go into the Quickstart and we can see that all this is going on.

04:07.420 --> 04:10.300
But let's go ahead and see how we can connect.

04:10.300 --> 04:13.390
So connections are there and there we go.

04:13.390 --> 04:18.640
So it says Mongoose, Dot Connect, and you can just use the connection just like that, or you can

04:18.640 --> 04:19.570
go ahead and work with that.

04:19.570 --> 04:20.590
So this is pretty easy.

04:20.590 --> 04:23.380
They say this is just a one liner Mongoose dot connect.

04:23.980 --> 04:28.420
Technically, yes, this is one liner and they give you example of MongoDB call local localhost.

04:28.420 --> 04:32.210
And again, I forgot that we need to put up this port as well.

04:32.210 --> 04:33.880
I almost forgot that.

04:33.910 --> 04:40.450
Okay, let's go back into the DMV and we have to say colon 27017.

04:40.450 --> 04:46.960
This is a default port of MongoDB so don't scare get get scared off on that OC this is okay, we can

04:46.960 --> 04:51.430
definitely use that or the link from the online version that is okay.

04:51.430 --> 04:55.930
But let me tell you a little bit more on that, that how this should actually be done.

04:56.320 --> 05:01.570
Let's go back and moving into the database is the first thing obviously we need.

05:01.610 --> 05:02.000
Mongoose.

05:02.000 --> 05:06.140
So let's go ahead and say, hey, mongoose, that is going to be required.

05:06.530 --> 05:08.320
And we're going to say Mongoose.

05:08.330 --> 05:09.020
There we go.

05:09.710 --> 05:14.900
Once Mongoose is there, then we also need this variable that we have just declared in the environment

05:14.900 --> 05:15.290
variable.

05:15.290 --> 05:17.090
So let's also structure that.

05:17.210 --> 05:24.880
So that will be coming up from process dot, E and V and what did we call our variable MongoDB URL?

05:24.890 --> 05:27.800
Copy that and paste it up here.

05:29.300 --> 05:36.620
Now the documentation says that you go ahead and simply say Mongoose, dot connect and there is no problem

05:36.620 --> 05:36.950
in that.

05:36.950 --> 05:39.050
This is exactly how we should be doing it.

05:39.920 --> 05:42.130
So we're going to save MongoDB URL.

05:42.410 --> 05:46.970
But in addition, if you're going to save this, you are going to notice that it asks you to provide

05:46.970 --> 05:49.390
a couple of options, the connection options.

05:49.400 --> 05:54.230
Now, these connection options are pretty common, known everywhere on every StackOverflow and every

05:54.230 --> 05:54.670
one.

05:54.680 --> 05:59.810
The first one is actually use new URL parser.

05:59.810 --> 06:03.620
So previously MongoDB was using some kind of different parser.

06:03.620 --> 06:08.060
They updated it and now they want you to pass on the flag because they are legacy compatible.

06:08.060 --> 06:12.380
So that is why we need to use the modern parser that we are working on now.

06:12.380 --> 06:20.270
Also similar one more feature got update or one more settings got update which is use unified.

06:21.220 --> 06:22.340
Topology.

06:22.360 --> 06:27.640
Now there are a couple of more settings that you can go with that probably like use the new index and

06:27.640 --> 06:28.060
all of that.

06:28.060 --> 06:32.920
We don't definitely need this in this one, but we are going to figure them out later on.

06:32.950 --> 06:37.630
Now, again, sometimes it's a little bit easy to get a typo in that, so make sure you keep an eye

06:37.630 --> 06:38.100
on that.

06:38.110 --> 06:39.460
Hopefully I didn't made that.

06:39.730 --> 06:44.920
Now, as I told you, everything that you do with the Mongo or mongoose, they are always a promise.

06:44.920 --> 06:45.610
So promise.

06:45.610 --> 06:46.370
What does it return?

06:46.390 --> 06:48.670
It returns a dot, then.

06:49.720 --> 06:50.350
There we go.

06:50.350 --> 06:50.920
Just like that.

06:50.920 --> 06:54.430
And it also returns a dot catch because there can be a problem here.

06:54.550 --> 06:56.620
So what happens when we get a dot?

06:56.620 --> 07:00.520
Then let's go ahead and separate them on the separate line so that it's easier for us.

07:01.150 --> 07:05.260
So in the case of Dot catch, then obviously we are going to receive an error.

07:05.260 --> 07:08.080
So let's go ahead and handle that part just like this.

07:08.260 --> 07:17.110
And we're going to simply go ahead and first log this out and we are going to say that DB connection

07:17.110 --> 07:18.130
failed.

07:18.160 --> 07:18.910
There we go.

07:19.270 --> 07:23.830
Also, we need to log these errors, so let's go ahead and log them properly.

07:23.830 --> 07:25.090
So let's go ahead and dump them.

07:25.090 --> 07:25.870
All of that.

07:26.200 --> 07:27.880
Not a good strategy in the production.

07:27.880 --> 07:32.380
We want to have a separate error between the production as well as in the development environment.

07:32.380 --> 07:34.300
We can talk about that later on.

07:34.300 --> 07:38.470
And finally, the most important is process dot exit.

07:38.620 --> 07:43.620
Now again, the documentation doesn't give you in detail the depth of it because it is known.

07:43.630 --> 07:48.070
It should be known that, hey, there's going to be a promise and you need to handle the promises accordingly.

07:48.100 --> 07:53.830
We will take care of the things in the next project that how properly we can have these things and notice.

07:53.830 --> 07:57.100
On top of that there needs to be a sync one here.

07:57.100 --> 08:02.170
It is not required, but in all of the other cases it needs to be an async request with the promises

08:02.170 --> 08:03.820
or the TRICARE wrapper.

08:04.000 --> 08:08.170
Now, if everything goes right, then we are going to go ahead and simply, simply log a message that,

08:08.170 --> 08:10.000
hey, database connection is all good.

08:10.330 --> 08:14.470
So we're going to simply throw up that DB connected.

08:15.940 --> 08:17.500
Successfully.

08:17.830 --> 08:18.640
There we go.

08:18.640 --> 08:19.870
And we don't need this.

08:21.100 --> 08:21.580
Save this.

08:21.580 --> 08:22.540
So this is all done.

08:22.540 --> 08:29.230
But again, this is not all we have to do because we won't be using this into the DVD as we would be

08:29.230 --> 08:32.620
rather throwing up into a separate folder.

08:32.620 --> 08:35.500
So notice we are not holding this entire thing into available.

08:35.500 --> 08:36.670
Now we need to do that.

08:36.670 --> 08:41.560
So what we can do, easiest thing is let's just cut this out, all of this and we're going to go ahead

08:41.560 --> 08:46.180
and directly write exports dot connect because there will be not too much of the variables into this

08:46.180 --> 08:46.450
file.

08:46.450 --> 08:51.430
We can directly say that, hey, this is a method connect that I'm throwing this out, this is how this

08:51.430 --> 08:56.710
method looks like and we can go ahead and paste everything inside that.

08:56.710 --> 08:57.550
So there we go.

08:57.850 --> 09:02.290
Now anybody can import this connect and can start connecting with the database.

09:02.290 --> 09:05.830
So let's go ahead and use it into app dogs now in the app.

09:06.010 --> 09:11.290
JS One important thing, we don't need to hold this connect this connect into a variable.

09:11.290 --> 09:13.900
We can just run it directly as soon as it comes in.

09:14.020 --> 09:15.640
So let's go ahead and do that.

09:15.640 --> 09:20.530
So once Express is required, all of that, we can actually do it at the top.

09:20.800 --> 09:25.900
We're going to go ahead and say Hey required and make sure your dot inv is required as first because

09:25.900 --> 09:28.990
it's going to bring in MongoDB URL in case it needs it.

09:29.470 --> 09:32.470
So we're going to go ahead and save from the current directory.

09:32.470 --> 09:40.090
Let's go into config and inside that we are going to have a database JS and it has a method dot connect

09:40.090 --> 09:41.740
and just go ahead and run this.

09:41.740 --> 09:46.120
Now again, there can be many other ways you can hold that into a variable and then say that, hey,

09:46.120 --> 09:50.830
I am calling this XYZ and run this totally up to you how you want to do that.

09:50.950 --> 09:55.330
Now notice here it says, hey, crashed because probably there is something wrong in the URL.

09:55.360 --> 10:00.400
So it says invalid string which is mongo and notice it's not mongo.

10:00.400 --> 10:03.490
It is also mentioned up there it is MongoDB.

10:03.490 --> 10:10.120
So again referring to my env files again and this is MongoDB.

10:10.540 --> 10:16.750
Save that and we need to restart because of an E and v files are not being monitored and notice now

10:16.750 --> 10:18.790
it says DB connected successfully.

10:18.880 --> 10:22.120
So again with a little bit of the error, now we are working on that.

10:22.120 --> 10:22.450
Okay.

10:22.450 --> 10:24.100
So now our database connection is done.

10:24.100 --> 10:28.720
But still there are a couple of things you should always keep in mind that this always returns a promise.

10:28.720 --> 10:32.140
In fact, whenever you make a query, this is going to return.

10:32.140 --> 10:34.720
You promise and you need to handle that promise.

10:34.720 --> 10:38.770
Here we are writing absolute the basic one because we need the US thing functionality as well.

10:38.770 --> 10:44.590
So there are a couple of things we can definitely talk about, but that's all moving back onto the presentation

10:44.590 --> 10:46.510
now that what else we have to do.

10:46.510 --> 10:51.310
So we have talked to the database, we have already worked on, already registered, but how we're going

10:51.310 --> 10:53.710
to take care of the password, that is the big question.

10:53.710 --> 10:55.630
And the next question is token generation.

10:55.630 --> 11:01.180
So in the next video, let's talk about what are the points where we can talk, take care of the password

11:01.180 --> 11:03.100
and how we can actually take care of it.

11:03.100 --> 11:05.050
Let's go ahead and catch up in the next video.
