Locked video

Please purchase the course to watch this video.

Buy Now

On MVC

Generating a model

Summary

Kelvin introduces a Sails convention for organizing pages and actions more effectively. He renames index.ejs files to match their respective controllers (home.ejs and articles.ejs) and regenerates actions using the sails generate action command. He follows the Sails convention of prefixing actions that display pages with view-, making it easy to differentiate between page-rendering actions and logic-handling actions in the routes file.

He then transitions into explaining the MVC (Model-View-Controller) pattern, emphasizing how Sails structures applications using models (data representation), views (UI), and controllers (logic). He introduces models as single JavaScript files that export an object, handled by Waterline, Sails' ORM. Finally, he generates an Article model using sails generate model article, preparing to configure it in the next lesson.

Transcript

Alright. So before I get on with today's lesson, let’s do a little housekeeping and properly set up our pages following Sails conventions.

Notice we have controllers named articles and home, and both have index.js inside them. While this works, there’s a better way to structure it. Both of these actions are just displaying pages, so we can improve this setup.

First, I’m going to delete both actions—don’t worry, stay with me!—and rename our pages. Instead of index.ejs, we’ll rename them to match their respective sections:

  • articles.ejs

  • home.ejs

Now, let's recreate the actions using the Sails CLI. I’ll open my terminal and run:

sails generate action home/view-home

When generating actions that display pages, the convention is to prefix them with view-. This follows Sails’ MVC (Model-View-Controller) pattern, where actions that render pages should clearly indicate they’re responsible for views.

Sails automatically detects that this action is for displaying a page and sets up the viewTemplatePath. We didn’t manually specify this, but Sails understands that since we named the action view-home, it should correspond to home.ejs.

Next, we need to update our config/routes.js file. Since we deleted the previous action, we need to point the home route to view-home:

'GET /': 'home/view-home'

This makes it easy to distinguish between routes that serve pages and those that handle API logic.

Now, let’s do the same for the articles page:

sails generate action articles/view-articles

This creates an action inside api/controllers/articles/view-articles.js. Since we renamed index.ejs to articles.ejs, everything aligns correctly.

Once everything is updated, restarting the server (sails lift) and refreshing the browser confirms that everything works just like before—except now, we have a cleaner, more structured setup.

Understanding MVC

Now that we've looked at actions, controllers, and routes, let’s step back and talk about the MVC (Model-View-Controller) pattern.

  • Models represent the data (e.g., interacting with a database like PostgreSQL, MySQL, or MongoDB).

  • Views define what the user sees (our .ejs templates).

  • Controllers handle the application logic and decide what data to send to views.

We've seen controllers and views, but we haven’t yet talked about models. Models in Sails are simple JavaScript files that export an object. They are handled by Waterline, Sails’ ORM, which allows you to interact with various databases.

Let’s create our first model for the blog:

sails generate model article

This creates a new file api/models/Article.js. In Sails, model names are always singular (e.g., Article instead of Articles) because each model represents a single record in the database.

If we open api/models/Article.js, we see that it's a simple JavaScript file exporting an object. The next step is to define its attributes, which we’ll do in the next lesson.

Full Course

$
29.99

USD

plus local taxes
Buy Now