Loading
Current section: Querying Data 5 exercises
lesson

Intro to Querying Data

Transcript

00:00 Let's query some data. Finally. Finally, we can pull up the real app and start talking to our database so at first before we do that as we're going to be migrating from our existing in-memory database into our New database that is actually from like the real database

00:18 And so here if I go to Cody's notes and I go to the first one You'll notice it's actually already gone and that's because at I have deleted that note from the in-memory database But it's showing up in here because here I'm loading it from the actual database That's what we're going to get to at the end of this exercise And so we are going to be in kind of a funny place

00:37 For a little while where some of the app is still loading from the in-memory database Some of its loading from the real one and they will not be in sync at all And so yeah You just have to prepare yourself for some weird things as we're migrating some of this stuff the reason that it works at all is because the IDs are going to be the same between in-memory and

00:56 Actual databases, so that's why It's working this way. It's kind of funny. But yeah, that is the funny DB stuff, but we're gonna start querying from the database So here's some SQL to query everything from the user I do not recommend like almost never should you use select star or like just select everything from a particular table

01:16 It's wasteful. It is like surprising when somebody adds a new field now You're selecting something that maybe you shouldn't be. So yeah, pretty much never do select star here. You can select specific items

01:30 From the particular table. So we're saying I want to get these values from this Table of users and I want all of them like every single user and so often you want to filter that down to just Something and so you can use the where clause where the ID is equal to, you know, some string or whatever

01:50 And so you almost like very regularly Are you going to be adding a where clause to your selects because you're not often wanting to get everything now Of course, there are some tables that are like config tables or later on when we get into another workshop We'll talk about permissions. You may want to select all those permissions because there aren't a lot in there But yeah

02:10 Pretty often you're gonna want to where clause and there are a bunch of other clauses That you have available to you as a part of this select Like you can choose how many records come back what order they come in like Just and then you can do nested selects and stuff like that, which we are not going to get that deep in the sequel side of things

02:29 Until later on in the exercise. We'll we'll do a little bit of sequel and it's going to be great. But yeah, you pretty much Sticking with select from and aware and then sometimes order by and limit as well But there there are quite a few closets. It's kind of interesting ORMs

02:46 make bring a sequel into your language of choice and our language of choice, of course is TypeScript and So here we're going to say I want to find unique where the ID is one and select these particular things And then to create that Prisma client. We've already done something similar to that using

03:04 The when we were working in our seed script, but here I wanted to show you an example of something Kind of odd that we're doing here and that is if Global dot Prisma is not defined then we will assign it to new Prisma client the reason that we would do this is if this module gets evaluated multiple times which

03:22 In production that won't happen a module gets evaluated once and that's it But during development, we don't want to have to shut down our server and start it up again every time we make a code change and so what we do is when we make a code change we take the Existing module that was there we kind of throw it away, but that stuff is still living in memory

03:42 And so like we've still got that Prisma client that we made over there and then we evaluate the module again with our changes So we get the latest stuff and so we're going to make a new Prisma client That would not be good because you then you're making a bunch of them. So We have this global dot Prisma so that when this about is evaluated again, this one says oh you already got one

04:02 Let me grab the one from over there so that we don't have that Issue with creating a bunch of Prisma clients. So it allows us to have our Restarts be really fast because we're not actually restarting the server. We're just re-evaluating modules And also allows us to avoid the problems that come with making a bunch of different connections to the Prisma client. So

04:23 that is that the the trick there is whatever is inside this block will not get re-evaluated whenever the Module is re-evaluated Because the global Prisma will be defined at that time So you will have to stop and start your server manually if you want to change anything inside of here now doing this is kind

04:42 Of an annoyance to make things type safe and everything like that So we have a utility called singleton that allows you to do the same sort of thing and it's type safe and it works nicely So you're going to be using that singleton utility and we're also going to be Eagerly connecting to the database Prisma does a lazy connection. We're a long-running server

05:03 We just eagerly connect and so the first request is as fast as it can be And then we're also going to be doing a little bit of stuff with logging as well So plenty of stuff to do to first create the client and then start doing some queries and some nested slacks and stuff like that It's going to be a fun one. So I think you're going to have an awesome time. We'll see you in the exercise