How to configure StrongLoop LoopBack MongoDB datasource for deployment to Heroku

user2808320 picture user2808320 · Feb 8, 2014 · Viewed 7.9k times · Source

I'm using LoopBack ver. 1.6 and have a local mongoDB server running for development using he following datasource configuration:

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

Now I want to deploy to Heroku but I don't know how to configure the datasource to point at the MongoLab db since it has a dynamically generated connection string:

from the 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 kind of changes do I need to make to my datasource JSON to map the Heroku connection string?

Answer

kynan picture kynan · Nov 9, 2014

This has now (as of June 27 2014) been addressed: create a file datasources.local.js with the following content (where mongodb is your data source name):

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 (can be empty) and the .js overrides the configuration in the .json file.