From 39d820a6579fe983522e9bd0d5c52d813bb49e23 Mon Sep 17 00:00:00 2001 From: Pradnya Baviskar Date: Tue, 7 Apr 2015 17:51:05 +0530 Subject: [PATCH] Use filename as default value for Model name --- lib/compiler.js | 3 + package.json | 3 +- test/compiler.test.js | 138 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 1 deletion(-) diff --git a/lib/compiler.js b/lib/compiler.js index 30ad2d0..e2dea15 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -7,6 +7,7 @@ var ConfigLoader = require('./config-loader'); var debug = require('debug')('loopback:boot:compiler'); var Module = require('module'); var _ = require('lodash'); +var _s = require('underscore.string'); var FILE_EXTENSION_JSON = '.json'; @@ -382,6 +383,8 @@ function tryResolveAppPath(rootDir, relativePath, resolveOptions) { function loadModelDefinition(rootDir, jsonFile, allFiles) { 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` var sourceFile = fixFileExtension(jsonFile, allFiles, true); diff --git a/package.json b/package.json index b198c18..c2df7b5 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ "debug": "^2.0.0", "lodash": "^2.4.1", "semver": "^4.1.0", - "toposort": "^0.2.10" + "toposort": "^0.2.10", + "underscore.string": "^3.0.3" }, "devDependencies": { "browserify": "^4.1.8", diff --git a/test/compiler.test.js b/test/compiler.test.js index cc3a5c2..6df170f 100644 --- a/test/compiler.test.js +++ b/test/compiler.test.js @@ -867,6 +867,144 @@ describe('compiler', function() { .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() { appdir.createConfigFilesSync();