WEBVTT 00:02.270 --> 00:06.470 So in this section of the course will be interacting with the database, which means that we need to 00:06.470 --> 00:09.630 connect both our front and back ends to the database. 00:10.280 --> 00:15.680 So what I'm going to do is go to my internal folder at the root level of my project, and I'll create 00:15.680 --> 00:25.070 a new directory there, which are called Driver, and inside of that can create a new file, which I'll 00:25.070 --> 00:26.960 call Driver Dutko. 00:27.290 --> 00:29.410 And this will be where we connect to the database. 00:29.420 --> 00:31.750 So let's give it a package declaration driver. 00:32.540 --> 00:39.560 And in that, I'm going to have one function and I'll call that function open DB and I'm going to export 00:39.560 --> 00:43.310 that function by giving it a capital letter as it's at the start of its name. 00:44.150 --> 00:46.550 And I will take one argument, which will be DSN. 00:47.390 --> 00:54.260 That's the database connection string and it's a string and it will return to things. 00:54.680 --> 01:03.080 A pointer to sequel DB and I hit Enter to let Visual Studio Code import that for me and potentially 01:03.080 --> 01:03.410 in error. 01:04.190 --> 01:11.870 OK, now this database connection function is going to connect to my school or actually Maria DB, so 01:11.870 --> 01:14.500 we need to import the necessary code for that. 01:14.510 --> 01:25.070 So I'll put a terminal window and I will type go get GitHub dot com go dash sequel dash driver slash 01:25.220 --> 01:30.080 my screw up and that will go and get the necessary driver for us. 01:30.920 --> 01:34.210 So that means we need to actually modify our import statement. 01:34.220 --> 01:37.310 So I'll surround the whole thing in parentheses like this. 01:39.830 --> 01:46.220 And at the very bottom of the of those parentheses just before the closing one, I'll import what I 01:46.220 --> 01:50.960 need with an underscore the blank identifier that says I'm not calling this directly, but I'm going 01:50.960 --> 01:51.410 to use it. 01:52.160 --> 01:59.410 GitHub dot com slash go dash sequel dash driver slash my school. 02:00.530 --> 02:06.530 OK, so now we can actually connect to the database and it's really simple and you can go read the documentation 02:06.530 --> 02:08.660 right at the URL there from line six. 02:08.660 --> 02:09.950 But I'm sure you've done this before. 02:10.610 --> 02:14.800 DBI and error are assigned the value of from the school package. 02:14.810 --> 02:22.610 We're going to open and we're going to open a minuscule connection and we're going to pass that our 02:22.610 --> 02:25.820 connection straight down and we'll check for an error. 02:26.240 --> 02:33.410 If error is not equal to nil, then we'll just return nil because we have no connection pool and the 02:33.410 --> 02:33.710 error. 02:34.640 --> 02:41.990 And then just to be absolutely safe, updating the database error equals DB ping. 02:43.690 --> 02:53.500 And if error is not equal to nil, then I'll print the error out format of print line error and I'll 02:53.500 --> 02:55.270 return nil and the error. 02:56.470 --> 03:02.200 And finally, if we've managed to ping it, then we can return our database connection pool and no error. 03:02.880 --> 03:08.030 OK, so now we have a driver package so close by terminal. 03:08.440 --> 03:11.470 And let me first of all, hook up Mingo. 03:12.010 --> 03:17.140 So that's inside our command web and there it is, Mingo. 03:18.250 --> 03:24.460 So I'm going to read the connection string right from a flag of the we're going to pass to this. 03:24.460 --> 03:27.190 And if you want to use an environment variable, that's fine, too. 03:28.030 --> 03:30.340 But I'm going to read it right from the command line for now. 03:30.340 --> 03:31.450 So I'll say flag. 03:33.820 --> 03:47.230 Scott Stringer, I'm going to read it into our CFT DB, populate our config variable and the flag will 03:47.230 --> 03:53.310 be called down and I'll give it a default value so I can work in development without any difficulty. 03:53.320 --> 03:54.990 And my connection string looks like this. 03:55.750 --> 04:02.140 My username, which I called Trevor Colen my password and I chose a secret which isn't very secure, 04:02.140 --> 04:06.440 but I'm in development and I don't care at TCP. 04:06.670 --> 04:13.330 That's how I'm going to connect and I'm connecting to localhost Colen three three oh six. 04:14.590 --> 04:20.110 Then close my parentheses and I've already created a database and I'll call my Kullman widgets because 04:20.110 --> 04:22.090 we're selling widgets, whatever those happen to be. 04:22.810 --> 04:29.770 And then because I'm working in Go with my school or Marea DB which is compatible with my school, I'll 04:29.770 --> 04:37.780 put the question mark and and hand at the flag pass time with a capital T equals true and then I'll 04:37.780 --> 04:38.860 put an ampersand. 04:39.040 --> 04:40.720 RTLS equals false. 04:40.750 --> 04:47.260 So I'm not using SSL and the connection help text is just dsf. 04:48.130 --> 04:53.190 OK, so that actually will read in the DSM our connection string. 04:53.200 --> 04:58.120 And of course yours will be different than mine, but it will follow this basic format that we have 04:58.120 --> 04:59.930 here in line fifty seven in my code. 05:01.840 --> 05:08.260 Now after that happens, say right after I declare my logger's here on line sixty five and sixty six 05:08.260 --> 05:11.430 in my code, let's actually try to connect to the database. 05:11.440 --> 05:14.920 So I'll call my variable con and I'm going to check for an error. 05:15.010 --> 05:20.860 And that's a sign the value of from the driver package dot. 05:23.540 --> 05:24.410 Open divi. 05:29.190 --> 05:38.430 There it is and I'm going to hand it cfg dot divi dsn and I'll check for her. 05:39.000 --> 05:48.090 If error is not equal to nil, then error log dot print one error. 05:50.260 --> 05:52.130 Actually, that's just stop at this point. 05:52.140 --> 05:59.280 We'll make that fatal instead is that I really don't want to go any further if I don't have my necessary 05:59.280 --> 06:00.390 connection to the database. 06:00.670 --> 06:11.040 OK, and then we'll defer conduct close because we don't want to leave our resources open and I want 06:11.040 --> 06:15.910 that connection pool to remain open until the application stops. 06:16.170 --> 06:24.600 So at this point, I should be able to open my terminal, clear the screen and try running this. 06:24.600 --> 06:30.510 So I'll just run it like this make start front and see if it works. 06:32.640 --> 06:34.930 And I have a typo and it's an easy one. 06:35.040 --> 06:40.140 So line eighty five right here, that has to go away. 06:40.650 --> 06:43.490 OK, let's clear the screen and try that again. 06:43.590 --> 06:44.520 Make start front. 06:47.360 --> 06:52.610 And we connected to our database perfect, so now we have a means of connecting to our database and 06:52.610 --> 06:57.660 of course, that means we have to do the same logic in our API Dongo file. 06:57.860 --> 06:59.000 And that's pretty straightforward. 07:00.090 --> 07:02.930 Let's just stop this so mixed up. 07:04.460 --> 07:11.210 Everything is stopped and I'll open my API and let me copy this line first, because I know I'm going 07:11.210 --> 07:11.660 to use it. 07:12.800 --> 07:14.690 So we'll open API Dargo 07:18.050 --> 07:19.480 and write in the main function. 07:19.910 --> 07:27.800 I'll put it right here paste and get rid of the blank line and go back to main Dongo and just copy the 07:27.800 --> 07:29.690 same logic right here. 07:32.250 --> 07:39.960 And modify as necessary, so back in April, go, we'll put it in the same place, paced, and I need 07:39.960 --> 07:48.390 to import that driver, so I'll just go copy it from the import statement in mean, go right here and 07:48.390 --> 07:48.780 back in. 07:49.520 --> 07:49.860 Go. 07:51.150 --> 07:52.200 I'll put it right here. 07:53.910 --> 07:55.400 So that makes that area go away. 07:55.410 --> 07:57.840 And there should be one more error right down here. 07:59.790 --> 08:05.550 So at this point I can clear the screen and type and make start and I should start both the front and 08:05.550 --> 08:06.210 back end. 08:06.450 --> 08:08.070 And it did perfect. 08:08.430 --> 08:10.290 So we have a connection to the database now. 08:10.290 --> 08:14.640 We have nothing in the database and we're going to have to create a few tables and we'll get started 08:14.640 --> 08:16.590 on that in the next lesson.