Check out a free preview of the full API Design in Node.js, v4 course

The "Designing a Schema" Lesson is part of the full, API Design in Node.js, v4 course featured in this preview video. Here's what you'd learn in this lesson:

Scott creates a schema for the User model. Each user has an id, username, password, and a createdAt field. The id field uses a UUID, a unique string that can be generated by Prisma when a user is created.

Preview
Close

Transcript from the "Designing a Schema" Lesson

[00:00:00]
>> Okay, so next we're just gonna start thinking about the models, and then we're gonna start making the models and creating the schema. So let's do that. Okay, so like I said, we're not building a UI for this API in this course, but having a design I think is really helpful for designing your models.

[00:00:12]
Knowing what data needs to be the UI gives you hints on what data needs to be recorded in a DB. So we'll be using this design here. So if you click this link, it will take you to this It's a pretty cool app that's some really cool designer made for Figma it's like a template.

[00:00:30]
And it's a change log app, and we can look at this I'm gonna click on Preview and here it is, here's what the app looks like. It's a really nice app. So I should just do this for the side project. And we're gonna make a schema for this app.

[00:00:44]
And we're gonna look at the design to determine what are the models that we need to make and this is actually what I do in reality. It's really hard for me, I know that's like basic stuff that I have to make first schema. But if a designer already like designed to thing, now I really know what to make because I'm basing my designed off of what information I gotta put on the screen.

[00:01:03]
So what we wanna do is we wanna look at this and kinda get an idea of what models we need to make and different things like that, and then we'll make them. Does that make sense? So just looking at this, anybody wanna take a stab at. Some information we need to record in a database based off what's on the screen?

[00:01:27]
No wrong answers.
>> You've a roadmap table.
>> Roadmap, I like that.
>> Projects, tasks.
>> Projects, tasks. Yeah, those make sense. Yes.
>> User.
>> Yeah, you can't do anything without a user. Got to have a user. Yep, anything else?
>> Features.
>> Features I like features?

[00:01:52]
Yeah, that's probably the thing. Yeah, I mean, that's basically it. You're just taking note of this and seeing all the different fields that these models would have and things like that. So. Let's get to it. So first, I'm just gonna introduce you to like the Prisma syntax. So I'm just gonna go into the code, and we're going to just write, let's just do the user schema.

[00:02:16]
So the way this works inside of Prisma if you've ever uses like graph SQL syntax is very similar to that. The team behind Prisma had a graph SQL product before this, so they're heavy in a graph SQL. So this thing is heavily influenced off graph SQLs, syntax, or whatever it's called, which is heavily influenced off JSON.

[00:02:34]
So if you know how to make JavaScript objects, you'll know how to do this. So if I wanna make a user scheme, I can say model user like that. And all users have a what?
>> ID.
>> ID Yeah, All users have an ID, so I can say ID.

[00:02:53]
I can make it an integer if I want to. But I like my IDs to be string. So I'm gonna say ID is a string. And then I'm gonna have to give this. I don't know what they call this in Prisma but always think of directive [LAUGH]. I think it's like an attribute, maybe.

[00:03:12]
So I'm gonna say this is identifying this field as the ID field. And then I can give it a default like this. And I can say uuid. So it's basically gonna default to a uuid. If you don't know what a uuid is, it's basically just like a guaranteed unique string that's in a certain format.

[00:03:38]
So this is guaranteeing that all users have a unique string as an ID. I can do integer and I can tell this thing to auto increment to go up by one, but then you have user one, two, three, four, five. I think you're just setting yourself up for trouble later, in my opinion.

[00:03:53]
So yeah, we have user ID. Is that users are gonna have usernames in this app, so I can say username. And that needs to be unique, so I can say unique. They also don't have a password. Which is a string, okay? And yeah, maybe even like a createdAt.

[00:04:21]
So let's say createdAt, and I think that's a date time, and we can say @default to now. Okay. So now that we have that. That's pretty much how Prisma's schema syntax works, it's not more difficult than this. I mean, yeah, you gotta look up some of the stuff to see what it is, but it's pretty intuitive, and it doesn't really matter what database you choose it's 99% the same no matter the database which I think is refreshing.

[00:05:00]
So this is how we're gonna make our schema. So cool we have our user, any questions on this? On the Prisma, yes.
>> You could define relationship with other table.
>> I knew someone who was working on databases was gonna ask that question. So the question was, how do you define relationships with other tables?

[00:05:17]
We're gonna get there and it's actually pretty cool. And this is why I recommend downloading the extension cuz the extension will actually do that for you. It'll help you define those relationships without you doing anything, which is really cool.
>> Why did you use the UUID?
>> I use the UUID because I don't like numbers as IDs.

[00:05:40]
And Postgres doesn't like Mongo, or it has mechanism built-in other than incrementing numbers for IDs. So you just have to set your own IDs in a SQL based database whereas Mongo like just we'll figure it out for you. So yeah, it was either numbers auto incrementing or using a UUID, so I chose UUID that way I also don't have to manage a system of coming up with globally unique strings myself.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now