Loading
Lesson 06
Courses / Quick Start Introduction to Sails
Generating a Model and Controller in Sails

Video Transcript

In this lecture we're going to build our model for the company. So say we have an API that serves information about a company. So a company has a name which is a string. Company also has a founding date, the date of establishment. What is going to be a date type in sales? Now we also have an employee count and that is going to be an integer data type for sales. Alright, so we're going to generate a sales model along with a controller. And then we're going to define the schema for this company type. Okay, this company has a name, a founding date, and an employee count. So sales is going to automatically figure out how to map this guy to the database. In this case we're using the local disk database provided by sales by default. So let's go to the command line and we're going to learn the command to generate a model as well as a controller at the same time. So we're going to say sales, okay, generate API and then you give the name of your resource. For example, we're generating information about a company. So I can just say company here. Doesn't matter if it's lowercase or uppercase for the first letter here. So this guy is going to generate what it's called an API in sales. Basically it's just going to generate a controller file and a model file. So let's take a look at the app structure here. I'm going to my text editor for the project. So let's look at the directory called API. Under API we have the controllers directory and the models directory. So let's look at models here. So you see it created a model file company.js. But knows also that it created a controller for that model. So we have company controller.js under the controllers directory too. So it automatically creates both the model and the controller when you say sales generate API resource name. So with that in mind, let's go to the model file. API slash model slash company dot j s. So we have a model dot exports that exports the whole module. So the whole module is essentially an object. And this object has a property called attributes. Attributes in turn is an object. So here inside attributes, we are going to define each field for this resource. So a company has a name. So what we do is we type name. And then we give an object as a value. Okay and then we say the type. In this case, name is going to be a string. So we write it like so. This is a sales way of writing the schema. Okay. Sales uses the waterline ORM to map this schema to the database. So it's called waterline ORM. So we got the name attribute of type string. Now let's get the other ones. The other ones is founding date and employee count. So let's do it. Founding date is going to be a date, the date of establishment of this company. So the founding date is going to be of type date. Simply like that. Okay. Now let's do the employee count. Employee count. Well the employee count is just going to be an integer. So we can say type integer this way. And just like that, sales is going to know how to map these attributes for the company resource to the database. Okay. So now we got our company model set up. The next step is see if this is actually working. So sales, as we saw before, generates also a controller called company controller. If you click the file api slash controller slash company controller dot js, you see there's a module that exports assigned to. There's an object assigned to the module, but there's nothing inside. Actually there is something, but sales does it behind the scenes. So sales already made a RESTful API for you. You don't see anything here, but by default sales creates a RESTful API. It's RESTful routes using what's called the blueprint API. So that's the topic of the next lecture. We're going to learn how to work with this data using the blueprint API. See you then.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: