Fix the model-config/datasource merge

This commit is contained in:
Raymond Feng 2015-07-31 10:01:53 -07:00
parent ab723b5490
commit 02c826337c
2 changed files with 49 additions and 31 deletions

View File

@ -140,20 +140,16 @@ function mergeConfigurations(configObjects, mergeFn) {
}
function mergeDataSourceConfig(target, config, fileName) {
for (var ds in target) {
var err = mergeObjects(target[ds], config[ds]);
var err = mergeObjects(target, config);
if (err) {
throw new Error('Cannot apply ' + fileName + ' to `' + ds + '`: ' + err);
}
throw new Error('Cannot apply ' + fileName + ': ' + err);
}
}
function mergeModelConfig(target, config, fileName) {
for (var mc in target) {
var err = mergeObjects(target[mc], config[mc]);
var err = mergeObjects(target, config);
if (err) {
throw new Error('Cannot apply ' + fileName + ' to `' + mc + '`: ' + err);
}
throw new Error('Cannot apply ' + fileName + ': ' + err);
}
}
@ -191,11 +187,9 @@ function mergePhaseConfig(target, config, phase) {
}
function mergeComponentConfig(target, config, fileName) {
for (var c in target) {
var err = mergeObjects(target[c], config[c]);
var err = mergeObjects(target, config);
if (err) {
throw new Error('Cannot apply ' + fileName + ' to `' + c + '`: ' + err);
}
throw new Error('Cannot apply ' + fileName + ': ' + err);
}
}
@ -259,19 +253,3 @@ function hasCompatibleType(origValue, newValue) {
// we don't need to explicitly check array types
return typeof newValue !== 'object';
}
/**
* Try to read a config file with .json extension
* @param {string} cwd Dirname of the file
* @param {string} fileName Name of the file without extension
* @returns {Object|undefined} Content of the file, undefined if not found.
*/
function tryReadJsonConfig(cwd, fileName) {
try {
return require(path.join(cwd, fileName + '.json'));
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
}
}

View File

@ -417,6 +417,46 @@ describe('compiler', function() {
expect(db.nested).to.eql(arrayValue);
});
it('allows env specific model-config json', function() {
appdir.createConfigFilesSync();
appdir.writeConfigFileSync('model-config.local.json', {
foo: { dataSource: 'db' }
});
var instructions = boot.compile(appdir.PATH);
expect(instructions.models).to.have.length(1);
expect(instructions.models[0]).to.have.property('name', 'foo');
});
it('allows env specific model-config json to be merged', function() {
appdir.createConfigFilesSync(null, null,
{foo: {dataSource: 'mongo', public: false}});
appdir.writeConfigFileSync('model-config.local.json', {
foo: {dataSource: 'db'}
});
var instructions = boot.compile(appdir.PATH);
expect(instructions.models).to.have.length(1);
expect(instructions.models[0]).to.have.property('name', 'foo');
expect(instructions.models[0].config).to.eql({
dataSource: 'db',
public: false
});
});
it('allows env specific model-config js', function() {
appdir.createConfigFilesSync();
appdir.writeFileSync('model-config.local.js',
'module.exports = { foo: { dataSource: \'db\' } };');
var instructions = boot.compile(appdir.PATH);
expect(instructions.models).to.have.length(1);
expect(instructions.models[0]).to.have.property('name', 'foo');
});
it('refuses to merge Array properties of different length', function() {
appdir.createConfigFilesSync({
nest: {