I'm trying to use the memory-connector as a datasource when doing integration testing. But it seems to always connect to the mongodb-datasource.
One major hack i have done is to change the datasource for each model to memory. But there must be a better way to do this. I'm running my tests from a gulp-task. My roflmao model-memory-hack:
var models = require('../server/model-config.json');
for (var key in models) {
var model = loopback.getModel(key);
loopback.configureModel(model, {dataSource: memory});
}
}
Is there any way to change the datasource for the app? Or do i have to change the datasource for each individual model..?
A way of doing this is to change the environment variable during testing, but so far, no luck.. I'm doing this with the gulp-task preprocess.
Hopefully by changing the environment variable, it would use datasources.integrationtesting.js, in which i have memory as a datasource.
My gulp-task:
return gulp.src('integration-tests/*.js')
.pipe($.preprocess({context: {NODE_ENV: 'integrationtesting'}}))
.pipe($.mocha())
I'm using:
Appreciate any comments.. : )
I think what you're looking for are environment-specific configuration files. Basically, you just create a datasource with the same name, but different implementations in different environments. Your datasources.json
file would be the default, but datasources.development.json
would be used if NODE_ENV
was set to development
.
From that linked page, you might have this in datasources.json:
{
db: {
connector: 'mongodb',
database: 'myapp',
user: 'myapp',
password: 'secret'
}
}
And this in datasources.development.json:
{
db: {
connector: 'memory'
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With