So we now have a database with these tables and the constraints defining the relations set up. So this is all ready to go. We now have a good working database and we can move on to the next step, which is actually giving it some data that we can play with in our application.
So back over here, what we're going to do for this is we're actually going to cheat because I don't want you guys to have to type out a whole seed file. So what I'm going to do is create a new file in the Prisma folder called seed.ts. And then I'm just going to paste this in. So let's copy this guy over. Let's see if I can do it. There we go, cool. Copy that guy over, go into the Prisma folder and create a seed.ts file. And then I'll paste this in. I think it copied the whole Notion block, so I just had to take those out. So this is what's going to run when we actually run our seed command. What we're doing is we're importing Prisma Client, which if you didn't see from the output down here, when we ran Prisma Migrate, after it created our database and applied our migration, it also ran the command called Prisma generate, which generates Prisma Client, and that's what you use to query and interact with your database. So we're importing that here, we're instantiating it, and then we're just running a function that clears out our database and then adds some new data using the Prisma Client, so we can see we're creating a user here, prisma.user.create, we're giving it, excuse me, we're giving it a name, and then we are giving it a couple of notes, which will create these notes and automatically set up the relations. So with that in place, what we need to do to actually run this seed script is go into our package.json, and we're going to add a prisma key to it, and within that, we're going to have a seed key, and this is gonna run, ts-node-dev-prisma-seed.ts. So we're just saying when our command that we're gonna run is run, we simply want to run the seed file. And to actually run this, now that that's in package.json, you could do npx prisma db seed. And there we go, we can see our data was seeded and the seed command has been executed. So at this point, what's happened is you've modeled out your data, you have created your database, and then you've performed a migration to actually apply your database schema to the database and you've now seeded your database with the data that you need. So at this point, we're done with the first section and what I'm gonna do is I'm gonna turn it over to you guys to start going through this on your own. I'm gonna give you guys 30 minutes, so I'll be back roughly around 10am PST. So yeah, just about 30 minutes from now. Um, so let's go ahead and jump right into it. I'll pop open the, uh, the second lesson here. And the goal of this is gonna be to set up the GraphQL server. So we have the server folder. We have Prism setup, but we don't actually have a GraphQL server yet. So that's what we're gonna be doing in this section. Um, I also want to point out that if you didn't notice in the first part, I forgot to mention that there are solutions available at the end of each task as well. So if you get stuck, um, and if we sort of talk through it on the Discord and we can't seem to find a solution, uh, for you, that you can also pop this open as sort of a cheat to, uh, see where maybe something went wrong. Um, also from here on out in the workshop, every setup step is going to have instructions on how to get, uh, caught up to the point where we're at. So if you got stuck on a section and then, and we move on, um, you can check out the branch associated with the next section. And it should have a working folder at this starting point. Uh, so instructions for that will be here. Uh, but let's go ahead and start, get, uh, start going on the tasks. Uh, the very first thing that I want us to do is to actually instantiate and export Prisma Client from a module within our server project. That way we can just get that out of the way. We're going to be using it a lot. Uh, so let's go ahead and get that going. Uh, to do this, we're going to create a new folder called source, which is where all of our code is going to live. And we're going to create a file called db.ts. And then we're going to actually instantiate our Prisma Client as we did in the seed file. Uh, and export it so that it's actually in a usable state. So I'll go back to the code. And as the instructions suggested, I'm creating a new folder here called source. And within there, we'll have a eb.ts. And this is simply going to import Prisma Client. It's going to export a const called Prisma. And this const is going to be a new Prisma Client. And then I'm also going to export the Prisma Client type. I think yeah, yeah. Because we will need it later on. And might as well get that out of the way now. So all this is doing is instantiating our Prisma Client and exporting it so that we can use it throughout our application. And that's all of the first part of the step here. So we have instantiated it and exported it and we should be good to move on to the next piece. And that's adding a tool called Pathos to our project. And this tool is what we're going to be using to build our GraphQL schema later on. This is a code-first schema builder. So through our code, we're going to be building our GraphQL types. But the cool thing about this tool is that it comes with a nice plugin for Prisma specifically that gives you type safety based on your Prisma schema within your GraphQL resolvers. If you aren't sure what any of those things mean, we're going to be going through it throughout the workshop so don't worry. But just know that we're adding this tool so that we can actually generate some extra types when we run Prisma Generate that are going to help us as we develop our GraphQL schema. So to get this going, though what we need to do is actually install these. So let's go ahead and copy this over. I think I can just copy that actually and go back to our project. And then I'll paste that in. So this is installing Pothos and the actual plugin that we need to get it to work with Prisma. And once those guys are installed, we're going to head over to our Prisma schema and I'm gonna close everything here so it's nice and clean.
Comments