{"id":376,"date":"2014-09-02T18:30:00","date_gmt":"2014-09-02T18:30:00","guid":{"rendered":"\/blog\/blog\/introduction_to_sails_js.html"},"modified":"2014-09-02T18:30:00","modified_gmt":"2014-09-02T18:30:00","slug":"introduction-to-salisjs","status":"publish","type":"post","link":"https:\/\/mohammedlakkadshaw.com\/blog\/introduction-to-salisjs.html","title":{"rendered":"Introduction to SalisJS"},"content":{"rendered":"<p>I was looking for a Node.JS web framework for my project, and in my quest for finding the best framework, I stumbled across Sails.JS, and I have been using it for a few months now and it is pretty awesome at most of the part, so if you need something like Rails for Node.js, Sails is the way to go.<\/p>\n<p>The Sails.JS is an MVC framework, so there are Models, Views and Controllers.<\/p>\n<h2 id=\"models\">Models:<\/h2>\n<p>In Sails.JS you can create models, and model represents the data model, so if for each table you would have a model and in case of no-sql databases, each collection is represented by a model.<br \/>\nInside a model you can describe, the attributes present in that model, their datatype and validation rules.<\/p>\n<p>Example of a User Model:<br \/>\n\tuser.js<\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>\tmodule.exports = {\n\t\tattributes: {\n\t\t\tfirstName: 'string',\n\t\t\tlastName: 'string',\n\t\t\temail: 'email',\n\t\t\trole: {\n\t\t\t\ttype: 'string',\n\t\t\t\tenum: ['admin', 'user']\n\t\t\t}\n\t}\n<\/code><\/pre>\n<\/div>\n<p>In the above example we have a user model, which has a number of attributes, like firstName, lastName, email and role.<br \/>\nThe model structure is pretty straightforward, and Sails.js provides some useful helpers in modules like the <code class=\"highlighter-rouge\">enum<\/code>.<br \/>\nWe have added an <code class=\"highlighter-rouge\">enum<\/code> attribute in role, now Sails will accept only the values present in the enum array.<\/p>\n<h3 id=\"magic\">Magic:<\/h3>\n<p>The magical part is sails is <strong>blueprints<\/strong>. Sails will create CRUD routes based on the data model.<br \/>\nSails will look at your data-model and will create blueprint routes and REST api.<br \/>\nSo, by looking at the model we have created above sails will create REST APIs.<br \/>\n<strong>Create User<\/strong>:\t\tHTTP POST:\t\t\/user<br \/>\n<strong>Update User<\/strong>:\t\tHTTP PUT: \t\t\/user\/:id<br \/>\n<strong>Delete User<\/strong>:\t\tHTTP DELETE: \/user\/:id<br \/>\n<strong>Fetch User<\/strong>:\t\t\tHTTP GET: \t\t\/user\/:id<br \/>\n<strong>Get All Users<\/strong>:\tHTTP GET: \t\t\/user<\/p>\n<h3 id=\"associations-in-model\">Associations in Model<\/h3>\n<p>In Sails you can associate one model with another model, and associations can span across databases.<br \/>\nSo if you have user data stored in Postgres and photos stored in Mongodb, you can interact with the data if they lived in the same database.<\/p>\n<h4 id=\"many-to-many\">Many to Many<\/h4>\n<p>In this type of association, one model can be associated with many other models and vice versa.<br \/>\nLets consider an example of a Real Estate, a Real Estate can be owned by multiple people, a person can own multiple Real Estates.<br \/>\nThus we can create a many to many relationship between people and real-estate.<br \/>\nSo, let\u2019s begin:<\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>estate.js\n\nmodule.exports = {\n\t\tattributes: {\n\t\t\tpropertyName: 'string',\n\t\t\tAddress: 'string',\n\t\t\towners: {\n\t\t\t\tcollection: 'user',\n\t\t\t\tvia: 'estates'\n\t\t\t}\n\t\t}\n\nuser.js\n\nmodule.exports = {\n\tattributes; {\n\t\tfirstName: 'string',\n\t\tlastName: 'string',\n\t\testates: {\n\t\t\tcollection: 'estates',\n\t\t\tvia: 'owner'\n\t\t}\n\t}\n}\n<\/code><\/pre>\n<\/div>\n<p>So, in the above example we have created two models, one is user model and another one is estate model.<\/p>\n<p>In the estate model we have created a property named owner, which reference the user model model and points to the owners of the property, so this will be an array containing all the owners of the property.<\/p>\n<p>In the User model we have created a property named estates, and it points to the estate model and it will contain all the estates owned by the user.<\/p>\n<h3 id=\"lifecycle-callbacks-in-models\">LifeCycle callbacks in Models<\/h3>\n<p>There are lifecycle call-backs present in the models which are very helpful, for e.g: If want to can write some cleanup code when an item in a model is deleted, so you can write the code for that inside the <code class=\"highlighter-rouge\">afterDestroy<\/code> method.<\/p>\n<p><strong>Callbacks on create<\/strong><\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>beforeValidate: fn(values, cb)\nafterValidate: fn(values, cb)\nbeforeCreate: fn(values, cb)\nafterCreate: fn(newlyInsertedRecord, cb)\n<\/code><\/pre>\n<\/div>\n<p><strong>Callbacks on update<\/strong><\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>beforeValidate: fn(valuesToUpdate, cb)\nafterValidate: fn(valuesToUpdate, cb)\nbeforeUpdate: fn(valuesToUpdate, cb)\nafterUpdate: fn(updatedRecord, cb)\n<\/code><\/pre>\n<\/div>\n<p><strong>Callbacks on destroy<\/strong><\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>beforeDestroy: fn(criteria, cb)\nafterDestroy: fn(destroyedRecords, cb)\n<\/code><\/pre>\n<\/div>\n<p>Consider the example:<\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>\tmodule.exports = {\n\t\tattributes; {\n\t\t\tfirstName: 'string',\n\t\t\tlastName: 'string',\n\t\t\testates: {\n\t\t\t\tcollection: 'estates',\n\t\t\t\tvia: 'owner'\n\t\t\t}\n\t\t},\n\t\tafterDestroy: function(values, cb) {\n\t\t\t\/\/Write you cleanup code here...\n\n\t\t\tcb();\n\t\t\t}\n\t}\n<\/code><\/pre>\n<\/div>\n<p>To learn more about the lifecycle callbacks, see the <a href=\"http:\/\/sailsjs.org\/#\/documentation\/concepts\/ORM\/Lifecyclecallbacks.html\">official documentation<\/a>.<\/p>\n<h2 id=\"controllers\">Controllers<\/h2>\n<p>The main logic of your application will be present in the controllers, the controllers are responsible for responding to the requests.<br \/>\nAlthough, some magic is provided by Sails, using blueprints to create automatic CRUD API, often times it is not enough and you will want to add more functionality and more types of routes.<\/p>\n<p>Consider an example controller file name greeting.js:<\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>\tmodule.exports = {\n\t\thello: function(req, res) {\n\t\t\t\treturn res.send(\"Hello world\");\n\t\t},\n\t\thi: function(req, res) {\n\t\t\t\treturn res.send(\"hi user\");\n\t\t}\n\t}\n<\/code><\/pre>\n<\/div>\n<p>The above file contains two actions, namely <code class=\"highlighter-rouge\">hello<\/code> and <code class=\"highlighter-rouge\">hi<\/code>, inside these actions we can write the code to do whatever we want, in the above example we are sending messages, \u201chello world\u201d and \u201chi user\u201d respectively.<\/p>\n<p>Sails has automatic routing, so the controllers and actions that you have created will be routed automatically, by the following format <code class=\"highlighter-rouge\">\/:controller\/:nameOfAction<\/code>.<\/p>\n<p>So routes for our above actions will be <code class=\"highlighter-rouge\">\/greeting\/hello<\/code> and <code class=\"highlighter-rouge\">\/greeting\/hi<\/code>.<\/p>\n<h3 id=\"overriding-blueprint-actions\">Overriding blueprint actions<\/h3>\n<p>If you want override the blueprint actions, like create, update or delete, just create actions in the controller with that name and it will override the default functionality.<br \/>\nSuppose we want override the <code class=\"highlighter-rouge\">create<\/code> functionality for user, so in user controller, we will create an action having the name <code class=\"highlighter-rouge\">create<\/code>.<\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>module.exports = {\n\t\tcreate: function(req, res) {\n\t\t\t\treturn res.send(\"Hello world\");\n\t\t}\n}\n<\/code><\/pre>\n<\/div>\n<p>The above code overrides the create functionally, now if we send a http post request at url \/user, instead of creating a new user it will send a response \u201cHello world\u201d.<\/p>\n<p>In the similar manner we can override the default behaviour by creating actions having the name <code class=\"highlighter-rouge\">create<\/code>, <code class=\"highlighter-rouge\">update\t<\/code> and <code class=\"highlighter-rouge\">delete<\/code>.<\/p>\n<h2 id=\"policies\">Policies<\/h2>\n<p>You don\u2019t want anyone accessing and performing unintended activity, like deleting all your real estates, so for that we have policies.<br \/>\nIf you are familiar with ExpressJS, polices are similar to ExpressJS middle-wares.<br \/>\nThe <a href=\"http:\/\/sailsjs.org\/#\/documentation\/concepts\/Policies\">official SailsJS documentation<\/a> explains policies in great detail, be sure to head over there and read about it in details.<\/p>\n<h1 id=\"conclusion\">Conclusion<\/h1>\n<p>That\u2019s my take on SailsJS, it\u2019s a great framework to build apps in Node.JS.<br \/>\nLet me know if you have any suggestions, questions about this in the comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was looking for a Node.JS web framework for my project, and in my quest for finding the best framework, I stumbled across Sails.JS, and I have been using it for a few months now and it is pretty awesome at most of the part, so if you need something like Rails for Node.js, Sails [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-376","post","type-post","status-publish","format-standard","hentry","category-nodejs"],"_links":{"self":[{"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/posts\/376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/comments?post=376"}],"version-history":[{"count":0,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/posts\/376\/revisions"}],"wp:attachment":[{"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/media?parent=376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/categories?post=376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/tags?post=376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}