DEV Community

loading...

Getting started with Sails.js

daniellunsc profile image Daniel Luna ・5 min read

This post was originally posted on my blog started yesterday(03/01/2020) yay!

I would like some feedback about the post, and if the content is relevant too!

My twitter

-- POST

Usually I prefer to learn by doing projects and exploring the tool, with Sails.js it wasn't different.
I'll show you how to create a restful API with Sails in the easy way in less than 30 minutes.

What you will learn

  • Basic about Sails.js
  • Configuring Sails.js to work with default database.
  • How to create a Model in Sails.js.

What is Sails.js ?

Sails.js is a web framework used mostly to develop applications using MVC(Model-View-Controller) pattern and also can be used to create restful API's quick and easy.

Installation

To work with Sails.js, you'll need installed:

  • Node.js
  • NPM or Yarn for package managing. (We will use NPM in this tutorial)

After Node.js and NPM(or Yarn) installed, lets install Sails-cli globally using npm:

$ npm i -g sails

Tools used

To test our API we'll be using Postman

And to write the code we'll be using VSCode

Bootstraping the project

To start our project, we just need to go to our CMD/Bash and type:

$ sails new code-addict

Note that code-addict its just the project name, you can use whatever you want.

And for this project, lets select option 2 when prompted:

prompt-selection

After that, Sails will install dependencies and will be ready for a launch!

Project Structure

After your project bootstrap, your folder should look like this:
project-structure

Ok, Ok, lets wait a little bit and discover the main parts of this structure.

  • /api: represents your application itself. Here we have our conntrollers, models, helpers and services, and its where we'll spent a lot of time!
  • /config: represents all the config of your app, including database connections, production environemnt configurations, how your project init, and security configs like policies.

If you would like, I can explain in more details what each config in config folder do (just tag me on twitter!), by now, lets focus on our API.

What are models?

Models represent our data, they have the responsability to load the data from our Database to something we can manipulate and use console.log()! to see the results.

Manipulate the models means add some data, remove or edit then and save it to keep our data stored.

Creating Models

Ok, now we have our project set-up, and it's ready for usage in development mode.
To create a model, there's two ways:

  • The easy way (sails generator)
  • The handy way (create file)

By now, lets use the easy way to create our model. Lets go back to your CMD/Bash in our project folder and type:
$ sails generate model user

Ok, ok, ok, lets slow down here and check out what is this thing going to do:

I'll break this line into 4 topics to explain:

  • > sails - here we are using our sails-cli installed globally.
  • > generate - here we are saying to our cli we want to generate something.
  • > model - here we are saying to our generate what we want to generate(a model!)
  • > user - and here we are giving a name to our model.

After that, if everything is ok, lets check our /api/models folder:

User-model

Here we go, now we have our model, but if we open it, we can see there's nothing on our model. Let's add a name and an age!

The magic of Sails.js

Ok, now we have added the attributes to our model, it's time to test it!

If we go to our CMD/Bash again and type:

$ sails lift 

It will prompt us about 3 options, for now lets just select alter.
WARNING: DO NOT USE THAT IN PRODUCTION.

And we must get a result that our server is up on port 1337!

server-up

Now let's just open our postman to test our fresh new endpoint:

Lets type on postman's url: localhost:1337/user and hit send to check the result:
postman-start-get

Remember to verify to type the same name of the model!

Ok, our API returned an empty array, now lets just change the operation to POST and try again to see the result.

postman-post-url

Woa! What is that? Don't worry!
If you read the message, you will realize that it's just an attribute missing from the request, Sails handles it automatically if we specify it on required:true while writing the attributes.

Let's select the body tab just under our request, select form-data, add an name attribute with your name and hit send!

postman-post-success

Verify our response now, we have an ID, a name fullfiled and a age set to 0, just like we specifed on our model, that's because our defaultsTo: 0 on our model.
Try again! Add an age attribute with your age and send!

Verify that we also have a createdAt and a updatedAt attribute, that's default by sails but we can change it inside the configs.

If you want me to show how, tag me on twitter to let me know!

Edit and Delete on our API

To edit a data or delete from our endpoint, we'll need an id. In our case, let's just use the id: 1 from the record created in last topic.

Let's change our postman operation to PATCH and insert into url localhost:1337/user/1 - check the 1 on the url now, it's the id we are changing! - then change the name to Tony Stark and hit send, check the response:

patch-successful-postman

Ok, the name changed and the updatedAt attribute too, that's awesome! Sails record when we change some data.

Now for you to test: change the operation to DELETE and hit send, check the result!

To finish up!

That's a quick and introductory post about Sails.js, and for the next parts I plan to explain how to use a real database like MySQL, the relationships (one-to-one, one-to-many, many-to-many), write custom controllers and protect the API using JWT.

If you would like, hit me on twitter to let me know!

Discussion (0)

pic
Editor guide