How to configure StrongLoop LoopBack MongoDB datasource for deployment to Heroku
Asked Answered
F

2

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?

Friedrich answered 8/2, 2014 at 20:39 Comment(0)
R
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.

Radke answered 9/2, 2014 at 6:6 Comment(1)
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 - looking forward to LoopBack support for env vars but this will work for now. thanks.Friedrich
F
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.

Funambulist answered 9/11, 2014 at 15:50 Comment(3)
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?Assumpsit
You still need the datasources.json, even if it is empty.Bowl
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.Oppression
R
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.

Radke answered 9/2, 2014 at 6:6 Comment(1)
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 - looking forward to LoopBack support for env vars but this will work for now. thanks.Friedrich

© 2022 - 2024 — McMap. All rights reserved.