Search code examples
sails.jssails-mongo

Sails v0.10.x doesn't respect connection config in production.js when in production mode


What is the correct way to set a db connection in Sails v.0.10.x for production use? I expected Sails to use the connection I referred to in production.js when I start my app in production mode (environment), but it doesn't. It seems to always always use the default connection - 'localDiskDb'.

However, when I start sails in development mode (environment), it does use the connection specified in config/development.js, as I would have expected.

UPDATED

Note: I was mistaken when I wrote the location of production.js. It was in /config/env/production.js just like sgress454 said it should be. Actually, this file was created by the generator and put in the right place and I didn't change that.

config/env/production.js looks like this:

// config/env/production.js

module.exports = {
    connection: 'mongo_production'
};

config/models.js looks like this:

// config/models.js

module.exports.models = {
    // connection: 'localDiskDb'
};

config/connections.js looks like this:

// config/connections.js

module.exports.connections = {
    mongo_development: {
        adapter: 'sails-mongo',
        host: 'localhost',
        port: 27017,
        user: '',
        password: '',
        database: 'my_local_db'
    },

    mongo_production: {
        adapter: 'sails-mongo',
        url: 'mongodb://me:mypw@foobar.mongohq.com:10052/my_production_db'
    }
};

Solution

  • Couple of issues here:

    1. Per-environment configuration files need to go in the config/env subdirectory, or else they'll be treated the same as regular config files (i.e., not given precedence). If you have multiple files trying to set the same key, results will be unpredictable.
    2. You're attempting to change the default connection for models by setting the connection config key; it needs to be models.connection.

    Putting the two together, you need a config/env/production.js file that looks like this:

    module.exports = {
        models: {
            connection: 'mongo_production'
        }
    };
    

    Then when lifting in production mode, models will use the mongo_production connection by default.