1
00:00:01,570 --> 00:00:08,020
So we have a driver package set up, and now it's time to use that driver package to connect our application

2
00:00:08,020 --> 00:00:11,550
to the database, and we're going to do that using the repository pattern.

3
00:00:12,100 --> 00:00:14,640
So we already have one repository, if you recall.

4
00:00:14,650 --> 00:00:18,700
And if we look at our main go file, we can see where that is.

5
00:00:19,660 --> 00:00:25,180
And that's way down here where we create a new repository and that calls Handler's new repo.

6
00:00:25,900 --> 00:00:32,620
And what we're going to do is add something to this repository, which is itself a repository, because

7
00:00:32,620 --> 00:00:39,430
our my goal here is to set up my application in such a way that right now I'm going to use postgrads.

8
00:00:39,460 --> 00:00:46,630
But if down the road I want to use, say, my as well or Maria DB or Oracle or some other database,

9
00:00:47,050 --> 00:00:48,190
I can do so.

10
00:00:48,460 --> 00:00:54,160
And when I make that change, I change as little of the application as possible.

11
00:00:54,190 --> 00:00:58,240
And the repository pattern is ideal for that particular situation.

12
00:00:58,930 --> 00:01:04,300
So what I'm going to do is I'm going to create a new package and I'm going to do it on my internal I'm

13
00:01:04,300 --> 00:01:05,680
going to create a new package.

14
00:01:06,280 --> 00:01:08,920
So I'll just create a new directory, which will be a package.

15
00:01:09,340 --> 00:01:11,200
And I'm going to call that repository.

16
00:01:13,850 --> 00:01:20,420
And inside of that, I'm going to create another package, so inside of the repository directory, I'll

17
00:01:20,720 --> 00:01:27,620
create another directory which I'm going to call DB Repo, and at the root level of repository, not

18
00:01:27,620 --> 00:01:31,220
inside the DP Rebo DB repo, but inside the repository folder.

19
00:01:31,220 --> 00:01:36,680
I'll create a gopher and I will call that repository Dongo.

20
00:01:37,790 --> 00:01:42,530
And I will add that to get and that file right now is very, very simple.

21
00:01:42,620 --> 00:01:50,060
I'm just going to create a type in here, an interface type, and I'll call it database repo, which

22
00:01:50,060 --> 00:01:53,930
is an interface, and right now it will be empty, will add something to that in a little while.

23
00:01:55,100 --> 00:01:59,210
And inside of the DB repo folder, I'll create a new file.

24
00:02:02,800 --> 00:02:08,760
And I will call that, not surprisingly, debri pogo, and I will add that to get as well.

25
00:02:10,150 --> 00:02:15,760
Now inside of here, I'm going to create a type and that type will be the repository itself and the

26
00:02:15,760 --> 00:02:20,200
type will be called Postgres Divi Repo.

27
00:02:20,950 --> 00:02:23,770
And it's a struct and it will hold two things.

28
00:02:23,800 --> 00:02:29,310
It will hold up an app, which will be a pointer to our config app config.

29
00:02:30,400 --> 00:02:35,800
So it's going to hold that and it's also going to hold a database connection pool, which, as you know,

30
00:02:35,800 --> 00:02:38,500
is a pointer to four postgrads of sequel.

31
00:02:38,500 --> 00:02:42,440
DeeVee So that's a type which I'm currently not using.

32
00:02:42,850 --> 00:02:44,230
Now I'm going to create a new function.

33
00:02:44,230 --> 00:02:50,560
And just as we did with our other repository, this is created using a function which I'll call new

34
00:02:50,620 --> 00:02:53,350
postgrads repoll.

35
00:02:56,170 --> 00:03:01,270
And it will take a parameter, actually a couple of parameters, one I'll call Con because it's a connection

36
00:03:01,270 --> 00:03:08,700
pool and it's a pointer to sequel DB and the other one is a and it's just a pointer to our config config

37
00:03:09,340 --> 00:03:14,680
and it's going to return a repository database repo.

38
00:03:16,210 --> 00:03:17,380
But our spell repository.

39
00:03:17,380 --> 00:03:17,580
Right.

40
00:03:21,220 --> 00:03:29,020
And just as we did with our other repository, we'll just return a reference to Postgrads Divi Rebo

41
00:03:29,650 --> 00:03:35,750
and of course, that has to take two arguments in order to be populated at which we have as a parameter

42
00:03:35,750 --> 00:03:40,580
we're passing here A and our DB, which will be our connection pool.

43
00:03:41,530 --> 00:03:41,970
All right.

44
00:03:42,490 --> 00:03:50,200
So now I have a means of passing this function, our connection pool and our app config and returning

45
00:03:50,200 --> 00:03:51,060
a repository.

46
00:03:51,100 --> 00:03:57,130
And because I'm now initializing this using new Postgres repo, it will be a connection pool that has

47
00:03:57,580 --> 00:03:58,690
Postgres connection pool.

48
00:03:59,680 --> 00:04:06,100
If I wanted to have another database, I would simply create a new type say type MySQL DB repo, which

49
00:04:06,130 --> 00:04:11,230
would hold whatever is required for that are up configuring a connection pool for for my school.

50
00:04:11,620 --> 00:04:17,650
And I'd create a new function called func new MySQL Repo and create that.

51
00:04:17,680 --> 00:04:21,760
So now I have a means of swapping out my database very, very quickly.

52
00:04:22,300 --> 00:04:22,710
All right.

53
00:04:22,720 --> 00:04:24,460
So this is created now.

54
00:04:24,460 --> 00:04:25,510
What am I going to do with this?

55
00:04:25,540 --> 00:04:28,120
Well, let's go back to our main file.

56
00:04:29,920 --> 00:04:34,180
May not go and down in the run function.

57
00:04:35,240 --> 00:04:37,180
I want to initialize my database here.

58
00:04:37,270 --> 00:04:39,550
I want to actually initialize my database repo.

59
00:04:39,550 --> 00:04:41,190
And I can do that pretty easily.

60
00:04:41,590 --> 00:04:51,640
I could say right after this Aptos session, I can just say connect to a database and what am I going

61
00:04:51,640 --> 00:04:51,970
to do?

62
00:04:52,090 --> 00:05:01,510
I'll write to the terminal log print line connecting to the database just to get some feedback and then

63
00:05:01,510 --> 00:05:02,380
actually do the connection.

64
00:05:04,200 --> 00:05:10,530
So I will call DB error, I'm going to get my connection pool and I get that from my driver package

65
00:05:10,530 --> 00:05:12,270
that we created in the last lecture.

66
00:05:12,960 --> 00:05:14,900
And the math on that is Connect sequel.

67
00:05:14,910 --> 00:05:17,420
And then we have our DSN and our DSN.

68
00:05:17,430 --> 00:05:18,720
I'm going to hardcoded right now.

69
00:05:18,720 --> 00:05:21,110
We'll change that before we're finished with this project.

70
00:05:21,120 --> 00:05:23,280
But right now I'm just going to hardcoded just to get it working.

71
00:05:23,580 --> 00:05:26,850
So we'll do it just like we did in our last hour.

72
00:05:27,090 --> 00:05:34,140
Test database application post equals localhost port equals five, four, three, two.

73
00:05:35,100 --> 00:05:38,280
DB name is equal to bookings.

74
00:05:39,010 --> 00:05:40,020
I think that's what I called it.

75
00:05:40,200 --> 00:05:41,060
We'll find out in a minute.

76
00:05:41,730 --> 00:05:47,340
User equals ticks and of course your username will be different and my password is currently empty.

77
00:05:47,340 --> 00:05:50,850
So I'll just put a password in that I can leave it out, but eventually you're going to be connecting

78
00:05:50,850 --> 00:05:52,260
to a database that has a password.

79
00:05:52,260 --> 00:05:54,720
So we may as well take that into account right now.

