loopback icon indicating copy to clipboard operation
loopback copied to clipboard

support configuration of datasources/models from environment variables and other sources

Open seanbrookes opened this issue 12 years ago • 12 comments

the current LB datasource configuration makes it awkward to deploy a mongo (mongolab) db based app to Heroku.

LB datasources:

  "mongodb": {
    "defaultForType": "mongodb",
    "connector": "loopback-connector-mongodb",
    "database": "dbname",
    "host": "localhost",
    "port": "27017"
  },

The instructions on Heroku are to use a process.env variable to dynamically assign the url value eg:

var mongoUri = process.env.MONGOLAB_URI ||
  process.env.MONGOHQ_URL ||
  'mongodb://localhost/mydb';

A pseudo work-around is to configure the LB datasource to use the 'url' property instead of database/host/port and then get Heroku to spit out the connection string https://devcenter.heroku.com/articles/mongolab#getting-your-connection-uri eg:

$ heroku config | grep MONGOLAB_URI
MONGOLAB_URI => mongodb://heroku_app1234:[email protected]:29017/heroku_app1234

and then reset the datasources.js to this:

  "mongodb": {
    "defaultForType": "mongodb",
    "connector": "loopback-connector-mongodb", 
    "url":"mongodb://heroku_app1234:[email protected]:29017/heroku_app1234"
  },

this kind of works but is a bit awkward. It would be good if I could just plug in my environment vars.

seanbrookes avatar Feb 09 '14 17:02 seanbrookes

In general configurabiliy for PaaSs needs work, there's a backlog item I think.

In the meanwhile perhaps I'll have Chandrika run thru and work with Rand on some additional tips in the documentation specifically for Heroku.

Thanks Sean!

  • Chanda

On Sunday, February 9, 2014, Sean Brookes [email protected] wrote:

the current LB datasource configuration makes it awkward to deploy a mongo (mongolab) db based app to Heroku.

LB datasources:

"mongodb": { "defaultForType": "mongodb", "connector": "loopback-connector-mongodb", "database": "dbname", "host": "localhost", "port": "27017" },

The instructions on Heroku are to use a process.env variable to dynamically assign the url value eg:

var mongoUri = process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || 'mongodb://localhost/mydb';

A pseudo work-around is to configure the LB datasource to use the 'url' property instead of database/host/port and then get Heroku to spit out the connection string https://devcenter.heroku.com/articles/mongolab#getting-your-connection-uri eg:

$ heroku config | grep MONGOLAB_URI MONGOLAB_URI => mongodb://heroku_app1234:[email protected]:29017/heroku_app1234

and then reset the datasources.js to this:

"mongodb": { "defaultForType": "mongodb", "connector": "loopback-connector-mongodb", "url":"mongodb://heroku_app1234:[email protected]:29017/heroku_app1234" },

this kind of works but is a bit awkward. It would be good if I could just plug in my environment vars.

Reply to this email directly or view it on GitHubhttps://github.com/strongloop/loopback/issues/174 .

ghost avatar Feb 09 '14 17:02 ghost

Correct. Environment configurability in general including PaaS support is on the backlog

Sent from my iPhone

On Feb 9, 2014, at 9:54 AM, chandadharap [email protected] wrote:

In general configurabiliy for PaaSs needs work, there's a backlog item I think.

In the meanwhile perhaps I'll have Chandrika run thru and work with Rand on some additional tips in the documentation specifically for Heroku.

Thanks Sean!

  • Chanda

On Sunday, February 9, 2014, Sean Brookes [email protected] wrote:

the current LB datasource configuration makes it awkward to deploy a mongo (mongolab) db based app to Heroku.

LB datasources:

"mongodb": { "defaultForType": "mongodb", "connector": "loopback-connector-mongodb", "database": "dbname", "host": "localhost", "port": "27017" },

The instructions on Heroku are to use a process.env variable to dynamically assign the url value eg:

var mongoUri = process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || 'mongodb://localhost/mydb';

A pseudo work-around is to configure the LB datasource to use the 'url' property instead of database/host/port and then get Heroku to spit out the connection string https://devcenter.heroku.com/articles/mongolab#getting-your-connection-uri eg:

$ heroku config | grep MONGOLAB_URI MONGOLAB_URI => mongodb://heroku_app1234:[email protected]:29017/heroku_app1234

and then reset the datasources.js to this:

