Sails.js
repository·master·Indexed 12 days ago
https://github.com/balderdashy/sailsAn 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.
What's inside Sails
- Sails, leveraging Express, provides built-in and configurable protection against common web-application-level attacks. The framework includes out-of-the-box support for several security mechanisms that can be enabled or tuned to protect your application.
Overview of Waterline ORM in Sails
masterSails apps come bundled with Waterline, an Object-Relational Mapper (ORM) implemented via the
sails-hook-ormdependency. Waterline allows you to interact with your database using JavaScript models instead of writing raw SQL.For fundamental usage patterns, refer to the Concepts > Models & ORM documentation.
Understand WebSockets in Sails
masterSails uses
socket.ioas its underlying engine for realtime communication. While every Sails application has a raw Socket.IO instance available viasails.io, most realtime functionality is wrapped by thesails.socketsmethod 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.jsclient library. - Server-to-client communication: Using the
sails.socketsAPI. - Model-driven updates: Using
Resourceful PubSubto broadcast changes in Sails models to connected clients.
- Client-to-server communication: Using the
Deployment options for Sails.js
masterSails.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.jsfile. - Ensure sensitive credentials are moved to environment variables rather than hard-coded in configuration files.
Overview of the sails.io.js socket client
masterThesails.io.jslibrary 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 callio.socket.post('/user'), Sails routes it exactly like an HTTP POST request to that route.What is a custom adapter and when to use one
masterIn 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.
What is Resourceful PubSub (RPS)?
masterResourceful 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
findorfindOneblueprints 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.
What is a One Way Association (Belongs To)
masterA 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:
- You can query the associating model (e.g.,
User) and use.populate()to retrieve the associated model (e.g.,Pet). - You cannot query the associated model (e.g.,
Pet) and use.populate()to retrieve the associating model (e.g.,User), because thePetmodel has no knowledge of theUser.
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.- You can query the associating model (e.g.,
What is middleware in Sails?
masterIn 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
routesfeature: 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).
What is the sails-hook-blueprints hook?
masterThe
sails-hook-blueprintsis a core hook in the Sails.js framework that implements support for the Blueprint API. It automates the creation of RESTful routes by:- Reading blueprints from
sails.modulesinto the hook's middleware. - Binding shadow routes to both blueprint actions and standard controller actions.
- Listening for the
route:typeUnknownevent on thesailsinstance 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.
- Reading blueprints from
What is the Waterline Query Language?
masterWaterline 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.Overview of the Sails asset pipeline
masterThe 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.