Merge pull request #118 from PradnyaBaviskar/lb-boot-issue-42

Use filename as default value for Model name
This commit is contained in:
Miroslav Bajtoš 2015-04-08 10:50:03 +02:00
commit 1b9f137062
3 changed files with 143 additions and 1 deletions

View File

@ -7,6 +7,7 @@ var ConfigLoader = require('./config-loader');
var debug = require('debug')('loopback:boot:compiler'); var debug = require('debug')('loopback:boot:compiler');
var Module = require('module'); var Module = require('module');
var _ = require('lodash'); var _ = require('lodash');
var _s = require('underscore.string');
var FILE_EXTENSION_JSON = '.json'; var FILE_EXTENSION_JSON = '.json';
@ -382,6 +383,8 @@ function tryResolveAppPath(rootDir, relativePath, resolveOptions) {
function loadModelDefinition(rootDir, jsonFile, allFiles) { function loadModelDefinition(rootDir, jsonFile, allFiles) {
var definition = require(jsonFile); var definition = require(jsonFile);
var basename = path.basename(jsonFile, path.extname(jsonFile));
definition.name = definition.name || _s.capitalize(_s.camelize(basename));
// find a matching file with a supported extension like `.js` or `.coffee` // find a matching file with a supported extension like `.js` or `.coffee`
var sourceFile = fixFileExtension(jsonFile, allFiles, true); var sourceFile = fixFileExtension(jsonFile, allFiles, true);

View File

@ -28,7 +28,8 @@
"debug": "^2.0.0", "debug": "^2.0.0",
"lodash": "^2.4.1", "lodash": "^2.4.1",
"semver": "^4.1.0", "semver": "^4.1.0",
"toposort": "^0.2.10" "toposort": "^0.2.10",
"underscore.string": "^3.0.3"
}, },
"devDependencies": { "devDependencies": {
"browserify": "^4.1.8", "browserify": "^4.1.8",

View File

@ -867,6 +867,144 @@ describe('compiler', function() {
.to.throw(/cyclic dependency/i); .to.throw(/cyclic dependency/i);
}); });
it('uses file name as default value for model name', function() {
appdir.createConfigFilesSync({}, {}, {
Car: { dataSource: 'db' }
});
appdir.writeConfigFileSync('models/car.json', {});
var instructions = boot.compile(appdir.PATH);
var modelNames = instructions.models.map(getNameProperty);
expect(modelNames).to.eql(['Car']);
});
it('uses `OrderItem` as default model name for file with name `order-item`',
function() {
appdir.createConfigFilesSync({}, {}, {
OrderItem: { dataSource: 'db' }
});
appdir.writeConfigFileSync('models/order-item.json', {});
var instructions = boot.compile(appdir.PATH);
var modelNames = instructions.models.map(getNameProperty);
expect(modelNames).to.eql(['OrderItem']);
});
it('uses `OrderItem` as default model name for file with name `order_item`',
function() {
appdir.createConfigFilesSync({}, {}, {
OrderItem: { dataSource: 'db' }
});
appdir.writeConfigFileSync('models/order_item.json', {});
var instructions = boot.compile(appdir.PATH);
var modelNames = instructions.models.map(getNameProperty);
expect(modelNames).to.eql(['OrderItem']);
});
it('uses `OrderItem` as default model name for file with name `order item`',
function() {
appdir.createConfigFilesSync({}, {}, {
OrderItem: { dataSource: 'db' }
});
appdir.writeConfigFileSync('models/order item.json', {});
var instructions = boot.compile(appdir.PATH);
var modelNames = instructions.models.map(getNameProperty);
expect(modelNames).to.eql(['OrderItem']);
});
it('overrides `default model name` by `name` in model definition',
function() {
appdir.createConfigFilesSync({}, {}, {
overrideCar: { dataSource: 'db' }
});
appdir.writeConfigFileSync('models/car.json', { name: 'overrideCar'});
var instructions = boot.compile(appdir.PATH);
var modelNames = instructions.models.map(getNameProperty);
expect(modelNames).to.eql(['overrideCar']);
});
it('overwrites model with same default name', function() {
appdir.createConfigFilesSync({}, {}, {
'OrderItem': { dataSource: 'db' }
});
appdir.writeConfigFileSync('models/order-item.json', {
properties: {
price: { type: 'number' }
}
});
appdir.writeFileSync('models/order-item.js', '');
appdir.writeConfigFileSync('models/orderItem.json', {
properties: {
quantity: { type: 'number' }
}
});
var appJS = appdir.writeFileSync('models/orderItem.js', '');
var instructions = boot.compile(appdir.PATH);
expect(instructions.models).to.eql([{
name: 'OrderItem',
config: {
dataSource: 'db'
},
definition: {
name: 'OrderItem',
properties: {
quantity: {type: 'number'}
}
},
sourceFile: appJS
}]);
});
it('overwrites model with same name in model definition', function() {
appdir.createConfigFilesSync({}, {}, {
'customOrder': { dataSource: 'db' }
});
appdir.writeConfigFileSync('models/order1.json', {
name : 'customOrder',
properties: {
price: { type: 'number' }
}
});
appdir.writeFileSync('models/order1.js', '');
appdir.writeConfigFileSync('models/order2.json', {
name : 'customOrder',
properties: {
quantity: { type: 'number' }
}
});
var appJS = appdir.writeFileSync('models/order2.js', '');
var instructions = boot.compile(appdir.PATH);
expect(instructions.models).to.eql([{
name: 'customOrder',
config: {
dataSource: 'db'
},
definition: {
name: 'customOrder',
properties: {
quantity: {type: 'number'}
}
},
sourceFile: appJS
}]);
});
it('returns a new copy of JSON data', function() { it('returns a new copy of JSON data', function() {
appdir.createConfigFilesSync(); appdir.createConfigFilesSync();