From d9d0e0297bf8007649778e25729d4b7d8077921c Mon Sep 17 00:00:00 2001 From: Shlomi Assaf Date: Tue, 24 Feb 2015 21:18:08 +0200 Subject: [PATCH 1/6] Extend options arg to support custom model definitions Extend the options argument of boot/compile to allow the developer to supply custom model definitions (to replace the result of findModelDefinitions). Together with the existing options.models, it will allow you to specify all model-related data and still use the benefits which loopback-boot provides (most notably loading models in order defined by inheritance, i.e. base models are loaded before models that are extending them). --- lib/compiler.js | 38 +++++++++++++++++-- test/compiler.test.js | 38 ++++++++++++++++++- .../common/models/foo-bar-bat-baz-with-def.js | 3 ++ 3 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/simple-app/common/models/foo-bar-bat-baz-with-def.js diff --git a/lib/compiler.js b/lib/compiler.js index 0a3ef7d..6c32391 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -76,8 +76,10 @@ module.exports = function compile(options) { delete modelsConfig._meta; var modelSources = options.modelSources || modelsMeta.sources || ['./models']; + var modelDefinitions = options.modelDefinitions || undefined; + var modelInstructions = buildAllModelInstructions( - modelsRootDir, modelsConfig, modelSources); + modelsRootDir, modelsConfig, modelSources, modelDefinitions); // When executor passes the instruction to loopback methods, // loopback modifies the data. Since we are loading the data using `require`, @@ -181,8 +183,8 @@ function tryReadDir() { } } -function buildAllModelInstructions(rootDir, modelsConfig, sources) { - var registry = findModelDefinitions(rootDir, sources); +function buildAllModelInstructions(rootDir, modelsConfig, sources, modelDefinitions) { + var registry = verifyModelDefinitions(rootDir, modelDefinitions) || findModelDefinitions(rootDir, sources); var modelNamesToBuild = addAllBaseModels(registry, Object.keys(modelsConfig)); @@ -263,6 +265,36 @@ function sortByInheritance(instructions) { }); } +function verifyModelDefinitions(rootDir, modelDefinitions) { + if (!modelDefinitions || modelDefinitions.length < 1) { + return undefined; + } + + var registry = {}; + modelDefinitions.forEach(function(definition, idx) { + try { + var fullPath = path.resolve(rootDir, definition.sourceFile); + definition.sourceFile = require.resolve(fullPath); + } catch (err) { + debug('Model source code not found: %s - %s', definition.sourceFile, err.code || err); + definition.sourceFile = undefined; + } + + debug('Found model "%s" - %s %s', definition.definition.name, + 'from options', + definition.sourceFile ? path.relative(rootDir, definition.sourceFile) : '(no source file)'); + + var modelName = definition.definition.name; + if (!modelName) { + debug('Skipping model definition without Model name (from options.modelDefinitions @ index %s)', idx); + return; + } + registry[modelName] = definition; + }); + + return registry; +} + function findModelDefinitions(rootDir, sources) { var registry = {}; diff --git a/test/compiler.test.js b/test/compiler.test.js index e7407c7..97760fb 100644 --- a/test/compiler.test.js +++ b/test/compiler.test.js @@ -28,8 +28,34 @@ describe('compiler', function() { models: { 'foo-bar-bat-baz': { dataSource: 'the-db' + }, + 'foo-bar-bat-baz-with-def': { + dataSource: 'the-db' + }, + 'foo-bar-bat-baz-with-def-no-source': { + dataSource: 'the-db' } }, + modelDefinitions: [ + { + definition: { + name: 'foo-bar-bat-baz-with-def' + }, + sourceFile: path.join(SIMPLE_APP, 'common', 'models', 'foo-bar-bat-baz-with-def.js') + }, + { + definition: { // sourceFile is a not existing file. + name: 'foo-bar-bat-baz-with-def-no-source' + }, + sourceFile: path.join(SIMPLE_APP, 'common', 'models', 'foo-bar-bat-baz-with-def!!!.js') + }, + { + definition: { + name: 'i-should-not-exist' //not in 'models' + }, + sourceFile: path.join(SIMPLE_APP, 'common', 'models', 'i-should-not-exist.js') + } + ], dataSources: { 'the-db': { connector: 'memory', @@ -61,7 +87,7 @@ describe('compiler', function() { }); it('has models definition', function() { - expect(instructions.models).to.have.length(1); + expect(instructions.models).to.have.length(3); expect(instructions.models[0]).to.eql({ name: 'foo-bar-bat-baz', config: { @@ -72,6 +98,16 @@ describe('compiler', function() { }); }); + it('load model definitions from object', function() { + expect(instructions.models[1].name).to.equal('foo-bar-bat-baz-with-def'); + expect(instructions.models[1].definition).not.to.equal(undefined); + expect(instructions.models[1].sourceFile).not.to.equal(undefined); + + expect(instructions.models[2].name).to.equal('foo-bar-bat-baz-with-def-no-source'); + expect(instructions.models[2].definition).not.to.equal(undefined); + expect(instructions.models[2].sourceFile).to.equal(undefined); + }); + it('has datasources definition', function() { expect(instructions.dataSources).to.eql(options.dataSources); }); diff --git a/test/fixtures/simple-app/common/models/foo-bar-bat-baz-with-def.js b/test/fixtures/simple-app/common/models/foo-bar-bat-baz-with-def.js new file mode 100644 index 0000000..5e8b965 --- /dev/null +++ b/test/fixtures/simple-app/common/models/foo-bar-bat-baz-with-def.js @@ -0,0 +1,3 @@ +module.exports = function (fooBarBatBazWithDef) { + fooBarBatBazWithDef.loaded = true; +}; From ea54f9bde7cf9ad7206eb0f3adfffab67cb1ecb1 Mon Sep 17 00:00:00 2001 From: Shlomi Assaf Date: Thu, 12 Mar 2015 01:38:40 +0200 Subject: [PATCH 2/6] test additions. --- test/compiler.test.js | 108 +++++++++++++++++++++++++++--------------- 1 file changed, 71 insertions(+), 37 deletions(-) diff --git a/test/compiler.test.js b/test/compiler.test.js index 97760fb..cb379cb 100644 --- a/test/compiler.test.js +++ b/test/compiler.test.js @@ -28,34 +28,8 @@ describe('compiler', function() { models: { 'foo-bar-bat-baz': { dataSource: 'the-db' - }, - 'foo-bar-bat-baz-with-def': { - dataSource: 'the-db' - }, - 'foo-bar-bat-baz-with-def-no-source': { - dataSource: 'the-db' } }, - modelDefinitions: [ - { - definition: { - name: 'foo-bar-bat-baz-with-def' - }, - sourceFile: path.join(SIMPLE_APP, 'common', 'models', 'foo-bar-bat-baz-with-def.js') - }, - { - definition: { // sourceFile is a not existing file. - name: 'foo-bar-bat-baz-with-def-no-source' - }, - sourceFile: path.join(SIMPLE_APP, 'common', 'models', 'foo-bar-bat-baz-with-def!!!.js') - }, - { - definition: { - name: 'i-should-not-exist' //not in 'models' - }, - sourceFile: path.join(SIMPLE_APP, 'common', 'models', 'i-should-not-exist.js') - } - ], dataSources: { 'the-db': { connector: 'memory', @@ -87,7 +61,7 @@ describe('compiler', function() { }); it('has models definition', function() { - expect(instructions.models).to.have.length(3); + expect(instructions.models).to.have.length(1); expect(instructions.models[0]).to.eql({ name: 'foo-bar-bat-baz', config: { @@ -98,19 +72,79 @@ describe('compiler', function() { }); }); - it('load model definitions from object', function() { - expect(instructions.models[1].name).to.equal('foo-bar-bat-baz-with-def'); - expect(instructions.models[1].definition).not.to.equal(undefined); - expect(instructions.models[1].sourceFile).not.to.equal(undefined); - - expect(instructions.models[2].name).to.equal('foo-bar-bat-baz-with-def-no-source'); - expect(instructions.models[2].definition).not.to.equal(undefined); - expect(instructions.models[2].sourceFile).to.equal(undefined); - }); - it('has datasources definition', function() { expect(instructions.dataSources).to.eql(options.dataSources); }); + + describe('with custom model definitions', function() { + var instructionCustom; + + beforeEach(function(){ + appdir.writeFileSync('custom-models/model-with-definitions.js', ''); + instructionCustom = boot.compile({ + appRootDir: appdir.PATH, + models: { + 'model-without-definitions': { + dataSource: 'the-db' + }, + 'model-with-definitions': { + dataSource: 'the-db' + }, + 'model-with-definitions-without-source-file': { + dataSource: 'the-db' + } + }, + modelDefinitions: [ + { + definition: { + name: 'model-with-definitions' + }, + sourceFile: path.join(appdir.PATH, 'custom-models', 'model-with-definitions.js') + }, + { + definition: { // sourceFile is a not existing file. + name: 'model-with-definitions-without-source-file' + }, + sourceFile: path.join(appdir.PATH, 'custom-models', 'model-with-definitions-without-source-file.js') + }, + { + definition: { + name: 'model-that-should-not-exist' //not in 'models' + }, + sourceFile: path.join(appdir.PATH, 'custom-models', 'i-should-not-exist.js') + } + ], + dataSources: { + 'the-db': { + connector: 'memory', + defaultForType: 'db' + } + } + }); + }); + + it('should loads models defined in `models` only.', function() { + expect(instructionCustom.models).to.have.length(3); + }); + + it('should loads model without definition.', function() { + expect(instructionCustom.models[0].name).to.equal('model-without-definitions'); + expect(instructionCustom.models[0].definition).to.equal(undefined); + expect(instructionCustom.models[0].sourceFile).to.equal(undefined); + }); + + it('should set source file path if the file exist.', function() { + expect(instructionCustom.models[1].name).to.equal('model-with-definitions'); + expect(instructionCustom.models[1].definition).not.to.equal(undefined); + expect(instructionCustom.models[1].sourceFile).not.to.equal(undefined); + }); + + it('should not set source file path if the file does not exist.', function() { + expect(instructionCustom.models[2].name).to.equal('model-with-definitions-without-source-file'); + expect(instructionCustom.models[2].definition).not.to.equal(undefined); + expect(instructionCustom.models[2].sourceFile).to.equal(undefined); + }); + }); }); describe('from directory', function() { From e1f474b03bf609eca79948796c1fae033d1a636f Mon Sep 17 00:00:00 2001 From: Shlomi Assaf Date: Thu, 12 Mar 2015 01:44:38 +0200 Subject: [PATCH 3/6] removed a test file that is not needed anymore. --- .../simple-app/common/models/foo-bar-bat-baz-with-def.js | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 test/fixtures/simple-app/common/models/foo-bar-bat-baz-with-def.js diff --git a/test/fixtures/simple-app/common/models/foo-bar-bat-baz-with-def.js b/test/fixtures/simple-app/common/models/foo-bar-bat-baz-with-def.js deleted file mode 100644 index 5e8b965..0000000 --- a/test/fixtures/simple-app/common/models/foo-bar-bat-baz-with-def.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = function (fooBarBatBazWithDef) { - fooBarBatBazWithDef.loaded = true; -}; From 24c44862ba6f02c85d7ba6e687cc613db766493b Mon Sep 17 00:00:00 2001 From: Shlomi Assaf Date: Thu, 12 Mar 2015 02:07:17 +0200 Subject: [PATCH 4/6] more test separation. --- test/compiler.test.js | 130 +++++++++++++++++++++++++++++------------- 1 file changed, 90 insertions(+), 40 deletions(-) diff --git a/test/compiler.test.js b/test/compiler.test.js index cb379cb..4bd285e 100644 --- a/test/compiler.test.js +++ b/test/compiler.test.js @@ -77,21 +77,36 @@ describe('compiler', function() { }); describe('with custom model definitions', function() { - var instructionCustom; + var dataSources = { + 'the-db': { + connector: 'memory', + defaultForType: 'db' + } + }; - beforeEach(function(){ - appdir.writeFileSync('custom-models/model-with-definitions.js', ''); - instructionCustom = boot.compile({ + it('should loads model without definition.', function() { + var instruction = boot.compile({ appRootDir: appdir.PATH, models: { 'model-without-definitions': { dataSource: 'the-db' - }, + } + }, + modelDefinitions: [], + dataSources: dataSources + }); + expect(instruction.models[0].name).to.equal('model-without-definitions'); + expect(instruction.models[0].definition).to.equal(undefined); + expect(instruction.models[0].sourceFile).to.equal(undefined); + }); + + it('should set source file path if the file exist.', function() { + appdir.writeFileSync('custom-models/model-with-definitions.js', ''); + var instruction = boot.compile({ + appRootDir: appdir.PATH, + models: { 'model-with-definitions': { dataSource: 'the-db' - }, - 'model-with-definitions-without-source-file': { - dataSource: 'the-db' } }, modelDefinitions: [ @@ -100,49 +115,84 @@ describe('compiler', function() { name: 'model-with-definitions' }, sourceFile: path.join(appdir.PATH, 'custom-models', 'model-with-definitions.js') - }, + } + ], + dataSources: dataSources + }); + expect(instruction.models[0].name).to.equal('model-with-definitions'); + expect(instruction.models[0].definition).not.to.equal(undefined); + expect(instruction.models[0].sourceFile).not.to.equal(undefined); + }); + + it('should not set source file path if the file does not exist.', function() { + var instruction = boot.compile({ + appRootDir: appdir.PATH, + models: { + 'model-with-definitions-with-falsey-source-file': { + dataSource: 'the-db' + } + }, + modelDefinitions: [ { definition: { // sourceFile is a not existing file. - name: 'model-with-definitions-without-source-file' + name: 'model-with-definitions-with-falsey-source-file' }, - sourceFile: path.join(appdir.PATH, 'custom-models', 'model-with-definitions-without-source-file.js') - }, - { - definition: { - name: 'model-that-should-not-exist' //not in 'models' - }, - sourceFile: path.join(appdir.PATH, 'custom-models', 'i-should-not-exist.js') + sourceFile: path.join(appdir.PATH, 'custom-models', 'model-with-definitions-with-falsey-source-file.js') } ], - dataSources: { - 'the-db': { - connector: 'memory', - defaultForType: 'db' - } - } + dataSources: dataSources }); + expect(instruction.models[0].name).to.equal('model-with-definitions-with-falsey-source-file'); + expect(instruction.models[0].definition).not.to.equal(undefined); + expect(instruction.models[0].sourceFile).to.equal(undefined); }); - it('should loads models defined in `models` only.', function() { - expect(instructionCustom.models).to.have.length(3); - }); - - it('should loads model without definition.', function() { - expect(instructionCustom.models[0].name).to.equal('model-without-definitions'); - expect(instructionCustom.models[0].definition).to.equal(undefined); - expect(instructionCustom.models[0].sourceFile).to.equal(undefined); + it('should not set source file path if no source file supplied.', function() { + var instruction = boot.compile({ + appRootDir: appdir.PATH, + models: { + 'model-with-definitions-without-source-file-property': { + dataSource: 'the-db' + } + }, + modelDefinitions: [ + { + definition: { // sourceFile is a not existing file. + name: 'model-with-definitions-without-source-file-property' + } + } + ], + dataSources: dataSources + }); + expect(instruction.models[0].name).to.equal('model-with-definitions-without-source-file-property'); + expect(instruction.models[0].definition).not.to.equal(undefined); + expect(instruction.models[0].sourceFile).to.equal(undefined); }); - it('should set source file path if the file exist.', function() { - expect(instructionCustom.models[1].name).to.equal('model-with-definitions'); - expect(instructionCustom.models[1].definition).not.to.equal(undefined); - expect(instructionCustom.models[1].sourceFile).not.to.equal(undefined); - }); + it('should loads models defined in `models` only.', function() { + var instruction = boot.compile({ + appRootDir: appdir.PATH, + models: { + 'some-model': { + dataSource: 'the-db' + } + }, + modelDefinitions: [ + { + definition: { + name: 'some-model' + } + }, + { + definition: { + name: 'some-fictional-model' + } + } + ], + dataSources: dataSources + }); - it('should not set source file path if the file does not exist.', function() { - expect(instructionCustom.models[2].name).to.equal('model-with-definitions-without-source-file'); - expect(instructionCustom.models[2].definition).not.to.equal(undefined); - expect(instructionCustom.models[2].sourceFile).to.equal(undefined); + expect(instruction.models).to.have.length(1); }); }); }); From d783af03af02bb870f58d9c2259c13e42e52f1a5 Mon Sep 17 00:00:00 2001 From: Shlomi Assaf Date: Wed, 1 Apr 2015 20:28:51 +0300 Subject: [PATCH 5/6] Added support for more sourceFile extensions (coffeescript). Added support for sourceFile handling that has no extension. --- lib/compiler.js | 38 ++++++++++++++++-------- test/compiler.test.js | 69 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 94 insertions(+), 13 deletions(-) diff --git a/lib/compiler.js b/lib/compiler.js index 6c32391..74e676b 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -265,6 +265,24 @@ function sortByInheritance(instructions) { }); } +/** + * Returns matching files with a supported `sourceFile` extension like `.js` or `.coffee` + * @param allFiles {Array} + * @param basename {string} + * @returns {Array} + */ +function filterValidSourceFiles(allFiles, basename) { + var base, ext, validFileType; + return allFiles + .filter(function(f) { + ext = path.extname(f); + base = path.basename(f, ext); + validFileType = (ext !== '.node') && (ext !== '.json') && + ((typeof require.extensions[ext]) === 'function'); + return validFileType && (base === basename); + }); +} + function verifyModelDefinitions(rootDir, modelDefinitions) { if (!modelDefinitions || modelDefinitions.length < 1) { return undefined; @@ -274,7 +292,13 @@ function verifyModelDefinitions(rootDir, modelDefinitions) { modelDefinitions.forEach(function(definition, idx) { try { var fullPath = path.resolve(rootDir, definition.sourceFile); - definition.sourceFile = require.resolve(fullPath); + var basename = path.basename(fullPath, path.extname(fullPath)); + var files = tryReadDir(path.dirname(fullPath)); + var sourceFile = filterValidSourceFiles(files, basename)[0]; + + definition.sourceFile = path.join(path.dirname(fullPath), sourceFile); + definition.sourceFile = require.resolve(definition.sourceFile); + } catch (err) { debug('Model source code not found: %s - %s', definition.sourceFile, err.code || err); definition.sourceFile = undefined; @@ -362,17 +386,7 @@ function loadModelDefinition(rootDir, jsonFile, allFiles) { var basename = path.basename(jsonFile, path.extname(jsonFile)); // find a matching file with a supported extension like `.js` or `.coffee` - var base; - var ext; - var validFileType; - var sourceFile = allFiles - .filter(function(f) { - ext = path.extname(f); - base = path.basename(f, ext); - validFileType = (ext !== '.node') && (ext !== '.json') && - ((typeof require.extensions[ext]) === 'function'); - return validFileType && (base === basename); - })[0]; + var sourceFile = filterValidSourceFiles(allFiles, basename)[0]; try { sourceFile = path.join(path.dirname(jsonFile), sourceFile); diff --git a/test/compiler.test.js b/test/compiler.test.js index 4bd285e..e59a00f 100644 --- a/test/compiler.test.js +++ b/test/compiler.test.js @@ -100,6 +100,72 @@ describe('compiler', function() { expect(instruction.models[0].sourceFile).to.equal(undefined); }); + it('should load coffeescript models', function() { + require('coffee-script/register'); + + appdir.writeFileSync('custom-models/coffee-model-with-definitions.coffee', ''); + var instruction = boot.compile({ + appRootDir: appdir.PATH, + models: { + 'coffee-model-with-definitions': { + dataSource: 'the-db' + } + }, + modelDefinitions: [ + { + definition: { + name: 'coffee-model-with-definitions' + }, + sourceFile: path.join(appdir.PATH, 'custom-models', 'coffee-model-with-definitions.coffee') + } + ], + dataSources: dataSources + }); + expect(instruction.models[0].name).to.equal('coffee-model-with-definitions'); + expect(instruction.models[0].definition).not.to.equal(undefined); + expect(instruction.models[0].sourceFile).not.to.equal(undefined); + }); + + it('should handle sourceFile path without extension.', function() { + require('coffee-script/register'); + + appdir.writeFileSync('custom-models/coffee-model-without-ext.coffee', ''); + appdir.writeFileSync('custom-models/js-model-without-ext.js', ''); + var instruction = boot.compile({ + appRootDir: appdir.PATH, + models: { + 'coffee-model-without-ext': { + dataSource: 'the-db' + }, + 'js-model-without-ext': { + dataSource: 'the-db' + } + }, + modelDefinitions: [ + { + definition: { + name: 'coffee-model-without-ext' + }, + sourceFile: path.join(appdir.PATH, 'custom-models', 'coffee-model-without-ext') + }, + { + definition: { + name: 'js-model-without-ext' + }, + sourceFile: path.join(appdir.PATH, 'custom-models', 'js-model-without-ext') + } + ], + dataSources: dataSources + }); + expect(instruction.models[0].name).to.equal('coffee-model-without-ext'); + expect(instruction.models[0].sourceFile) + .to.equal(path.join(appdir.PATH, 'custom-models', 'coffee-model-without-ext.coffee')); + + expect(instruction.models[1].name).to.equal('js-model-without-ext'); + expect(instruction.models[1].sourceFile) + .to.equal(path.join(appdir.PATH, 'custom-models', 'js-model-without-ext.js')); + }); + it('should set source file path if the file exist.', function() { appdir.writeFileSync('custom-models/model-with-definitions.js', ''); var instruction = boot.compile({ @@ -121,7 +187,8 @@ describe('compiler', function() { }); expect(instruction.models[0].name).to.equal('model-with-definitions'); expect(instruction.models[0].definition).not.to.equal(undefined); - expect(instruction.models[0].sourceFile).not.to.equal(undefined); + expect(instruction.models[0].sourceFile) + .to.equal(path.join(appdir.PATH, 'custom-models', 'model-with-definitions.js')); }); it('should not set source file path if the file does not exist.', function() { From 564c37531d44f22337f9d31e23fe2e6e0ac16e27 Mon Sep 17 00:00:00 2001 From: Shlomi Assaf Date: Wed, 1 Apr 2015 20:40:15 +0300 Subject: [PATCH 6/6] find sourceFile only if set. --- lib/compiler.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/compiler.js b/lib/compiler.js index 74e676b..756ca44 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -290,17 +290,22 @@ function verifyModelDefinitions(rootDir, modelDefinitions) { var registry = {}; modelDefinitions.forEach(function(definition, idx) { - try { - var fullPath = path.resolve(rootDir, definition.sourceFile); - var basename = path.basename(fullPath, path.extname(fullPath)); - var files = tryReadDir(path.dirname(fullPath)); - var sourceFile = filterValidSourceFiles(files, basename)[0]; + if (definition.sourceFile) { + try { + var fullPath = path.resolve(rootDir, definition.sourceFile); + var basename = path.basename(fullPath, path.extname(fullPath)); + var files = tryReadDir(path.dirname(fullPath)); + var sourceFile = filterValidSourceFiles(files, basename)[0]; - definition.sourceFile = path.join(path.dirname(fullPath), sourceFile); - definition.sourceFile = require.resolve(definition.sourceFile); + definition.sourceFile = path.join(path.dirname(fullPath), sourceFile); + definition.sourceFile = require.resolve(definition.sourceFile); - } catch (err) { - debug('Model source code not found: %s - %s', definition.sourceFile, err.code || err); + } catch (err) { + debug('Model source code not found: %s - %s', definition.sourceFile, err.code || err); + definition.sourceFile = undefined; + } + } + else { definition.sourceFile = undefined; }