Go & Vanilla JS: Fullstack Without Frameworks

Adding More Database Queries

Maximiliano Firtman
Independent Consultant
Go & Vanilla JS: Fullstack Without Frameworks

Lesson Description

The "Adding More Database Queries" Lesson is part of the full, Go & Vanilla JS: Fullstack Without Frameworks course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano adds more database queries, such as getting a movie by ID, searching movies by name with optional filters, getting all genres, and fetching movie relations like actors and keywords. He also tests the logger functionality by logging an error message.

Preview
Close

Transcript from the "Adding More Database Queries" Lesson

[00:00:00]
>> Maximiliano Firtman: We could finish the MovieRepository with the rest of the services before start working with HTML. So now that we do have some services up and running, we are ready to start working in the client, actually finally seeing something on the screen. Go so far everything was actually really abstract, okay?

[00:00:23]
>> Maximiliano Firtman: So to finish a repository, we can go and just copy and paste the rest of the queries that we have, so it's in the readme basics. So if you get here, basis, finish the MovieRepository. I can just copy and paste everything, or just in case, I will just get what's left.

[00:00:51]
So I have GetMovieByID, search movies by name, and get all Genre. Also I have fetch movie relations, so let's copy everything. Also I may need to adjust a little bit some variables because maybe I spell something different so we can solve that. So MovieRepository. And I will paste the rest of the services.

[00:01:21]
So let's try to analyze it. GetMovieByID, is actually pretty straightforward. It's not a big deal. Here, it's important to use an argument and not concatenate what's coming from the client, okay? This is really important. It's just one movie, so it's just scanning one movie. Doing something else. Something that is new here is that it's also fetching other related data, okay?

[00:01:47]
And we will see what is that. So because now we don't just need the data from the movie, when we get the details we need all the details. That's including the actors, the full cast or the keywords, and that needs to make more queries to other tables as well.

[00:02:07]
That's what we're doing. We don't need that when we are getting the list, we are just getting the basic metadata for movies. So search movies, my name this has more arguments than the one that I used. And also receiving an order, so it can be ordered by score, by name, by date, or the default one by popularity, okay?

[00:02:30]
And you can see I'm defining what's the SQL order that I should use later. And also I'm receiving a genre for filtering. The idea is that I can search for unknown Batman movies, but only I want comedy Batman movies. Maybe I will get the one from the 60s that was kind of a comedy movie, right?

[00:02:52]
So that's idea. So I'm getting that, and why do I have an asterisk on an integer? And if I don't give you any extra info, why do you think there is an asterisk on an integer?
>> Maximiliano Firtman: Anyone?
>> Speaker 2: It's nullable.
>> Maximiliano Firtman: It's nullable, exactly, that means it's optional.

[00:03:18]
So when you see that in the signature, it means maybe it's optional, so maybe it's not there. So that's why here I'm actually verifying if I do have it, okay? And based on that, I'm adding a sub query, okay? Checking how many movie genre records do I have for that movie, and that genre, I need to be exactly one, because, remember, it's end to end.

[00:03:46]
The query is actually using ILike, that in case you haven't played with Postgres before, ILike is similar to like that's available in NSQL, but case insensitive, okay? So that's ILike, case insensitive. So this is the same code. GetAllGenre, it's just getting all the genres, nothing important. And fetchMovieRelation, it's trying to get actors, keywords for a movie, okay?

[00:04:20]
Making all the other esthetics and actually filling the models. So creating the radio factor, or the slice of factor, actually, the slice of genre, the slice of keywords, okay? And attaching that to the movie. To be honest, you can make everything here in one large SQL query with Postgres.

[00:04:41]
So Postgres lets you return arrays with subqueries. So it's possible to make a large query and get everything from a movie, including data from other tables or end-to-end relations. The problem is that it's a huge query, it's faster to execute, but also more difficult to understand. So we are losing clean code at that point, so that's why I kept it like this, okay?

[00:05:15]
So this is a repository, the only person that I have to actually make everything work. Work right now is that maybe the interfaces, I need to uncomment this, like that, so now, I think everything is working. So before using these new services I also need to register those handlers, okay?

[00:05:43]
So I need to register those handlers. So before finishing the handlers, something that we haven't tested yet is the logger. So is the logger working? So something quick that we can do is remember, here we are initializing the logger. So at any time we can say here for example, take that log instance and try to log an error.

[00:06:09]
So, we can log a message like hello from the error system, and we need to pass an error actually we can say nill for now. So if I say this, I don't need to do anything like I don't need to run anything, because that code will be executed through the main call, right?

[00:06:31]
Because initialized logger is being executed through the main call. So now if I go to movie.log, I can see that message there, hello from the arrow system and nil, because we didn't pass any error, okay? So that object is the one that we are passing through.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now