80
00:05:55,590 --> 00:05:56,730
So that will do our connection.

81
00:05:56,730 --> 00:05:57,960
Then we test for our error.

82
00:05:57,990 --> 00:06:00,930
If error is not equal to nil, what do I want to do?

83
00:06:00,930 --> 00:06:06,480
I'll I'll just log fatele because if I can't connect to my database, bad things are happening and I'll

84
00:06:06,480 --> 00:06:07,470
print the message.

85
00:06:07,620 --> 00:06:15,030
Cannot connect to database dying because I'll die with that log fail.

86
00:06:15,780 --> 00:06:19,500
Now that I have this connected, I have my database connection pool.

87
00:06:19,530 --> 00:06:20,510
What am I going to do with it?

88
00:06:21,030 --> 00:06:25,290
Well, if you recall, one of the things that I said we should do is close it when we're done with it

89
00:06:25,290 --> 00:06:34,680
so I could write deferred DB Forclose DB dot SQL close.

90
00:06:35,520 --> 00:06:37,370
But if I do that, what's going to happen?

91
00:06:38,010 --> 00:06:42,300
I'm actually in a function called Run, which is called up here.

92
00:06:42,510 --> 00:06:44,730
Very first thing I do in my main function is call that.

93
00:06:45,180 --> 00:06:52,500
So if I open my connection to the database and then put defer close and then the run function is finished,

94
00:06:52,920 --> 00:06:59,190
it will close the database connections immediately after the run finishes, which you know is no good

95
00:06:59,190 --> 00:07:02,070
because I'll close my database connections right after opening them.

96
00:07:02,370 --> 00:07:03,650
So that's not going to be helpful.

97
00:07:04,050 --> 00:07:10,710
So what I need to do instead is have this not here, take it out and have this run function return more

98
00:07:10,710 --> 00:07:11,700
than a potential error.

99
00:07:11,970 --> 00:07:13,380
So I'll have it return.

100
00:07:13,980 --> 00:07:15,240
What shall I have it return.

101
00:07:15,240 --> 00:07:16,290
I shall have a return.

102
00:07:16,290 --> 00:07:21,570
My driver DB So it's a pointer to driver DB and an error.

103
00:07:22,630 --> 00:07:25,440
So now I got a few things to fix down here.

104
00:07:25,440 --> 00:07:26,820
I can't return nothing.

105
00:07:27,180 --> 00:07:28,080
Return just the air.

106
00:07:28,080 --> 00:07:30,350
I have to return nil in the error because I couldn't create it.

107
00:07:30,510 --> 00:07:39,030
Just die at this point and down here I'll return my DB so that will take care of the errors for the

108
00:07:39,030 --> 00:07:39,780
run function.

109
00:07:40,260 --> 00:07:44,460
But up here I can't just er have er equals run.

110
00:07:44,460 --> 00:07:53,520
Instead I say db and error equals run and I'm not using DB right now, but if I get past this then I

111
00:07:53,520 --> 00:08:03,390
want to defer DB sequel Duclos and now the database connection won't be closed until the main function

112
00:08:03,600 --> 00:08:06,720
itself stops running, which means the application is stopped.

113
00:08:06,720 --> 00:08:07,610
So that's very good.

114
00:08:08,100 --> 00:08:13,980
So I've actually got this database connection right now, but I'm not doing anything with it and I need

115
00:08:13,980 --> 00:08:14,940
to do something with it.

116
00:08:14,940 --> 00:08:16,550
So what shall I do with it?

117
00:08:17,430 --> 00:08:19,140
Well, back down here in the run function.

118
00:08:21,030 --> 00:08:27,060
I need to ensure that my database connection is available and certainly to my handlers, possibly something

119
00:08:27,060 --> 00:08:28,950
else, but certainly to my handlers.

120
00:08:29,610 --> 00:08:36,940
So over in our handlers, what I'm going to do is change this repository and have it hold another repository.

