Connecting to a database

GetGoing: Introduction to Golang Introduction to API development with Go
5 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
This is a free item
$0.00
د.إ0.00
Kz0.00
ARS$0.00
A$0.00
৳0.00
Лв0.00
Bs0.00
B$0.00
P0.00
CA$0.00
CHF 0.00
CLP$0.00
CN¥0.00
COP$0.00
₡0.00
Kč0.00
DKK kr0.00
RD$0.00
DA0.00
E£0.00
ብር0.00
€0.00
FJ$0.00
£0.00
Q0.00
GY$0.00
HK$0.00
L0.00
Ft0.00
₪0.00
₹0.00
ISK kr0.00
¥0.00
KSh0.00
₩0.00
DH0.00
L0.00
ден0.00
MOP$0.00
MX$0.00
RM0.00
N$0.00
₦0.00
C$0.00
NOK kr0.00
रु0.00
NZ$0.00
S/0.00
K0.00
₱0.00
₨0.00
zł0.00
₲0.00
L0.00
QR0.00
SAR0.00
SEK kr0.00
S$0.00
฿0.00
₺0.00
$U0.00
R0.00
ZK0.00
Already have an account? Log In

Transcript

Hi everyone. In this video, we're going to be learning how we can connect a database with our API. So if you take a look at this requirement specification real quick, we have a create entry, read all entries and delete entries endpoints. So all of these endpoints require us having an access to a database, because all of those entries are going to be persistent in our database. So, if you take a look at our code over here, then you can you can go to the model layer, and in the model layer, create a new function, create a new file called connected go. So our connect.go is going to be containing the code or in fact, our pack the whole package model is going to be containing the code which is used to talk to our model or a persistent database.

So every everything database related goes to the model layer, everything controller, an endpoint related goes to the controller and everything. The responsibility and request later goes to the structure views. So, to open it, just word DBS SQL open, I created a function called Connect, which returns an instance to a reference to a sql.db object. So the syntax is the username, the password at TCP, and I'm running my, my SQL on localhost Port 3306. So before this video, it's a good idea. If you guys download my SQL, and after downloading if you're on Linux, you can simply do a sudo service, my script start is going to start the server.

If you're on Windows or Mac, then simply see the documentation. Then going to main.go can access their DB object right over here, I can say that okay, Morgan dot Connect, which returns their DB object. Now the thing with SQL is, the thing with any database connection is that when you create a connection, you also need graceful shutdown. So whenever our server shuts down, or whenever we press Ctrl C, or Whenever you want to do an exit, it's a good idea if we exit our DB also, so we should close our connection to our database. But can you see one problem right here. So one problem is that we always need to define and define the DB dot close at the end.

So whatever we have hundred lines of code, so for that case, defer keyword is used. So defer means that defer this function call to the end of the scope, which essentially means that when all of those things run, then run this idea towards the end. So when listen and so fields, then this DB clause is gonna run. So what we can do is we can wrap this around our log statement. So this essentially means that our lesson and so returns an error if the error is nil, then do nothing. If there is some error, then simply log it out to fatal and then close the database connection.

So one more thing that we want to do is that after our database connection has been established, I want to print Something I want to say font dot print line connected to the database. Now let's see what happens if you do do a quick Google run, I'm going to CD into my section for To Do List API, and then go down. So in this case, why did fail. So the reason it failed was because main.go requires a my SQL driver. So there is a great open source community around go. So what people have done is they've created drivers around their favorite databases like my SQL and MongoDB.

So driver consistent contains a lot of code, which you had to write if you didn't have those drivers to talk to the database. So I'm going to clean up my terminal and then I'm going to go then now you can see that we have successfully connected to the database. Now I want to add something else for readability. So whenever I start a server, I want to I want this to occur. So I'll do a boot on main.go. It's connected to the to the database, and it's serving right now.

So this is how we connect it to the database. In the next couple of videos, I'm going to be talking about how we can, how we can interact with our database. But before doing so, I'm going to create a global connection. So for the sake of this video, I'm creating a global connection, I will say we are going and this variable is going to be of the type start sql.db. And I'm going to let you guys know the reason real quick. So whenever I connect to the database, I'm just going to say corn is equal to dB and then return my dB.

So notice that I don't have to return corn because I'm storing the differences and I'm doing everything, bypassing everything by reference. So the reason why I'm doing this is that whatever function I write, be able to create function, delete, update, or read function require access to the connection. Right. So what I want, what I want is I want only the model to have access to direct calls to the database. So that is why I have created this cone. Notice that it's a small C, it's not a capital C. So if it were a capital C, then every package has access to it, but it's a small C. So only package model has access to this connection.

So concluding this video with this, and see you guys in the next video where we write database wrapper functions, and where we complete our first route, which is creating an entry. Thank you guys and see you guys in the next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.