A second version of this course released using Next 13+, but this course is great if you want to see another example fullstack project. We now recommend you take the Build a Fullstack App with Next.js, v2 course.
Transcript from the "Data Modeling" Lesson
>> All right, the next thing is is let's look at our design, see if we can kinda figure out this model. I think there's a name for this, I said like Domain Driven Development or something like that. But I look at the design, and I figure out what the data model I need based on what information is on the screen.
[00:00:14] And that kinda gives you the obvious data, and then there's this secondary data, things that don't show up on the screen that you need for querying purposes or for information purposes, analytics purposes, that stuff comes later. But just by looking at this screen, you can kinda guess what are some of the entities and some of the fields of those entities that we'll need.
[00:00:36] So we already talked about a user, you can see right now our user needs a name. I don't have a name field for our user, I have email and I have password, but how am I gonna put the name here then. So I can tell you right now that we gotta add a name for the user, so we can save that somewhere, maybe a first name, last name, or maybe just the name, but we got to add that.
[00:00:52] Looking at this design here, what are some other entities that you could think of that we would need to make?
>> Artist, yeah, we need to make artist. What else?
>> Playlist, yep. Anything else?
>> Albums, yep.
>> And a song. Yeah, absolutely. I would say those are the four minimum ones that we need. You could probably get into more detail about other different things, but yeah, at minimum, I think we need those four things, song, playlist, album, artist. I think yeah, we kinda need those.
[00:01:31] So let's do that and we'll figure it out from there. Okay, let's start with the song. So we'll say model Song. I'm just gonna copy these three cuz everyone's gonna have these three. They're all gonna have an ID and a created at and updated at, and then a song has a name that is gonna be a string.
[00:01:55] It's not gonna be unique because, I guess, I mean I've heard songs with the same names before, so that would just not be fair. I guess you could get crazy and have it to where a song plus an artist combination is unique, but then artists might have the same song.
[00:02:10] Or maybe you can get to a song plus an artist plus an album has a unique name, but you're just getting crazy at that point, just let them have the same names. [LAUGH] It's okay. I mean how many times you run into that when you're searching for music and you type in a name of a song and you think it's the song but it's a completely different song and you're like, that song was a cover of this other song.
[00:02:30] That's when you realize that's not an original song anymore. That's happened to me so many times. I think everything else on the song is gonna be relations. Let's start building some of those relations. So the first one is gonna be an artist cuz the song belongs to an artist, so we'll do that.
[00:02:45] And again it's gonna have all three of these. And then, so song can belong to one artist and an artist can have many songs, so it's a one to many relationship. So the way that we wanna do that is we can come down here and we can add it on either side and then Prismo will try to complete it for us.
[00:03:06] So that's really cool. So I can say, this thing has an artist, that I can say it's type is Artist, like that, and if I hit Save, you can see it just did all this stuff. And then what it did was it created a relation field and it's going to map to the artist ID, so that's how it knows what artists to go to.
[00:03:30] That's an integer. And it's referencing the ID field on the artist which is here. And then on the artist side, it went and made a song field that is of type array of songs, which is pretty cool. Only thing I will do is just lowercase is field to be a lowercase s for a song and I'm gonna pluralize it to say songs.
[00:03:51] There we go.