Check out a free preview of the full Build a Fullstack App from Scratch (feat Next.js) course:
The "Seeding Playlist Data" Lesson is part of the full, Build a Fullstack App from Scratch (feat Next.js) course featured in this preview video. Here's what you'd learn in this lesson:

Scott live codes the seed script for new playlist arrays to populate the playlist data in the database. This seed script will also connect the playlists to a user.

Get Unlimited Access Now

Transcript from the "Seeding Playlist Data" Lesson

[00:00:00]
>> So far all we've done is with some front end stuff, and now we're working on database stuff, and then eventually we'll get to the API. So, haven't touched that part yet, I'll let you know. And then as far as some of the errors people were having with the C-script, just make sure you have your C TS file inside the Prisma folder.

[00:00:20] And that you successfully created this Prisma command in your package JSON, which should be in your notes file, and that you run the MPX Prisma dBc command. And you should be good to go as far as seeding. And yeah, so what I'll do is I'll continue on and I'll continue to push this up to GitHub.

[00:00:39] So if you ever fall back and you just feel you can't catch up, no worries, you can pull down the steps from GitHub that I'm putting up every single time and catch up. If there's any questions, as always just ask. Before I start anyone here have any questions on either side that we've been doing?

[00:01:00] Okay, all right, and the last thing I want to do now that we have this user is I want to give this user some playlists and I want those playlists to have songs. So let's do that, so the first thing is I need to get all the songs.

[00:01:14] So I'll do that by saying const, let me, I hate writing at the bottom of a file. So, let me bring this up, here we go, const songs. That's gonna equals prisma.song.findmany. And I just want all the songs, so I'm gonna get all the songs. So, my filter is just gonna be an empty object.

[00:01:39] So give me all the songs in the database. And basically it's gonna add all the songs in a database, which I think is seven or eight at this point. I don't know how many songs I put in there, actually took me a long time to go find those songs and download them.

[00:01:52] I wish I would add it more [LAUGH] it actually took me a long time just to get those eight royalty free songs that didn't sound really bad. Okay, so we got songs and now I want to await promise.all, and the reason I'm doing promise.all because, I don't know if anyone caught this but you can actually do prisma.some.entity.create many.

[00:02:14] So I could just create many and create all the playlists at once versus doing promise.all and creating them one at a time. But if you do that, you don't get it from my experience and I just couldn't get past this. You can't do a nested create whereas you're creating more than one thing at a time, so in the case of an artist we created an artist and each artist created songs.

[00:02:37] We couldn't do that if we did a create many, so we couldn't create many artists, that also created many songs. That didn't work for me, every time I tried to do that will break. So I had to create each artist individually, so they can also create songs at the same time.

[00:02:51] So if you wanna do nested rights I don't think you can do create many. So that's why I'm doing promise.all or I'm creating one at a time and then they all do a nested create. So, promise.all, and what we wanna do on this is I don't know we can create any places we want, let's create ten playlists, right?

[00:03:15] So we'll say we'll create a new array, we'll put 10 in here, we'll say .fill again, doesn't matter where you put in fill one, we're gonna map over that. I don't care about the first argument because that's literally always gonna be the number one, only care about the index, I guess I need some parentheses in here, there we go.

[00:03:40] This is gonna be an async function in the map callback. And then what I'm gonna do is for each one of these, I'm gonna return a prisma.playlists.create, like that. So I create a playlist, and for create, we don't have to put the where clause, we just put the data.

[00:04:09] And the reason I'm doing create an up cert this time is because you can only do up cert if you can query for something unique. But if you go back and look at the playlist schema only thing unique about it is the id, that's it. And I don't have the ids because they haven't been created yet.

[00:04:32] So I actually can't search for anything. So because of that, I might as well just create, so that's why I'm not doing up cert. Whereas with the artists I was able to use the names for the where, and then for the user, I was able to use the email for the where, but for the playlist, there is nothing unique about it except for the id which I don't have.

[00:04:50] So therefore I cannot do a query to up cert it, it won't find anything. So what's the point of doing up cert, it's always gonna create the model just create. So, that's what I'm doing, I'm gonna change this back to create here. So for each playlist I wanna create, when you do a create in Prisma, you always put the data, so this is what you're passing and the for this, this is where the autocomplete comes in, it's pretty cool.

[00:05:13] You can see that, we'll first of all, it's fixed that. And then if I go here, it's got all these types that were generated for me, which is really cool. But if you start typing then you'll see, so we know a playlist has a name. So you can see it says name string, so I'm gonna say that's gonna be, the name is gonna be, let's call it playlist number and then i+1, so it's not starting with 0.

[00:05:40] So that'd be the playlist's name. And then what else does a playlist have, a playlist has songs and it has a user and a user id. So, what we'll do is we'll say user id. And that's gonna be this user, that id, like that. So, we got that, but it actually, I think if you do it this way, I'm not sure if Prisma will make the connection between the user or it'll just store the id.

[00:06:13] So what I've been doing, this is what I've always done is I use this other thing well, I'll say, so I'm saying user id, I'll say user. And then I'll come down here and I'll say connect that I wanna connect the user whose id is user.id. So I've been doing that to make sure that Prisma connects the user with this id to the user on this playlist.

[00:06:38] The inverse might also work, but I've always done it this way. That's what their Doc's recommend, so I've always done it this way. But if someone wants to run user id equals user.id and see that works, let me know. But I've always done it this way, I've always done the connect, there also this other one is called connect or create, which is really cool.

[00:06:58] So, you can make a user, you can connect the user with this id but if it didn't exist, also make a user and put the user stuff here too. It gets really crazy with Prisma, I'm like, wow, this is really, you know how had this is to do without Prismas?

[00:07:10] It's incredibly difficult to do this, so yeah, really cool. So we got the user there, and then we got the songs. We're also gonna do the same thing with the songs. Because we all ready created the songs, so we actually do have to do connect, not connect or create.

[00:07:27] We'll say connect, and we want to connect all these songs, so I'll say songs.map. And for each song, I'm gonna just put their ids, so I'll just say, id is song.id. So this is saying, create ten playlists whose names are playlist whatever number that have a user whose id is this one, connect those two together.

[00:07:54] And then it has an array of songs whose values are an array of the song ids, connect those. So now they're all connected, they have a relationship in the database now which is really cool, I'm trying to think what the SQL query for this will be? It would be insane, it would probably take someone a day to write the SQL query for this, I think that would pretty much all we need to do this.

[00:08:18] So, I'm gonna save it and I'm going to run this C-script again, and we'll stop the studio, run the C-script to see if we broke anything. Everything looks good, I'm gonna run the studio one more time. I'm gonna go into our playlist, looks like we have ten playlists.

[00:08:42] That's really cool, so that seem to work. And then you can see that they're connected to this user. And yeah, seems to work, let's connects to the user, then I should be able to go the other way around and go to a user and see if they have any playlists.

[00:08:58] Yeah, it looks they have ten playlists, so that also works, pretty cool.