June 22, 2014 Uncategorized

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:

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:

 

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:

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:

blog-ch2-empty-collection

If we want to create a new message then we will send a POST request to our API:

blog-ch2-post

And if we send again a GET request to http://localhost:1337/message then we will get the following data:

blog-ch2-collection

 

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:

And I’m going to define it as the Sails.js documentation do it:

Next, I’m going to create our API for the User entity:

And I’m going to define it with the following properties:

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

blog-ch2-rel-insert-user

 

GET request to get an user list

blog-ch2-rel-list-users-pets-empty

POST request to create a new pet

blog-ch2-rel-insert-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

blog-ch2-rel-list-users-with-pets

Automatically our API returns the pets associated to each user.

GET request to get an user

blog-ch2-rel-list-one-user-with-pets

GET request to get an user’s pet

blog-ch2-rel-list-one-user-one-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.

  1. Pablo Peuscovich says:

    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 ([])

  2. daniel says:

    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.

  3. Raj says:

    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

  4. masume says:

    Gracias Jose .es moue importante para me

  5. Muhammed Sadık says:

    Greetings from Turkiye,
    Thanks for this great post! Please keep going on sharing Sails tutorials.
    Well done!

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top