Strongloop app does not load local datasource
Asked Answered
S

2

10

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
Soot answered 12/2, 2016 at 14:4 Comment(8)
Hmm... what if you delete the otherwise empty datasources.json file?Lynelllynelle
tried, does not work, what else can I try?Soot
Is it possible that your current environment is in fact staging? I see that your staging datasource config file does not have a name property in the definition. It would need this regardless.Lynelllynelle
Oh, and the connector should just be mongodb, I think.Lynelllynelle
No, I tried to add the local conf to staging conf and defined the variable NODE_ENV, but it would not load neither datasource.staging.jsSoot
try to remove "module.exports = " from your datasoruces.staging.jsonWaldowaldon
Are you sure? it is a javascript file not jsonSoot
No, sorry misread the question.Waldowaldon
S
9

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.

Soot answered 12/2, 2016 at 15:2 Comment(2)
It is not really obvious the way it is written in the documentation, but indeed you can only override parameters, not create them in alternative configs You can **override** values that are **set** in config.json in: config.local.js or config.local.json config.env.js or config.env.json, where env is the value of NODE_ENVOdericus
I'm not so sure that this is exactly the case - I specify a port in config.local.js but not in config.json and the app starts on the expected port.Maddis
T
0

Trick is to add all the datasources(memory/redis/mongo/postgres) in datasources.json and then override parameters in datasources.local.js or datasources.staging.js or datasources.production.js

Sample file configuration:

datasources.json

{
  "db": {
    "name": "db",
    "connector": "memory"
  },
  "redisDS": {
    "name": "redisDS",
    "connector": "redis"
  },
  "testPostgress": {
    "port": 5432,
    "name": "localPostgress",
    "user": "akumar",
    "connector": "postgresql"
  }
}

datasources.staging.js

module.exports = {
  db:{
    connector: 'memory'
  },
  redisDS:{
    connector: 'redis'
  },
  testPostgress:{
    database:'stagingPostgress'
  }
};

Loopback will override database name in this case similarly you can override other datasource parameters like port and user

Tubulate answered 12/10, 2016 at 20:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.