Transcript from the "Transitioning from Schemas to Models" Lesson
>> Scott Moss: So for every single resource that we want to expose through our API, in this case, we have a user, a list, an item, we need to create a schema and a model for. But in a REST API, you might have resources that are private that aren't exposed to the REST API.
[00:00:13] So that means you might have database models that don't have resources attached to the routes. Like you have some internal private analytics thing that you're tracking or something completely different that you don't wanna expose to the REST API. That doesn't mean you have to make a resource for it.
[00:00:25] You don't have to. You can do whatever you want. So just cuz you have routes doesn't mean you have models to go with them. They don't always go hand in hand. But according to REST, the resources line up to something that you're trying to do in the database.
[00:00:38] Should they be called the same? Maybe you should call them the same to save people's sanity, but they don't have to be. You can do whatever you want. They could be virtual. They could be something that calls something else, but on the route, they're called a user. But on somewhere else, they're called person.
[00:00:51] They could be anything you want, but that's usually how it lines up. A resource lines up with a model, and a model is created from a schema.
>> Scott Moss: So when we talk about transitioning from schemas to the models, basically, like I said, schemas are gonna be instructions for the model.
[00:01:09] So you can't create a model without creating a schema first. Schemas basically hold the instructions for the models. Things like validations, the names of the fields, indexes, hooks. Pretty much anything a database will support, it goes through to the schema. When you have to do a migration, you gotta change the schema and stuff like that.
[00:01:28] So the schema's everything. So if we go back and check out this user model here. Actually, I'll just go through, let me just delete this stuff and we'll just make some new one here. This thing's moving so fast. Okay, all right. So we'll just make a new schema here for the user.
[00:01:48] So for instance, if I wanted this user to have a first name, I can give it firstName, and I can give it a type string. And all I have to do is just pass this object to mongoose.Schema, which basically is creating a new schema, so it's gonna return a new schema.
[00:02:05] So I'm saying, hey, I want this schema to have a first name and its type is string, right? I can also give it a last name, but this time I want this to be required. For some reason, the last name has to be required. I don't know, that's just how our application works.
[00:02:20] So in this case, instead of giving it a type, I give it an object like this. And then I could say its type is also a string, but it has a required validation on it, like this. All right, so this is making it required. So as you can see I'm building a schema that I'm then going to use to create a model.
[00:02:37] And then that model is gonna help us inside of our controllers that we'll build eventually that attach to the resources that we expose over at REST. So it all combines. It all starts with a schema. So you design your data with the schemas. You convert those to models.
[00:02:53] You have your controllers use the models to query and perform CRUD on the models. And then the resources activate the controllers, and there you go. You have a REST API.
>> Scott Moss: So as you see, you can continue building on the schema. There's a lot of different things you can do here.
[00:03:10] So I already have examples of all the different possibilities this schema could do with Mongoose. Like I said, there is another course where we go into more detail about it. But for what you're gonna be doing, there's definitely gonna be enough here for you to understand and see what you're doing.
[00:03:24] If you check out the user schema, like I said, you'll see more here. The list schema also has some other stuff here as well, but there's some really good examples. I don't wanna go too deep or into developing schemas and stuff with Mongo or Mongoose.