sails.config.connections

What is this?

Adapters are the middle man between your Sails app and some kind of storage (typically a database)

Adapters are configured in the connections.js file located in your project’s config directory. Here you can create different global “saved settings” that you can mix and match in your models.

Sails adapters have been written for a variety of popular databases such as MySQL, Postgres and Mongo. You can find a list of supported adapters here.

Example

To use the sails-memory adapter (for DEVELOPMENT ONLY), first install the module with npm install sails-memory, then define it in connections.js:

Here is an example adapter configuration file

myApp/config/connections.js

  1. module.exports.connections = {
  2. // sails-disk is installed by default.
  3. localDiskDb: {
  4. adapter: 'sails-disk'
  5. },
  6. memory: {
  7. adapter: 'sails-memory'
  8. }
  9. };

If you wanted to set memory as the default adapter for your models, you would do this. myApp/config/models.js

  1. module.exports.models = {
  2. connection: 'memory'
  3. };

Keep in mind that options you define directly in your model definitions will override these settings. Prior to v0.11, adapters were defined in myApp/config/Adapters.js. See v0.9 docs for more info.

Multiple connections for an adapter

You can set up more than one connection using the same adapter. For example, if you had two mysql databases, you could configure them as:

  1. module.exports.connections = {
  2. localMysql: {
  3. adapter: 'sails-mysql',
  4. user: 'root',
  5. host: 'localhost',
  6. database: 'someDbase'
  7. },
  8. remoteMysql: {
  9. adapter: 'sails-mysql',
  10. user: 'remoteUser',
  11. password: 'remotePassword',
  12. host: 'http://remote-mysql-host.com',
  13. database: 'remoteDbase'
  14. }
  15. };

Note If any connection to an adapter is used by a model, then all connections to that adapter will be loaded on sails.lift, whether or not models are actually using them. In the example above, if a model was configured to use the localMysql connection, then both localMysql and remoteMysql would attempt to connect at run time. It is therefore good practice to comment out any connection configurations that you aren’t using!