Today I got the opportunity to give a presentation at Dev Mountain in Provo, UT on why you should use Sails.js, an MVC framework for Node. There are a lot of reasons, but I wanted to share some of my favorites.
Blueprints
One feature of Sails that is really useful in getting started is its Blueprints API. The Blueprints API allows you to get a RESTful API up and running quickly and with very little actual coding. Use the CLI to create a new blueprint:
sails generate api User
That generates a model file (api/models/User.js
) and a controller file (api/controllers/User.js
). In addition, it gives you basic operations like getting a list of users or creating a new one. To get a list of the users, simply make a GET request to /user
and you'll get the response back as an array of objects. No additional code needed. To create: /user/create?name=Name&age=20
. That's it.
Now clearly you'll want the create functionality to be a little different than this. In addition, you'll need to set up the database connection so it stores in the correct spot, but it still saves you some work right out of the box.
Policies
Another Sails feature that I love using are Policies. Policies can be used to prevent access to different parts of your app. A policy is a simple function that has 3 parameters, the request, the response, and a next callback. If the conditions needed are met, the next()
callback is invoked. Otherwise, you can return an error message of your choice.
Sails also makes it very easy to assign policies to all routes in your app, to certain controllers, or even single functions on a controller. Policies are a very powerful part of Sails.
In my experience, we've used policies for functions other than just authentication and access to the app. For certain routes, we need more information about the user stored to the session to reuse, and we don't want to continue to make calls over and over to get that information. So we use policies to call the database, get the information once, and then save it to the request.
Routes
Adding routes to your app, whether for views or API endpoints to retrieve data, is extremely simple in Sails. One of the files in the config
folder, config/routes.js
, is where all the routes are declared. Declaring a route is as simple as this:
'GET /api/route': 'SomeController.functionOnTheController'
To simply declare a route that returns a view:
'/': { view: 'homepage' }
Anyways, route declaration and producing a RESTful API is really easy in Sails. Between the simplicity of the routes.js
file and the Blueprints API, it's easy to get data to your app.
Waterline
Waterline is an ORM/ODM provided as part of the framework. It is a datastore agnostic tool that allows you to find, create, update, or delete data without writing vendor specific code. So, in other words, whether you are using a SQL database or a no SQL database, you write your database access functions in the same way. For those that have used Mongoose, it's the same idea.
The only thing you have to do is declare which database adapter you want to use with your Sails project. In fact, each model can use a different database if you'd like. You can install those adapters through NPM. A cool thing that Waterline allows you to do is even to do joins across databases. You can do joins in Waterline from one Model to another, and if one model is saved in SQL Server and another model is saved in Mongo, you can still do the join. Check out the docs to get more information on this.
Conclusion
There are a lot of reasons to like Sails.js. These are just 4 reasons why I like it. And, I only barely touched on each of these ideas. I definitely recommend using Sails.js on any of your projects. It's been great when I've used it. The community is great, as well as the core team. I've gotten responses from both StackOverflow and Mike McNeil, the creator of the framework. Definitely let me know if you decide to use this framework, or if there are others that you use! I love learning more and seeing what other people are doing.
Top comments (0)