We just created our Mongo DB instance which is great.
So now we need to make sure that our auth service actually connects to it before we do so however I
want to give you a very quick little note.
Right now we have created a deployment that creates a pod that is going to run our Mongo DB instance.
We haven't really spoken about the lifecycle of pods in communities yet but suffice it to say if we
ever delete or restart the pod that's running Mongo DB.
Then we're going to lose all the data that is loaded into our database.
We're gonna come back and fix this up at a later section.
We're going to discuss why releasing this data and we're going to figure out some way to solve it and
give a mongo DV some persistent storage.
But for right now I really just want to focus on the authentication side of things.
All right so let's go back over to our editor.
We're going to find our auth service inside there we'll find our index dot t s file not GST us.
And we're just going to add in a little bit of code to reach out to mongo DV and connect to it using
mongoose.
Now I can get through and mongoose in the last video I mentioned it without really saying anything about
what mongoose is.
If you're here in this course I'm going to kind of assume that you've got a reasonable idea of what
mongoose is.
It is a library that we use to get access to Mongo DB and work with data inside of it.
If you are not at all familiar with Mongoose then we're going to kind of just say All right that's life.
Just go ahead and copy the code that we write and hopefully you'll learn something along the way.
We're not going to go too deep into the world of Mongoose because it is kind of far removed from this
whole idea of micros services all right.
So inside of indexed next to us at the top let's import Mongoose from Mongoose you'll notice right away
that we've got an airtight mongoose.
If we hover over that area it tells us a typescript doesn't know anything about this Mongoose library.
So to solve that it wants us to install a tight definition file so that typescript understands what
is going on inside this library.
To do so we'll run the command you see right there.
So back at my terminal inside of my off sub project.
Once again I'll run that command.
Now if I flip back over there goes away.
All right.
Well that's good.
Now that we've got Mongoose inside of this file we're going to use Mongoose to connect to that Mongo
DB instance.
There are many different ways that we can connect to a mongo instance using mongoose.
We're going to do so by using the async await syntax.
Let me show you how at the very part of the file I'm going to find where we have got that app.
Listen line right above that line.
I'm going to create a new function called start or start up I want to start keep simple I'm going to
mark this function as being async and then inside of here I will write out a weights Mongoose dot connect
the first argument to connect is going gonna be a connection that you are El.
So this is going to be the actual you URL of the Mongo DB instance that we need to connect to the front.
Now we're just going to write out the URL directly inside of here.
Usually if we are running or connecting to a mongo instance that is on our local machine it would be
something very simple like Mongo D.B. all slash slash local host.
But in our case we are not really connecting to a mongo DB instance that is available on local host.
Instead it is available inside of this other pod over here.
And remember to connect to that other pod we have to go through that cluster I.P. service we had created.
Anytime we are trying to connect to a cluster I.P. service we're going to write in the name of that
cluster I.P. service where we would normally put a domain so if we need a reminder we could always go
back to the infra Cade's directory and then open up the off Mongo deployment file and then go down to
our service our service for the Mongo DB instance has a name of off Mongo SRB but that is the domain
name that we're going to attempt to connect to to get to our Mongo DB database.
So in other words we will write out Mongoose dot connect then still have that Mongo DB Collins slash
slash and then a right there instead a local host we will put off Mongo SRB
after that we will then add in the ports to the very end which is 20 7 0 1 7 we are using the default
port and then we can put on the name of the actual database inside of there that we want to connect
to if we make up a name right here Mongo DB or mongoose will create the database for us automatically.
We're going to create a new database inside of your called off after that.
We're gonna put in an options object.
We're gonna throw a couple of options inside of here we'll say use new you are L parser true use unified
topology true and use create index of true.
These options are not super relevant they are just configuration ops and options for mongoose.
Really the only reason we're putting them inside of here is that if we don't mongoose is going to start
to throw a couple of warnings and errors at us.
All right so that's it for our next statement.
Now this is going to hold up or it's going to wait for the connection.
If we fail to connect for any reason then this line is going to throw an error.
Let's try to capture that era that might be thrown and console log it or do something like that just
so you and I are aware if our service fails to connect to DB.
So I gotta wrap that with a try
catch.
And so if something goes wrong and we end up with that era there we'll do a console dot air and log
out that air.
All right.
So if we get past this tri catch statement successfully then it is probably time for us to actually
start listening for traffic.
So I gonna take the app to listen line right here.
That whole thing I'm going to cut it and put it right in after the try catch you'll notice that we've
now got a single function called start that is going to first try to connect to our database if it successfully
connects fantastic.
That's great.
If it fails to then we're going to log the error that just occurred at then after all that we'll start
to listen for incoming traffic.
The reason that we defined a separate function of starts is that depending upon the version of node
that you are running you have to wrap all of your async await code inside of a function.
The latest version of node can make use of the awake keyword at the top level.
So in other words outside of a function but you might be using a version of node that does not support
this or to be more precise the node image that you are downloading to use inside of your cluster might
not be the latest version.
So that's why you're just finding that extra function just so we can use async await after we defined
that function will then call it right there.
So when our application first starts up just go ahead and run that start function do the connection
start listening to traffic.
All right.
Let's save this look back over to our terminal where we are running scaffold and see how we are doing
all right.
Well it looks like everything is going as expected.
So I still have a listening up port three thousand right here.
I don't have any errors around connecting to mongo but just to make sure that we are actually running
the correct code here.
How about right after we do this connect statement.
We just do a quick console log right here that says something like connected to mongo D.B. we'll save
that as well put back over yep looks good All right.
So we've now got our Mogadishu instance up and running and our authentication application has connected
to it.
Now that we've got this ability to store some information.
Let's talk about how we're going to create users and store them inside this database in the next video.