
Lesson Description
The "Setup Postgres Connection" Lesson is part of the full, Build a Fullstack App with Vanilla JS and Go course featured in this preview video. Here's what you'd learn in this lesson:
Maximiliano demonstrates how to store the connection string, retrieve the connection string, and open the database. He also demonstrates how to handle the naming mismatch between Go properties and the database field names by adding metadata to the models.
Transcript from the "Setup Postgres Connection" Lesson
[00:00:00]
>> Maximiliano Firtman: Well, now it's time to create the implementation for this. And for that, we will need the connection string, so I'm going to create a dom.env file. So in the root folder, I will create a .env file from environment, and we're going to create a DATABASE_URL string. And I'm going to paste the connection string of my database from the same source.
[00:00:31]
Remember, this is not actually a Go design pattern. Go doesn't read .env files, but we are going to use a library, very small library, that will do that for us. Actually, it's opening the file, parsing the file, and reading the arguments. Okay, does it make sense? We will use this file later also to add more secret information.
[00:01:00]
Have in mind that it depends on where are you deploying this app. The system might have different ways to write your env files, but you will have to also publish the env file apart from the Go binary. But remember when we are deploying this, and at the end of the workshop, we will see what to deploy.
[00:01:25]
We are deploying actually the binary, not the .go files. Well, you need to deploy the binary, the public folder, and the .env. Okay, not the rest of the Go files. So now we need to read this file, where? In main.go. Somewhere, so let's say this is the Log Initializer, this is the Movie Handler Initializer.
[00:01:55]
Now we're going to add here somewhere the Environmental Variables. So to do that, we are going to use go,
>> Maximiliano Firtman: If you remember from go.mod, we have the godotenv library. So we're going to use that library, okay, godotenv. It's a library that we have already added as a dependency in our project.
[00:02:25]
So I'm going to say godotenv. If I press Enter or Return, it will automatically add me the import, the right import, okay, and we're going to say load. This return a possible error, so we're going to follow the pattern of putting in an if, creating a variable for it.
[00:02:49]
Semicolon and asking if we do have an error.
>> Maximiliano Firtman: Something like this, it's load with capital L. And if it's an error, well, I need to log this.
>> Maximiliano Firtman: That we do have a problem with this, so we need to say log print F or Fatal. No .env file was available.
[00:03:23]
So actually, we don't have the string for the database, we don't have other secrets, so we should stop everything right now.
>> Maximiliano Firtman: Okay, does it makes sense? So how to connect to the database.
>> Maximiliano Firtman: Well, now, we first need the connection string, so what we're going to connect to the database.
[00:03:49]
We need the connection string. So dbConnStr or ConnectionString, however you wanna call this. So now we are going to talk with the OS and call Getenv When you say, why to the OS? We are using a library for this. Well, actually, this library is populating the OS environmental variables,
>> Maximiliano Firtman: Just for this execution.
[00:04:19]
This is to make it more compatible with the standard Go. Does it make sense? os.Getenv will try the environmental variables of your current operating system. In this case, if we first load a .env file, that file will replace or will actually be merged with the OS environmental variables from that moment for our code and only for that code.
[00:04:51]
So if for some reason it's empty, we have a problem. So we also need to make a fatal load saying, hey, we can't initialize. So DATABASE_URL not set, or something like that. And if that's the case, now we are ready to open the database of type postgres with that connection string.
[00:05:23]
And that returns database in a possible error object. We will check again one more time if we do have an error. If it's not nil, we'll also go into do a fatal error, like failed to connect to the database. We could pass also a value, so instead of Fatal, I can use Fatalf and pass as the value, the current error.
[00:05:55]
>> Maximiliano Firtman: Okay, does it make sense? And Go is complaining because I'm not using the database yet. So something quick that I can use is I can defer db.Cose, so closing the database right now. Remember defer will defer that for later at the end. So we remember to close the database as soon as we are opening it.
[00:06:18]
So we have the open and we have the close. But remember, because we do have defer, it's not closing the database right now. Okay, so that's something that Go has to defer the execution to the end. Well, now we need to make queries somehow. And for that, we are still missing one part that is creating the current implementation of this.
[00:06:40]
But before doing that, one more step, I promise you that it will work at the end. Remember our models, we have movie, genre, actor. If we remember our structure, I'm talking about the sequel structure. For example, let's talk about the actor. Remember this ID and FirstName? This is using title case, so capital F, capital N.
[00:07:06]
Why capital F? Because I wanna make it public, okay, that's a Go thing. But now in my database, the actor, it's actually id, lowercase, and first_name. Everything lowercase with underscore, so this is a snake case. Okay, someone thought that this looks like a snake for some reason. This, it's like a snake, whatever.
[00:07:33]
So we have a problem, I mean, the Go properties are not matching the table's field names. To solve the problem, we can add metadata to our model, okay? So to make things easier for you, under README, we already have that here. So we can just copy and paste the new model, so replace the models.
[00:08:01]
So is something like that. So this is the type movie where we specify. Okay, you know what? Here, even if it says json, okay, it's for the database anyway, because the database provider will use that metadata, the JSON metadata, okay? So I just need to replace my current movie with this one.
[00:08:24]
What's the only difference? Is that now it has metadata with the real field names in the database. That will also be the name here when we convert this to JSON.
>> Maximiliano Firtman: Okay, makes sense? So that's the movie. I need to do the same for the other two as well, which is actually pretty simple.
[00:08:52]
So for example, if you copy one on actor, this is json: id, we introduce it manually, first_name, last_name, image_url. Every time you save, in this case, VS Code is pretty fine, your tabs. In genre, we have id and name.
>> Maximiliano Firtman: Okay, so they should match the field names in the database.
[00:09:31]
So we have actor, genre, and movie now with the right metadata.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops