I want to use different environment specific datasource configurations in a Strongloop app. I saw at +configuration that the priority of configurations are:
- Environment-specific configuration, based on the value of NODE_ENV; for example, server/config.staging.json.
- Local configuration file; for example, server/config.local.json.
- 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
I want to use different environment specific datasource configurations in a Strongloop app. I saw at https://docs.strongloop./display/public/LB/Environment-specific+configuration that the priority of configurations are:
- Environment-specific configuration, based on the value of NODE_ENV; for example, server/config.staging.json.
- Local configuration file; for example, server/config.local.json.
- 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
Share
Improve this question
edited Feb 12, 2016 at 14:28
Sanandrea
asked Feb 12, 2016 at 14:04
SanandreaSanandrea
2,1941 gold badge27 silver badges46 bronze badges
8
-
Hmm... what if you delete the otherwise empty
datasources.json
file? – Jordan Kasper Commented Feb 12, 2016 at 14:13 - tried, does not work, what else can I try? – Sanandrea Commented Feb 12, 2016 at 14:14
-
Is it possible that your current environment is in fact
staging
? I see that your staging datasource config file does not have aname
property in the definition. It would need this regardless. – Jordan Kasper Commented Feb 12, 2016 at 14:21 -
Oh, and the connector should just be
mongodb
, I think. – Jordan Kasper Commented Feb 12, 2016 at 14:21 - 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.js – Sanandrea Commented Feb 12, 2016 at 14:22
2 Answers
Reset to default 9I 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.
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