How to configure StrongLoop LoopBack MongoDB datasource for deployment to Heroku

混江龙づ霸主 提交于 2019-12-03 13:32:33

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!