1

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'
    }
};
kevinsapp
  • 374
  • 5
  • 11

2 Answers2

2

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.

sgress454
  • 24,870
  • 4
  • 74
  • 92
  • First, thank you for answering my question so quickly. Next, I was mistaken about the location of production.js. It was in the right place all along, and I updated the question to reflect that. Finally, your point #2 was spot on! Once I set the `models.connection` key, all worked fine. Thanks again. – kevinsapp Aug 08 '14 at 15:07
0

In sails version 0.12 there will be two env files under config/env folder. So we can write mode specific (production/development) configuration in those two files.
In order to run our sails app in specific mode, we must follow the below steps:

Step 1) In config/local.js file

module.exports = {  
   // port: process.env.PORT || 1337,   // comment this line if you want to set the different ports for different modes
   environment: process.env.NODE_ENV || 'development'  
};

Step 2) In env/developement.js file
Write the development specific configurations.

module.exports = {
  port: 8080, // will change from default port 1337 to 8080
  models: {
      connection: 'developement_db',
      migrate: 'alter'
  }
};

Step 3) In env/production.js file
Write the production specific configurations.

module.exports = {
  port: 9090, // will change from default port 1337 to 9090
  models: {
      connection: 'production_db',
      migrate: 'safe'
  }
};

Step 4) To run sails app in specific mode,

  • To run in production mode

    $ NODE_ENV=production npm start

    Sails will be running on port 9090

  • To run in developement mode

    $ NODE_ENV=developement npm start

    Sails will be running on port 8080

Vishal Biradar
  • 1,219
  • 2
  • 12
  • 24