"mongodb": { "defaultForType": "mongodb", "connector": "loopback-connector-mongodb", "url":"mongodb://heroku_app1234:[email protected]:29017/heroku_app1234" },

this kind of works but is a bit awkward. It would be good if I could just plug in my environment vars.

Reply to this email directly or view it on GitHubhttps://github.com/strongloop/loopback/issues/174 .

— Reply to this email directly or view it on GitHub.

altsang avatar Feb 09 '14 20:02 altsang

@seanbrookes You could write your datasources.json dynamically when starting your application:

var datasourcesWriter = require('./lib/datasources-writer');
datasourcesWriter.write();

Where datasourcesWriter.write();would read the correct values from process.env. Just make sure to put those lines before app.boot(__dirname);.

This module may help you: https://www.npmjs.org/package/12factor-config

albertoleal avatar May 13 '14 01:05 albertoleal

The solution I would like to implement in app.boot at some point in the near future is to support both datasources.json and datasources.js, probably with the .js file taking precedence.

That way there will be two options:

  • use datasources.json to declare the datasources statically
  • use datasources.js to configure the datasources dynamically using arbitrary javascript.

bajtos avatar May 14 '14 15:05 bajtos

This works for me. Something like this can be done with two different datasource files.

//datasources.json
{
  "mail": {
    "defaultForType": "mail",
    "connector": "mail"
  }
}
//app.js
var models            = require('./models.json'),
      dataSources   = require('./datasources.json');
if ('development' === env) {
  dataSources.db = {
    "connector": "mongodb",
    "host": "localhost",
    "port": 27017,
    "database": "db"
  };
} else {
  dataSources.db = {
      "connector": "mongodb",
      "database": "db",
      "host": "dsport_num.mongolab.com",
      "username": "user",
      "password": "password",
      "port": port_num
  };
}
app.boot({
  models: models,
  datasources: dataSources
});

BerkeleyTrue avatar Jun 19 '14 05:06 BerkeleyTrue

node-config works quite well.

I've been creating a config directory and placing four config files in it, sample.js, development.js, testing.js and production.js. Make sure to .gitignore all the config files except for sample.js and don't put real credentials in the sample file.

Now simply require config and the correct configuration will be loaded based on the environment.

var dbConfig = require('config').db;

dbConfig will now contain the following object.


{
    host: 'localhost'
    , port: 3306
    , database: 'db_name'
    , username: 'db_user'
    , password: 'db_password'
    , debug: false
}

RichAyotte avatar Jun 19 '14 22:06 RichAyotte

Wow! Node-config is awesome. Just switch over and it is a much cleaner way of doing things. Thanks!

BerkeleyTrue avatar Jun 21 '14 17:06 BerkeleyTrue

We are moving app.boot out of loopback to a standalone module loopback-boot.

The 1.x version is backwards compatible with app.boot and adds few more features, most notably support for dynamic configuration files (app.json, datasources.json).

See the loopback-boot/README and strongloop/loopback-boot@c1743dc.

The solution for this issue is to create a file datasources.local.js and put the following code there:


var mongoUri = process.env.MONGOLAB_URI ||
  process.env.MONGOHQ_URL ||
  'mongodb://localhost/mydb';

module.exports = {
  mongodb: {
    defaultForType: "mongodb",
    connector: "loopback-connector-mongodb", 
    url: mongoUrl
  }
}; 

In the future, I'd like to improve loopback-boot to automatically apply certain environment variables - see strongloop/loopback-boot#4.

Note: we are already working on a 2.0 version that uses a different project layout. Make sure to install 1.x series of loopback-boot in your project (unless you want to try out the new stuff).

bajtos avatar Jun 27 '14 17:06 bajtos

Great!!! No need of node-config anymore.

lchenay avatar Jun 27 '14 20:06 lchenay

@bajtos how should I configure my boot options in order to use datasources.local.js? I like this solution, but I still got this AssertionError: User is referencing a dataSource that does not exist: "pg" currently, I still use boot(app, __dirname); in my server.js as the loopback generator configures

hungtuchen avatar Apr 28 '15 13:04 hungtuchen

@transedward Run your app with DEBUG=loopback* and check the output for any messages that may explain what's happening under the hood. If that does not show you how to fix the problem, then I'll need a sample project reproducing the problem per bug reporting instructions.

bajtos avatar Apr 28 '15 14:04 bajtos

@bajtos , #1345

hungtuchen avatar Apr 28 '15 16:04 hungtuchen