121
00:08:37,500 --> 00:08:39,260
So what am I going to put in here?

122
00:08:39,840 --> 00:08:48,600
I call it DBI because of the database connection and it's a type repository that, again, repository

123
00:08:50,190 --> 00:08:51,060
database repo.

124
00:08:52,050 --> 00:08:58,580
And down here I'll actually accept a second parameter and it will not be a repository.

125
00:08:58,740 --> 00:09:06,270
It will instead be a DB, which is a pointer to driver DB and I'll have to pass that in the main function.

126
00:09:06,270 --> 00:09:07,090
But we'll do that in a minute.

127
00:09:08,010 --> 00:09:13,890
And now I will populate my DB repository, not with DB because it's not a repository.

128
00:09:13,890 --> 00:09:15,530
That's a pointer to driver DB.

129
00:09:15,540 --> 00:09:21,080
Instead, I'll call that new pupi new postgrads repo function that I made in my DB Repo Golfo.

130
00:09:21,330 --> 00:09:23,460
So I call DB Repo.

131
00:09:26,890 --> 00:09:36,910
Debbie Repo new postcrisis repo, and it requires our connection, which is Debe, and it requires our

132
00:09:37,330 --> 00:09:37,790
config.

133
00:09:37,870 --> 00:09:38,870
So put that in there as well.

134
00:09:39,130 --> 00:09:41,080
Probably don't need up config there, but it's not going to hurt.

135
00:09:41,710 --> 00:09:44,760
It's not DIVI, it's DB DOT sequel there.

136
00:09:45,430 --> 00:09:46,850
Now that matches the correct type.

137
00:09:47,560 --> 00:09:53,080
So what I've done here, when I call New Repo, I'm going to pass it my app config, which is a pointer

138
00:09:53,080 --> 00:09:54,280
to configure config.

139
00:09:54,280 --> 00:09:58,900
I'm going to pass it in my database connection pool, which is a pointer to Driver DB which holds my

140
00:09:58,900 --> 00:10:00,010
database connection pool.

141
00:10:00,400 --> 00:10:07,030
And then I populate this repository type up here with all the information I received as parameters and

142
00:10:07,030 --> 00:10:08,910
hand that back as a pointer to repository.

143
00:10:09,400 --> 00:10:11,290
So I've created that.

144
00:10:11,650 --> 00:10:15,460
Now we need to go back to our main function and you'll discover that we have an error.

145
00:10:15,460 --> 00:10:23,760
And the error is I need to pass that second parameter, which is DB, and that should create our repository.

146
00:10:24,100 --> 00:10:29,560
So with that one change, I think about what I've done here, I've now created this handlock, this

147
00:10:29,560 --> 00:10:31,450
repo that I'm going to use in my handlers.

148
00:10:31,900 --> 00:10:37,120
We have our application config available to all of our handlers and we can put whatever we want in there.

149
00:10:37,600 --> 00:10:38,830
And we've also passed it.

150
00:10:38,830 --> 00:10:43,920
This DB, which is not a database connection pool tied to a specific database.

151
00:10:44,230 --> 00:10:46,120
Instead, it's a pointer to a driver.

152
00:10:46,330 --> 00:10:50,050
And that driver can right now only handle postgrads.

153
00:10:50,290 --> 00:10:58,480
But I can very easily, just by creating a new instance, a new a new function instead of new my new

154
00:10:58,480 --> 00:11:04,970
postgrads, I can create a new MySQL function so I can have this repository for the handlers, hold

155
00:11:04,990 --> 00:11:13,080
a repository that's of a type for Postgrads or MySQL or Oracle of Mongo DB or whatever I want.

156
00:11:13,330 --> 00:11:15,130
Now let's see how this is all going to work.

157
00:11:16,150 --> 00:11:22,270
You recall a little while ago, first of all, let's make sure this runs and run my application.

158
00:11:23,660 --> 00:11:24,610
Okay, compile.

159
00:11:24,610 --> 00:11:25,090
That's good.

