Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 14 additions & 27 deletions lib/config-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ConfigLoader.loadDataSources = function(rootDir, env) {
*/
ConfigLoader.loadModels = function(rootDir, env) {
/*jshint unused:false */
return tryReadJsonConfig(rootDir, 'model-config') || {};
return loadNamed(rootDir, env, 'model-config', mergeModelConfig);
};

/**
Expand Down Expand Up @@ -140,11 +140,16 @@ function mergeConfigurations(configObjects, mergeFn) {
}

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

function mergeModelConfig(target, config, fileName) {
var err = mergeObjects(target, config);
if (err) {
throw new Error('Cannot apply ' + fileName + ': ' + err);
}
}

Expand Down Expand Up @@ -182,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]);
if (err) {
throw new Error('Cannot apply ' + fileName + ' to `' + c + '`: ' + err);
}
var err = mergeObjects(target, config);
if (err) {
throw new Error('Cannot apply ' + fileName + ': ' + err);
}
}

Expand Down Expand Up @@ -250,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;
}
}
}
40 changes: 40 additions & 0 deletions test/compiler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down