Sails.js

repository·master·Indexed 12 days ago

https://github.com/balderdashy/sails

An enterprise-grade MVC web framework for Node.js designed for building complex, data-driven, and real-time applications. Based on Express and Socket.io, version 1.5.18 provides native support for async/await, WebSockets, and a structured architecture similar to Ruby on Rails.

Tokens
222.7K
Snippets
698
Records
1.2K
Agent score
96%

What's inside Sails

  1. Understand WebSockets in Sails

    master

    Sails uses socket.io as its underlying engine for realtime communication. While every Sails application has a raw Socket.IO instance available via sails.io, most realtime functionality is wrapped by the sails.sockets method to provide a more convenient and safer API for developers.

    Key areas of WebSocket usage in Sails include:

    • Client-to-server communication: Using the sails.io.js client library.
    • Server-to-client communication: Using the sails.sockets API.
    • Model-driven updates: Using Resourceful PubSub to broadcast changes in Sails models to connected clients.
  2. Deployment options for Sails.js

    master

    Sails.js can be deployed to almost any Node.js compatible hosting provider. The deployment process is generally identical to deploying any other Node.js application.

    Key Hosting Providers:

    • PaaS (Platform as a Service): Heroku, Microsoft Azure, Google Cloud Platform, Nanobox, OpenShift (Red Hat), and various others.
    • IaaS (Infrastructure as a Service / Bare Metal): Amazon Web Services (AWS), DigitalOcean, exoscale / CloudControl, and RoseHosting.
    • Process Management: PM2 (KeyMetrics) is recommended for managing Node.js production environments on VPS instances.

    Important Pre-deployment Checklist:

    • Review and adjust your config/env/production.js file.
    • Ensure sensitive credentials are moved to environment variables rather than hard-coded in configuration files.
  3. Overview of the sails.io.js socket client

    master
    The sails.io.js library is a lightweight JavaScript wrapper for the Socket.IO client designed for Sails applications. It provides an Ajax-like interface (.get(), .post(), .put(), and .delete()) that allows you to communicate with your Sails backend using WebSockets while reusing your existing HTTP routes. When you call io.socket.post('/user'), Sails routes it exactly like an HTTP POST request to that route.
  4. What is a custom adapter and when to use one

    master

    In Sails, database adapters expose interfaces that act as a contract for implementing specific functionality. This ensures consistent usage patterns across different models and databases.

    Use Cases

    • Databases: Integrating with relational (MySQL, PostgreSQL) or non-relational (MongoDB, Redis) databases.
    • RESTful Services: Integrating with any purely RESTful web service or internal/proprietary API.
    • Hardware/Devices: Normalizing asynchronous communication with hardware into a CRUD-like interface.

    When NOT to use an adapter

    Adapters are designed for services that fit a CRUD (Create, Read, Update, Delete) mold. If you are integrating a service with an RPC-style interface (e.g., sending an email or reading a remote sensor with one-off methods), you should use a machinepack instead of a Waterline adapter.

  5. What is Resourceful PubSub (RPS)?

    master

    Resourceful PubSub (RPS) is a high-level, data-centric interface for managing real-time client-server communication in Sails. Instead of manually managing socket rooms and event names, RPS automatically equips every model with methods to broadcast notifications and subscribe sockets to specific database records.

    Key Benefits:

    • Standardization: It provides a conventional interface for socket communication (standardized room names, event names, and data schemas).
    • Automation: If you use the Sails blueprint API, RPS methods are called automatically. For example, calling the find or findOne blueprints via a socket request automatically subscribes the requesting socket to those records.
    • Abstraction: RPS acts as a contextualized wrapper around lower-level sails.sockets.* methods, handling the complexity of room naming and event identity behind the scenes.
  6. What is a One Way Association (Belongs To)

    master

    A one way association (also known as a "Belongs To" association) occurs when one model is associated with another, but the relationship is not reciprocal in the model definitions.

    In this setup:

    1. You can query the associating model (e.g., User) and use .populate() to retrieve the associated model (e.g., Pet).
    2. You cannot query the associated model (e.g., Pet) and use .populate() to retrieve the associating model (e.g., User), because the Pet model has no knowledge of the User.

    Because the association is only defined on one side, the associated model (the Pet) has no restrictions on how many models of the associating type (User) can point to it.

  7. What is middleware in Sails?

    master

    In Sails, middleware refers to code that runs in the middle of the request/response stack. Depending on your specific goal, you can implement different types of middleware:

    • HTTP middleware: Runs before every HTTP request. This is used for low-level tasks like parsing cookies or request bodies.
    • Policies: Runs before one or more specific controller actions.
    • Hooks with routes feature: Runs before one or more route handlers.
    • Custom responses: Runs after one or more controller actions.

    Note: The HTTP middleware stack is only used for "true" HTTP requests; it is ignored for virtual requests (e.g., requests from a live Socket.io connection).

  8. What is the sails-hook-blueprints hook?

    master

    The sails-hook-blueprints is a core hook in the Sails.js framework that implements support for the Blueprint API. It automates the creation of RESTful routes by:

    1. Reading blueprints from sails.modules into the hook's middleware.
    2. Binding shadow routes to both blueprint actions and standard controller actions.
    3. Listening for the route:typeUnknown event on the sails instance to interpret route syntax and bind the appropriate middleware during the Router loading phase.

    Because it is a core hook, it is enabled by default in Sails applications.

  9. What is the Waterline Query Language?

    master
    Waterline Query Language is the syntax used by Sails' model methods to retrieve or mutate records. It acts as an abstraction layer that translates your queries into native queries for your specific database (e.g., MySQL, MongoDB, Redis) via database adapters. This allows you to switch databases with minimal changes to your application code.
  10. Overview of the Sails asset pipeline

    master

    The Sails asset pipeline uses Grunt tasks to automate frontend workflows. It provides a set of conventional defaults that can be fully customized to fit your project's needs.

    Key capabilities included in the default configuration:

    • LESS compilation: Automatic conversion of LESS to CSS.
    • Cache busting: Using file hashing to manage browser caching.
    • Asset optimization: Automatic injection, minification, and concatenation.
    • Production readiness: Transpilation of ES6+ JavaScript for broad browser compatibility and optimization of assets for production.
    • Development workflow: File watching, syncing, and creation of a web-ready .tmp/public/ directory.