Current section: Seeding Data 4 exercises

Creating and Managing Data with Prisma Seed Scripts


00:00 Our first step is to make it so that Prisma can actually run our seed script as part of the seed command and the things that it generates and things. So we're going to go down here to the bottom, and we're going to add a section in here for Prisma,

00:13 and we'll say seed, and we're going to say npxtsx prisma seed.ts. And with that now, we should be able to run npx prisma db seed, and that will execute our seed script. And the seeding command has been executed. Awesome.

00:31 So our seed script is not awesome right now, though, because we've got our users are or we're expecting to have users in there. We're expecting to have a note in there. Definitely not something we should really expect. And so we're going to actually delete the users.

00:47 And because of the cascading that we have, all the notes that the users own will be deleted and all the images that they have and the images that the notes have will also be deleted. So all we have to do is say delete all my users and all of our data goes away. So that's great.

01:02 So now we're going to make a new user. We'll call it Cody and we'll await user create. And we're going to provide the data is Cody at KCD dot dev. And then the username. Come on. We'll just borrow this.

01:17 Stick it up there. There we go. And then we've got our user and that's it. We created Cody. OK, now we don't need the note anymore because we're going to create a new note.

01:29 There is no note anymore in there anyway. And so instead of finding the first note, we're going to create a new note using this update. So instead of update, we're going to swap it for create. And now we don't need a where clause because we're creating something new.

01:45 And we'll say here we're going to provide an ID. The reason we're going to provide an ID. Normally you just let it generate an ID for you, but we're going to provide an ID because that way it would be consistent every time. So we'll always be able to go to that note even after receiving the database, which is very, very handy.

02:04 So we have the ID. We have the title. We have some content. And then the owner is Cody. And that will take care of that. It will also create these images as well. So we've got this nested stuff going on. We're going to dive deeper into that later.

02:18 And so with that now, I should be able to run the seed script. So it's actually really quick. We're going to take a look at. Oh, yeah, we're in trouble here because we've just messed everything up. So let's just run the seed script and everything will be much better. Oh, actually, it's because I wasn't running the studio.

02:34 There we go. OK, so now we've got our user and user has a note. We go over to the notes. We've got that note and we've got those two images. And if we run the seed script again, then it will delete that user and it will create a new one.

02:53 But you'll notice the idea is unchanged and that makes it a lot easier to like just I'm on this note. I'm always on this note. I don't have to worry about navigating to whatever the new notes ID is, which is nice. So let's talk about what we did. We've got our Prisma schema right here or Prisma config.

03:10 And that is got a seed property so that whenever we run, there are actually several commands that will end up running the seed scripts. The like migrate command, the DB push, various other commands will end up running the seed script.

03:26 And of course, DB seed will run the seed script, which is quite nice. And then we updated our seed script to be item potent, which means we just run it over and over and over again. And the same result will come from that.

03:42 So we delete all the users that were in there and then we create a new one and then we create a new note with those images. And that is our first look at a seed script.