REST API Development with Sails.js
In the previous article I explained how develop a REST API with MongoDB, NodeJS and Express from scratch. We saw that developing an API from scratch is a simple process although it could be quite tedious. We have also written in previous articles that when we have to develop a new project is very important to know and to analyze which tools are the best for our project.
Productivity is a very important factor in the software development process. For this reason, taking productivity into account, in this article I would like to introduce the Sails.js framekork, which will easy and expedite the REST API development process.
Sails.js description
Based on the official Sails.js website:
Sails.js make it easy to build custom, enterprise-grade Node.js apps. It is designed to mimic the MVC pattern of frameworks like Ruby on Rails, but with support for the requirements of modern apps: data-driven APIs with scalable, service-oriented architecture. It’s especially good for building chat, realtime dashboards, or multiplayer games.
One of the things that Sails.js provides us, among many other, is the REST API development easily and quickly. For this reason, I consider that Sails.js is a very important tool in the software stack that I explained in the article modern web applications development.
Developing an API with Sails.js
In this article I had planned to write some sections to explain how to develop an API with Sails.js, but as the process is so easy and so fast I’m going to explain it in only one section. Running solely a couple of commands and writing solely a line of code we will be able to develop the same API that we developed in the article API REST Development with MongoDB, NodeJS and Express, but with more features:
1 2 3 4 5 |
npm install sails@beta -g sails generate new MyApi --no-front-end cd MyApi npm install sails generate api message |
As we can see, at first place we generate our project running the command sails generate new MyApi –no-front-end and, after that, we generate our API for the Message entity running the command sails generate api message.
Finally, we have to define our entity properties, which only has the text property. For this, we have to edit the file api/models/Message.js and define its properties in this way:
1 2 3 4 5 |
module.exports = { attributes: { text: { type: 'string'} } }; |
Sails.js Database
Sails.js works with the Waterline ORM, which is developed and maintained by the Sails.js development team.
Waterline has adapters to connect to some databases (MySQL, PostgreSQL and MongoDB, among others). Also we can configure Waterline to save our data in disk or in memory (it isn’t recommended for production), so we could test our applications without the need of running a database in our workstation.
By default, Sails.js is configured to use the local disk adapter (save the data in a temporary folder of our file system). We define our database connections in the file config/connections.js and we define which connection we want to use by default for our entities in the file config/models.js. When we define each of our entities we can specify which connection we want to use. If we don’t specify any connection then Sails.js will use the connection defined by default in the config/models.js file.
Testing our API
For testing our API we have to run our application running the following command:
1 |
sails lift |
Once we have started our server then we will use a Chrome extension called POSTMAN to send requests to our API.
If we want to get all the messages then we will send a GET request to the URL http://localhost:1337/message:
If we want to create a new message then we will send a POST request to our API:
And if we send again a GET request to http://localhost:1337/message then we will get the following data:
Models associations with Sails.js
Models associations is another feature that has got Sails.js to generate APIs and that would involve quite work if we had to develop it manually.
To explain models associations we are going to use the same example that use the Sails.js team in the documentation. The example that I’m going to explain is an one to many relationship between Users and Pets, where an user will have got many pets.
At first place, I’m going to generate our API for the Pet entity:
1 |
sails generate api pet |
And I’m going to define it as the Sails.js documentation do it:
1 2 3 4 5 6 7 8 9 |
module.exports = { attributes: { name: 'string', color: 'string', owner: { model: 'user' } } }; |
Next, I’m going to create our API for the User entity:
1 |
sails generate api user |
And I’m going to define it with the following properties:
1 2 3 4 5 6 7 8 9 10 |
module.exports = { attributes: { name: 'string', age: 'integer', pets: { collection: 'pet', via: 'owner' } } }; |
We are going to start our application with the command sails lift and we are going to test our API with POSTMAN, sending the following requests to our server:
Post request to create an user
GET request to get an user list
POST request to create a new pet
When we create a new pet we have to introduce the user ID to which the pet belongs.
GET request to get a user list
Automatically our API returns the pets associated to each user.
GET request to get an user
GET request to get an user’s pet
If we take notice of the URL we see how we associate the models to get the pet which ID equals to 1 of the user which ID equals to 1.
Conclusion
In this article we have seen that developing REST APIs with Sails.js is a very simple and a very fast process. Only with a couple of Sails.js commands we are able to create an application structure and an API. From there, we only will have to define our models properties and its associations.
Furthemore, Sails.js offers much more features: realtime notifications, security management, an ORM that will allow us to develop our applications for many databases, and much more.
We have seen that if we choose Sails.js for developing our API instead of developing our API from scratch (as we did in our previous article) we will improve our productivity noticeably.
In next articles we will see more tools that will help us developing our projects.
Gracias Jose Luis, por estos articulos. Siguiendo este cuando hago el ultimo ejemplo
http://localhost:1337/user/1/pets/1 no se porque obtengo un arreglo vacio ([])
Hola Jose Luis, me ocurre el mismo evento que a PABLO PEUSCOVICH. Me regresa un arreglo vacio [] al hacer la ultima llamada. ¿Tienes idea de que lo pudo ocasionar? Segui el tutorial al pie de la letra.
Hi Jose,
Good Day.. Your API helped me to test One to Many Relationship between User & Pet.
When i tried to implement the Many to Many relationship between user & pet, I am facing this issue. Can you please review my request in Stackoverflow
http://stackoverflow.com/questions/28572810/sails-js-blueprint-api-get-many-to-many-relationship-by-id-is-not-working
Thanks and Regards,
Raj
Gracias Jose .es moue importante para me
Greetings from Turkiye,
Thanks for this great post! Please keep going on sharing Sails tutorials.
Well done!