support configuration of datasources/models from environment variables and other sources
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.
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 .
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.
@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
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.jsonto declare the datasources statically - use
datasources.jsto configure the datasources dynamically using arbitrary javascript.
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
});
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
}
Wow! Node-config is awesome. Just switch over and it is a much cleaner way of doing things. Thanks!
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).
Great!!! No need of node-config anymore.
@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
@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 , #1345