View our GitHub

Please visit sails-docs on GitHub to view documentation on your mobile device.

Edit Page

Globals

Overview

For convenience, Sails exposes a handful of global variables. By default, your app's models, services, and the global sails object are all available on the global scope; meaning you can refer to them by name anywhere in your backend code (as long as Sails has been loaded).

Nothing in Sails core relies on these global variables - each and every global exposed in Sails may be disabled in sails.config.globals (conventionally configured in config/globals.js.)

The App Object (sails)

In most cases, you will want to keep the sails object globally accessible- it makes your app code much cleaner. However, if you do need to disable all globals, including sails, you can get access to sails on the request object (req).

Models and Services

Your app's models and services are exposed as global variables using their globalId. For instance, the model defined in the file api/models/Foo.js will be globally accessible as Foo, and the service defined in api/services/Baz.js will be available as Baz.

Async (async) and Lodash (_)

Sails also exposes an instance of lodash as _, and an instance of async as async. These commonly-used utilities are provided by default so that you don't have to npm install them in every new project. Like any of the other globals in sails, they can be disabled.

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails-docs repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.