7

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?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
user2808320
  • 89
  • 1
  • 3

2 Answers2

7

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.

kynan
  • 13,235
  • 6
  • 79
  • 81
  • 1
    I've removed datasources.json and added a datasources.local.js file as follows: `var postgresURI = process.env.DATABASE_URL; module.exports = { db: { defaultForType: 'postgresql', connector: 'postgresql', url: postgresURI } };` In model-config.json I have "dataSource": "db" set for all sections. It throws the following error: `User is referencing a dataSource that does not exist: "db"`. I understand the error but why it's being thrown. What am I missing? – Lee Apr 03 '15 at 07:15
  • 2
    You still need the datasources.json, even if it is empty. – mancvso Apr 10 '15 at 02:25
  • 2
    I spent hours before figuring out that a .js is only an addition to the json configuration. So the json gets loaded, and then you can use .js files to overwrite values. – Arne Jun 26 '15 at 07:32
5

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

Related to your question, LoopBack allows you to configure the datasource using a '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 an issue at https://github.com/strongloop/loopback/issues.

Raymond Feng
  • 1,516
  • 9
  • 5
  • Thanks Raymond, the url property was the key. I was able to generate the mongolab connection url from the Heroku docs: [Getting your connection UR](https://devcenter.heroku.com/articles/mongolab#getting-your-connection-uri) - looking forward to LoopBack support for env vars but this will work for now. thanks. – user2808320 Feb 09 '14 at 17:20