Processing database environment configuration in Sails.js

The problem I have is related to the following quote from the official documentation :

Note. If any connection to the adapter is used by the model, all connections to this adapter will be uploaded to sails.lift, regardless of whether they use models. In the above example, if the model was configured to use the localMysql connection, then localMysql and remoteMysql will try to connect at run time. Therefore, it is good practice to split the connection configurations into the environment and save them in the appropriate configuration files, depending on the specific environment, or comment out any connections that you do not want to use.

How to configure a connection for a production server?

My connections.js file is as follows:

module.exports.connections = { mongoDev: { adapter: 'sails-mongo', host: 'localhost', port: 27017, user: 'username', password: 'password', database: 'database' }, mongoLive: { adapter: 'sails-mongo', host: 'host.mongolab.com', port: 31681, user: 'user', password: 'password', database: 'database' } }; 

And in my environment configuration files I have:

development.js

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

production.js

 module.exports = { models: { connection: 'mongoLive' }, port: 3000, }; 

This works on my local machine because the production database server is on an external server. In a production environment, I get the following error:

 [Error: failed to connect to [localhost:27017]] 

It works if I delete the mongoDev object from my connections object.

I also tried using adapters.js, but this only led to some errors in obsolescence.

 $ sails -v info: v0.9.9 

I get something else when running sails lift :

 info: Sails info: v0.10.5 
+7
javascript database-connection sails-mongo
source share
1 answer

You want to save the actual definition of the connection in the development.js or production.js file and remove them from connection.js. This is a little unintuitive.

development.js

 module.exports = { connections : { mongoDev: { adapter: 'sails-mongo', host: 'localhost', port: 27017, user: 'username', password: 'password', database: 'database' } }, models: { connection: 'mongoDev' } }; 

production.js

 module.exports = { connections : { mongoLive: { adapter: 'sails-mongo', host: 'host.mongolab.com', port: 31681, user: 'user', password: 'password', database: 'database' } }, models: { connection: 'mongoLive' }, port: 3000, }; 
+12
source share

All Articles