Sanandrea Sanandrea - 1 month ago 18
Javascript Question

Strongloop app does not load local datasource

I want to use different environment specific datasource configurations in a Strongloop app. I saw at https://docs.strongloop.com/display/public/LB/Environment-specific+configuration that the priority of configurations are:


  1. Environment-specific configuration, based on the value of NODE_ENV;
    for example, server/config.staging.json.

  2. Local configuration file;
    for example, server/config.local.json.

  3. Default configuration file;
    for example, server/config.json.



I have declared three datasource conf files:
datasources.json:

{}


datasources.local.json:

{
"db": {
"name": "db",
"connector": "loopback-connector-mongodb",
"host":"127.0.0.1",
"port": "27017",
"database": "woowDev"
}
}


and datasources.staging.js:

module.exports = {
db: {
connector: 'mongodb',
hostname: process.env.OPENSHIFT_MONGODB_DB_HOST,
port: process.env.OPENSHIFT_MONGODB_DB_PORT,
user: process.env.OPENSHIFT_MONGODB_DB_USERNAME,
password: process.env.OPENSHIFT_MONGODB_DB_PASSWORD,
database: 'woow'
}
};


Now unless I put the configuration of datasources.local.json in datasources.json it does not work. I keep getting the error:
AssertionError: User is referencing a dataSource that does not exist: "db"


I tried also to add the local conf to staging conf and defined the variable NODE_ENV, but it would not load neither datasource.staging.js. I defined the NODE_ENV by doing:

export NODE_ENV=staging

Answer

I used node-debug to track down the issue. And it came in this particular source strongloop file:

node_modules/loopback-boot/lib/config-loader.js

the function:

function mergeDataSourceConfig(target, config, fileName) {
  for (var ds in target) {
    var err = applyCustomConfig(target[ds], config[ds]);
    if (err) {
      throw new Error('Cannot apply ' + fileName + ' to `'  + ds + '`: ' + err);
    }
  }
}

will not merge configs if "db" key is not defined in the master file i.e. datasources.json.

So, I just modified the datasources.json to:

{
  "db": {}
}

and it worked!

Maybe it is my fault but the documentation is not clear enough.

Comments