160
00:11:25,240 --> 00:11:30,910
I'm going to put one more log in there just to say that I actually connected to the database.

161
00:11:30,910 --> 00:11:35,830
So log print line connected to database.

162
00:11:36,820 --> 00:11:37,150
All right.

163
00:11:37,160 --> 00:11:44,140
Again, just to get some feedback, so I'll clear the screen and run it again and everything past.

164
00:11:44,140 --> 00:11:44,490
Good.

165
00:11:44,530 --> 00:11:45,000
All right.

166
00:11:45,670 --> 00:11:48,610
So now that I've done this, how can I use it?

167
00:11:48,610 --> 00:11:56,560
How can I put some methods or some functions available to this repository for the database that I've

168
00:11:56,560 --> 00:11:57,810
created and passed right here?

169
00:11:58,150 --> 00:12:00,070
How can I actually use some functions in there?

170
00:12:00,100 --> 00:12:04,690
Well, you may recall we have this interface that's of type database.

171
00:12:04,690 --> 00:12:06,580
It's a type database repo.

172
00:12:06,580 --> 00:12:07,450
That's an interface.

173
00:12:07,720 --> 00:12:14,140
I'm going to create another file inside of the DB repo folder and I'm going to call it Postcrisis Dargo.

174
00:12:14,350 --> 00:12:20,140
And here is where I will put any functions that I want to be available to that interface.

175
00:12:20,140 --> 00:12:25,360
So I'll create a function, I'll call it, and it has to have a receiver and I'll call the receiver

176
00:12:25,570 --> 00:12:26,110
for model.

177
00:12:26,350 --> 00:12:32,140
And it's a type postgrads DB repo which links it to my postgrads repository and I'll call it all users.

178
00:12:32,920 --> 00:12:36,150
And it's right now it's not going to take any parameters and it's just going to return a bool.

179
00:12:37,750 --> 00:12:38,650
This is going to change.

180
00:12:38,650 --> 00:12:40,960
This is just to show you how it works and I'll just have this return.

181
00:12:40,960 --> 00:12:41,290
True.

182
00:12:42,460 --> 00:12:49,120
So I've defined this all users function that has a receiver of postgrads DB repo.

183
00:12:49,130 --> 00:12:54,910
So what I'm going to do is copy the function name and its return type and I'll paste it right into my

184
00:12:54,910 --> 00:12:55,630
repository.

185
00:12:56,620 --> 00:13:02,830
Now that I've done that, now that this function is listed in the interface and the function exists

186
00:13:03,280 --> 00:13:10,090
with a receiver of postgrads DB repo and the function exists exists exactly as it's defined in the interface.

187
00:13:10,300 --> 00:13:15,370
Now I can go to my handlers and I can choose any handler, say, the home function, for example.

188
00:13:16,000 --> 00:13:22,690
And on that repository, I don't have access to the DB and I can call all users.

189
00:13:23,380 --> 00:13:30,760
And that is really, really cool and very useful because all I have to do what I want to add new functionality

190
00:13:30,760 --> 00:13:35,170
to that, be a member of the repository.

191
00:13:35,530 --> 00:13:37,210
I'll get rid of this because we're not going to use it here.

192
00:13:38,170 --> 00:13:45,340
All I have to do is defined my function in here with this receiver and add it to the repository.

193
00:13:45,490 --> 00:13:50,230
And I have access to all of my database functions which will be writing in the in the coming lectures.

194
00:13:50,560 --> 00:13:57,670
I have access to all of them right in my handlers just because I created that repository that holds

195
00:13:57,670 --> 00:13:58,900
a database repository.

196
00:13:59,530 --> 00:13:59,980
All right.

197
00:14:00,190 --> 00:14:05,140
So in the next few lectures, we'll start thinking about what we need to be able to do in our database,

198
00:14:05,290 --> 00:14:08,860
and we'll write some functions and then we'll update our handlers to use those functions.

199
00:14:09,070 --> 00:14:09,820
We're getting closer.
