1 00:00:01,080 --> 00:00:03,580 We just created our Mongo DB instance which is great. 2 00:00:03,610 --> 00:00:07,770 So now we need to make sure that our auth service actually connects to it before we do so however I 3 00:00:07,770 --> 00:00:09,960 want to give you a very quick little note. 4 00:00:10,260 --> 00:00:16,300 Right now we have created a deployment that creates a pod that is going to run our Mongo DB instance. 5 00:00:16,350 --> 00:00:21,960 We haven't really spoken about the lifecycle of pods in communities yet but suffice it to say if we 6 00:00:21,960 --> 00:00:25,620 ever delete or restart the pod that's running Mongo DB. 7 00:00:25,650 --> 00:00:29,650 Then we're going to lose all the data that is loaded into our database. 8 00:00:29,820 --> 00:00:32,950 We're gonna come back and fix this up at a later section. 9 00:00:33,090 --> 00:00:37,350 We're going to discuss why releasing this data and we're going to figure out some way to solve it and 10 00:00:37,350 --> 00:00:40,400 give a mongo DV some persistent storage. 11 00:00:40,470 --> 00:00:44,190 But for right now I really just want to focus on the authentication side of things. 12 00:00:45,050 --> 00:00:47,300 All right so let's go back over to our editor. 13 00:00:47,580 --> 00:00:55,200 We're going to find our auth service inside there we'll find our index dot t s file not GST us. 14 00:00:55,500 --> 00:01:00,120 And we're just going to add in a little bit of code to reach out to mongo DV and connect to it using 15 00:01:00,210 --> 00:01:01,410 mongoose. 16 00:01:01,410 --> 00:01:05,220 Now I can get through and mongoose in the last video I mentioned it without really saying anything about 17 00:01:05,220 --> 00:01:06,350 what mongoose is. 18 00:01:06,420 --> 00:01:10,950 If you're here in this course I'm going to kind of assume that you've got a reasonable idea of what 19 00:01:10,950 --> 00:01:11,750 mongoose is. 20 00:01:11,850 --> 00:01:16,260 It is a library that we use to get access to Mongo DB and work with data inside of it. 21 00:01:16,320 --> 00:01:21,600 If you are not at all familiar with Mongoose then we're going to kind of just say All right that's life. 22 00:01:21,600 --> 00:01:25,460 Just go ahead and copy the code that we write and hopefully you'll learn something along the way. 23 00:01:25,500 --> 00:01:29,670 We're not going to go too deep into the world of Mongoose because it is kind of far removed from this 24 00:01:29,700 --> 00:01:32,960 whole idea of micros services all right. 25 00:01:32,960 --> 00:01:42,480 So inside of indexed next to us at the top let's import Mongoose from Mongoose you'll notice right away 26 00:01:42,480 --> 00:01:44,010 that we've got an airtight mongoose. 27 00:01:44,010 --> 00:01:49,450 If we hover over that area it tells us a typescript doesn't know anything about this Mongoose library. 28 00:01:49,500 --> 00:01:54,510 So to solve that it wants us to install a tight definition file so that typescript understands what 29 00:01:54,510 --> 00:01:56,340 is going on inside this library. 30 00:01:56,340 --> 00:01:59,430 To do so we'll run the command you see right there. 31 00:01:59,430 --> 00:02:03,680 So back at my terminal inside of my off sub project. 32 00:02:03,690 --> 00:02:08,370 Once again I'll run that command. 33 00:02:08,400 --> 00:02:11,070 Now if I flip back over there goes away. 34 00:02:11,070 --> 00:02:11,270 All right. 35 00:02:11,280 --> 00:02:12,740 Well that's good. 36 00:02:12,750 --> 00:02:17,310 Now that we've got Mongoose inside of this file we're going to use Mongoose to connect to that Mongo 37 00:02:17,310 --> 00:02:18,640 DB instance. 38 00:02:18,690 --> 00:02:23,070 There are many different ways that we can connect to a mongo instance using mongoose. 39 00:02:23,070 --> 00:02:26,600 We're going to do so by using the async await syntax. 40 00:02:26,610 --> 00:02:33,670 Let me show you how at the very part of the file I'm going to find where we have got that app. 41 00:02:33,700 --> 00:02:36,590 Listen line right above that line. 42 00:02:36,630 --> 00:02:45,220 I'm going to create a new function called start or start up I want to start keep simple I'm going to 43 00:02:45,220 --> 00:02:53,220 mark this function as being async and then inside of here I will write out a weights Mongoose dot connect 44 00:02:54,030 --> 00:02:57,260 the first argument to connect is going gonna be a connection that you are El. 45 00:02:57,370 --> 00:03:02,730 So this is going to be the actual you URL of the Mongo DB instance that we need to connect to the front. 46 00:03:02,730 --> 00:03:06,200 Now we're just going to write out the URL directly inside of here. 47 00:03:06,330 --> 00:03:10,890 Usually if we are running or connecting to a mongo instance that is on our local machine it would be 48 00:03:10,890 --> 00:03:15,750 something very simple like Mongo D.B. all slash slash local host. 49 00:03:16,200 --> 00:03:20,570 But in our case we are not really connecting to a mongo DB instance that is available on local host. 50 00:03:20,590 --> 00:03:26,310 Instead it is available inside of this other pod over here. 51 00:03:26,310 --> 00:03:31,460 And remember to connect to that other pod we have to go through that cluster I.P. service we had created. 52 00:03:31,540 --> 00:03:36,270 Anytime we are trying to connect to a cluster I.P. service we're going to write in the name of that 53 00:03:36,270 --> 00:03:41,940 cluster I.P. service where we would normally put a domain so if we need a reminder we could always go 54 00:03:41,940 --> 00:03:49,560 back to the infra Cade's directory and then open up the off Mongo deployment file and then go down to 55 00:03:49,560 --> 00:03:56,640 our service our service for the Mongo DB instance has a name of off Mongo SRB but that is the domain 56 00:03:56,640 --> 00:04:01,320 name that we're going to attempt to connect to to get to our Mongo DB database. 57 00:04:01,320 --> 00:04:06,600 So in other words we will write out Mongoose dot connect then still have that Mongo DB Collins slash 58 00:04:06,600 --> 00:04:11,100 slash and then a right there instead a local host we will put off Mongo SRB 59 00:04:14,760 --> 00:04:21,370 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 60 00:04:21,370 --> 00:04:28,230 port and then we can put on the name of the actual database inside of there that we want to connect 61 00:04:28,230 --> 00:04:34,560 to if we make up a name right here Mongo DB or mongoose will create the database for us automatically. 62 00:04:34,560 --> 00:04:39,910 We're going to create a new database inside of your called off after that. 63 00:04:39,910 --> 00:04:41,620 We're gonna put in an options object. 64 00:04:41,650 --> 00:04:50,450 We're gonna throw a couple of options inside of here we'll say use new you are L parser true use unified 65 00:04:51,550 --> 00:04:57,380 topology true and use create index of true. 66 00:04:57,670 --> 00:05:02,070 These options are not super relevant they are just configuration ops and options for mongoose. 67 00:05:02,140 --> 00:05:06,070 Really the only reason we're putting them inside of here is that if we don't mongoose is going to start 68 00:05:06,070 --> 00:05:10,450 to throw a couple of warnings and errors at us. 69 00:05:10,480 --> 00:05:13,290 All right so that's it for our next statement. 70 00:05:13,350 --> 00:05:17,670 Now this is going to hold up or it's going to wait for the connection. 71 00:05:17,670 --> 00:05:21,630 If we fail to connect for any reason then this line is going to throw an error. 72 00:05:21,690 --> 00:05:26,340 Let's try to capture that era that might be thrown and console log it or do something like that just 73 00:05:26,340 --> 00:05:30,030 so you and I are aware if our service fails to connect to DB. 74 00:05:30,730 --> 00:05:32,760 So I gotta wrap that with a try 75 00:05:37,610 --> 00:05:39,240 catch. 76 00:05:39,420 --> 00:05:45,980 And so if something goes wrong and we end up with that era there we'll do a console dot air and log 77 00:05:45,980 --> 00:05:50,300 out that air. 78 00:05:50,540 --> 00:05:50,770 All right. 79 00:05:50,780 --> 00:05:54,890 So if we get past this tri catch statement successfully then it is probably time for us to actually 80 00:05:54,890 --> 00:05:56,300 start listening for traffic. 81 00:05:56,390 --> 00:05:58,750 So I gonna take the app to listen line right here. 82 00:05:58,820 --> 00:06:04,210 That whole thing I'm going to cut it and put it right in after the try catch you'll notice that we've 83 00:06:04,210 --> 00:06:09,220 now got a single function called start that is going to first try to connect to our database if it successfully 84 00:06:09,220 --> 00:06:10,140 connects fantastic. 85 00:06:10,150 --> 00:06:11,000 That's great. 86 00:06:11,050 --> 00:06:15,730 If it fails to then we're going to log the error that just occurred at then after all that we'll start 87 00:06:15,730 --> 00:06:17,710 to listen for incoming traffic. 88 00:06:17,710 --> 00:06:22,270 The reason that we defined a separate function of starts is that depending upon the version of node 89 00:06:22,300 --> 00:06:28,980 that you are running you have to wrap all of your async await code inside of a function. 90 00:06:29,050 --> 00:06:34,830 The latest version of node can make use of the awake keyword at the top level. 91 00:06:34,870 --> 00:06:40,030 So in other words outside of a function but you might be using a version of node that does not support 92 00:06:40,030 --> 00:06:45,850 this or to be more precise the node image that you are downloading to use inside of your cluster might 93 00:06:45,850 --> 00:06:47,100 not be the latest version. 94 00:06:47,200 --> 00:06:53,430 So that's why you're just finding that extra function just so we can use async await after we defined 95 00:06:53,430 --> 00:06:57,120 that function will then call it right there. 96 00:06:57,120 --> 00:07:01,500 So when our application first starts up just go ahead and run that start function do the connection 97 00:07:01,650 --> 00:07:03,810 start listening to traffic. 98 00:07:04,000 --> 00:07:04,260 All right. 99 00:07:04,280 --> 00:07:09,790 Let's save this look back over to our terminal where we are running scaffold and see how we are doing 100 00:07:11,000 --> 00:07:11,210 all right. 101 00:07:11,220 --> 00:07:14,480 Well it looks like everything is going as expected. 102 00:07:14,520 --> 00:07:17,090 So I still have a listening up port three thousand right here. 103 00:07:17,130 --> 00:07:22,050 I don't have any errors around connecting to mongo but just to make sure that we are actually running 104 00:07:22,050 --> 00:07:23,370 the correct code here. 105 00:07:23,400 --> 00:07:25,410 How about right after we do this connect statement. 106 00:07:25,410 --> 00:07:34,540 We just do a quick console log right here that says something like connected to mongo D.B. we'll save 107 00:07:34,540 --> 00:07:40,870 that as well put back over yep looks good All right. 108 00:07:40,900 --> 00:07:45,460 So we've now got our Mogadishu instance up and running and our authentication application has connected 109 00:07:45,460 --> 00:07:46,150 to it. 110 00:07:46,390 --> 00:07:49,110 Now that we've got this ability to store some information. 111 00:07:49,130 --> 00:07:53,170 Let's talk about how we're going to create users and store them inside this database in the next video.