How to configure StrongLoop LoopBack MongoDB data source for deployment in Heroku

I am using LoopBack ver. 1.6 and have a local mongoDB server for development using the following data source configuration:

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

Now I want to deploy to Heroku, but I do not know how to configure the data source to point to MongoLab db, since it has a dynamically generated connection string:

from Heroku dox:

 var mongo = require('mongodb'); var mongoUri = process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || 'mongodb://localhost/mydb'; mongo.Db.connect(mongoUri, function (err, db) { db.collection('mydocs', function(er, collection) { collection.insert({'mykey': 'myvalue'}, {safe: true}, function(er,rs) { }); }); }); 

So, what changes do I need to make for my JSON data source to match Heroku connection string?

+7
javascript mongodb heroku loopbackjs strongloop
source share
2 answers

This is TODO for LoopBack to support the configuration of data sources / models from environment variables and other sources. One idea is to use the template engine to load datasources.json so that it can have variables that must be resolved at runtime.

In connection with your question, LoopBack allows you to configure the data source using the "url" property. For example:

 { "connector": "loopback-connector-mongodb", "url": "mongodb://localhost:27017/mydb" } 

As a workaround, you can write a post-deployment script for Heroku to replace the url value with process.env.MONGOLAB_URI or process.env.MONGOHQ_URL.

 sed -i.bak s/MONGODB_URL/$MONGOHQ_URL/g datasources.json 

Meanwhile, please open the question at https://github.com/strongloop/loopback/issues .

+5
source share

Currently (as of June 27, 2014) it is addressed : create a datasources.local.js file with the following contents (where mongodb name of the data source):

 var mongoUri = process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || 'mongodb://localhost/mydb'; module.exports = { mongodb: { defaultForType: "mongodb", connector: "loopback-connector-mongodb", url: mongoUri } }; 

Note. datasources.json is still required (may be empty), and .js overrides the configuration in the .json file.

+7
source share

All Articles