Sails.js: A Look at Models & Controllers

09:41

Last time I provided a brief introduction to an awesome framework that is Sails.js. Here at Couchjumping we can't sing it's praises enough, well at least I can't anyway. This week I'm going to take a closer look into Sails.js, focusing on Models and Controllers which are two crucial concepts needed to understand the framework. So if that sounds somewhat intriguing and you have nothing better to do feel free to keep on reading!


In Sails.js, there are collections of structured data known as models. These models usually represent a corresponding table or collection within a database. A simple way to think of them would be like a template for objects. Models are stored in the api folder of your sails project and can be generated in the root folder of your sails project from the command line. Below is a detailed walk through of the steps involved in creating a model and controller:

  1. In the command line type: 
    • sails new api user 
And that's it! I know very long and complicated, doing this simply creates a user model and controller in our api folder. We still need to take care of a few things before we can use our model. Within a model we can define attributes of the instance we wish to create. These attributes can be anything we want, they just need a data-type (e.g. integer) and possibly some rules (e.g. an email address will need to be unique). Below is an example of a user model created within sails.



So what exactly is happening in this block of code? Good question reader, it's really straight forward. Within "module.exports" we have our "attributes", these are the characteristics of our model. We have an attribute called "name" set with a value of string along with "firstName" and "lastName" also with a data-type of string.  With me so far? Good, this model can be accessed from other various files including controllers, one of which was generated along with the model itself. 
In our api folder is now a file called "UserContoller.js". Controllers are comprised of functions called "actions", these functions are used to carry out some basic logic followed by a response. For example in the image below we create a user based on the model we defined earlier.

So lets go through this line by line and break it down. 
  • We have created an action called simply "create" with the parameters of req (request), res (response), and next. 
  • In this function we select our User model on the first line.
  • On the next line the .create() adapter uses all of the parameters of our User model to create a new instance of the model.
  • In an asynchronous fashion the .then() adapter is used to define a function with a parameter of "newUser" and logs it to the console.
  • Finally the .catch() adapter creates a function with an "error" parameter which will perform a stack trace up hitting an error and respond with a server error.

Now I know what you're thinking, how do we execute this action we just created? Another great question dear reader, there are in fact a number of different ways in which we can do this. However I'm only going to demonstrate one of them. Say for example we create a view containing a simple user sign up form, with three fields for our three model attributes.


Once the submit button is clicked, our create action is going to take all of these attributes the user has entered and assign them to the new instance of our user model! Isn't that incredibly fascinating? Finally you're probably thinking "That's all well and good but what now?". So lets do something with the user object we just created, lets create another action to index this user object and any more we may create.


 In the above picture is an example of an index action. So once again, lets go a head and break this action down.
  • The action is called index and it contains the same parameters as our create action
  • Again we select our user model, then the .find() adapter defines a function called "foundUsers" with parameters of "err" and "users".
  • If an error occurs we log our value for users, which is an array.
  • Finally we respond with the value of users to the current view.
We can then create an index view to show any users we create:


And there we have it! I hope you enjoyed this brief look at models and controllers, you should go now and try it for yourself! 

Thanks for reading. 

You Might Also Like

1 comments

  1. Thank's for this article this is great and it helped me a lot ;)

    ReplyDelete

Couch Jumping