Search code examples
angularjsnode.jsmongodbloopback

Dynamic datasource.json in loopback


I am using loopback with nodejs.

In my datasource.json file i have a connection Settings

"mongoConnector": {
    "host": "127.0.0.1",
    "port": 27017,
    "url": "",
    "database": "DB",
    "password": "",
    "name": "mongoConnector",
    "user": "",
    "connector": "mongodb"
  }

I have a global config json file which contains db connection settings like pwd, host ...

Is there any way to modify mongoConnector dynamically according to global config file.


Solution

  • You'll have to use a .js configuration file for this. You can create either a server/datasources.local.js (will always take precedence over other config files). Or you can use a NODE_ENV environment variable to specify a different suffix (i.e. datasources.{NODE_ENV}.js). In the file, just export an object that contains your configuration. You can use process.env.FOO to get environment variables. You could also require() your global file inside the .js config file, and pull from there. It's up to you.

    You can find some more info on the LB docs.