Lesson Description
The "Database Seed Script" Lesson is part of the full, Build a Fullstack Next.js App, v4 course featured in this preview video. Here's what you'd learn in this lesson:
Brian demonstrates writing a seed script, how to add it to the package.json file, inserting rows into the database, associating users with articles, and ensuring the primary keys get reset. He also discusses the installation of TSX to run TypeScript files as node files without compilation.
Transcript from the "Database Seed Script" Lesson
[00:00:00]
>> Brian Holt: We're going to write a seed script here really quick, but let's go add this to our package.json really quick. So, we're going to have a few scripts, we're going to have a DB generate. And this is going to be Drizzle Kit. Uh, generate, we're going to have a DB migrate. And that's going to be Drizzle Kit. Uh, migrate. And then we're going to have one's called DB seed, uh, and that's going to be TSX.
[00:00:38]
Um, source slash seed.ts. Um, also, we're going to have to install TSX just lets you run TypeScript files as Node files, and you don't have to like do all the compilation. So you just install npm i -D TSX. Alright, so now I have TSX. And I have all these TVs. I wrote a little seed, uh, file for you here, so I have a link to it here somewhere.
[00:01:11]
So yeah, just grab the raw file here. And here in DB. Called the seed.ts. So. Oh yeah, we also need to export here in index status as well. Um, export. Const. SQL equals SQL. Oh, that's weird. Or exporters. Or rather you just put export here. I don't know what I'm doing export. Like that. OK. Reason being is that seed file needs it.
[00:02:18]
So, what this is going to do is it's going to start a DB seed like seed is just like putting a bunch of rows into the database, um, so that you can have like some real data to work on. It's going to delete everything that's already in the database. Um, they have something called Drizzle reset, but I found that it's not resetting the primary key.
[00:02:44]
Uh, which is like this restart identity cascade. Um, so I did not use that for that reason. This will then go find real users that you have existing in your database and it'll associate those to articles. So the one thing that this seed file will not work, unless you have at least one user in your database. So please make sure you have at least one, if you've already signed up as yourself, this should work just fine.
[00:03:07]
And then here you can see DB which we just imported from our index, right? Um, this is how you quote it, select from, order by, like it's all very SQL-ish. And then it uses uh Drizzle seed, which just like, you say like, uh, here's some values to use like author ID it is values from array, these values I got from the users table.
[00:03:38]
Um, you can give it however many things you want. Uh, title, it's just going to be like Lorem Ipsum. Um, this one will say everything will be true. This will create random timestamps, and the same thing here with slugs, it's just going to be some random string. OK, then I'll insert all of that, um, it does some of this to make sure that the primary keys get reset.
[00:04:04]
And that's it. So right now, everything will be offered by me because I only have one user in my database. But let's see if that works. Npm run DB seed. OK. Now, if we go back over to our Neon console and we refresh here. You should see here that we have. 25. Uh, items in our database. So, one thing I was going to show you is that you can see this user sync, sync thing, this will show me like, it's referring to this user in the other database, you can actually click in there to see what it's actually referencing with the foreign key, which I thought was pretty cool.
Learn Straight from the Experts Who Shape the Modern Web
- 250+In-depth Courses
- Industry Leading Experts
- 24Learning Paths
- Live Interactive Workshops