From b887b33b573f2f2494c1de1aecb60870249152f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 16 Jun 2014 10:39:59 +0200 Subject: [PATCH] compiler: Move model-sources cfg to models.json Remove `modelSources` option from `boot()` options, add `_meta.sources` to `models.json`. ```json { "_meta": { "sources": ["./custom/path/to/models"] }, "Car": { "dataSource": "db" } } ``` --- docs/configuration.md | 25 +++++++++++++------------ lib/compiler.js | 7 +++++-- test/compiler.test.js | 10 +++++----- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index d96c37c..d209bb7 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -165,7 +165,7 @@ All code samples are referring to the sample project described above. *models.json* - ```js + ```json { "car": { "dataSource": "db" @@ -176,7 +176,6 @@ All code samples are referring to the sample project described above. 2. Change per-model javascript files to export a function that adds custom methods to the model class. - *models/car.js* ```js @@ -188,18 +187,20 @@ All code samples are referring to the sample project described above. }; ``` - 4. Modify the boot configuration to list the directory containing - model definitions. + 3. If your model definitions are not in `./models`, then add an entry + to `models.json` to specify the paths where to look for model definitions. - ```js - var loopback = require('loopback'); - var boot = require('loopback-boot'); + *models.json* - var app = loopback(); - boot(app, { - appRootDir: __dirname, - modelSources: ['./models'] - }); + ```json + { + "_meta": { + "sources": ["./custom/path/to/models"] + }, + "Car": { + "dataSource": "db" + } + } ``` #### Attaching built-in models diff --git a/lib/compiler.js b/lib/compiler.js index 48a6757..a8a0e88 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -42,9 +42,12 @@ module.exports = function compile(options) { // require directories var bootScripts = findScripts(path.join(appRootDir, 'boot')); - var modelSources = options.modelSources || ['./models']; + var modelsMeta = modelsConfig._meta || {}; + delete modelsConfig._meta; + + var modelSources = modelsMeta.sources || ['./models']; var modelInstructions = buildAllModelInstructions( - appRootDir, modelsConfig, modelSources); + modelsRootDir, modelsConfig, modelSources); return { app: appConfig, diff --git a/test/compiler.test.js b/test/compiler.test.js index ac4ade3..b3823d6 100644 --- a/test/compiler.test.js +++ b/test/compiler.test.js @@ -267,17 +267,17 @@ describe('compiler', function() { }); }); - it('supports `modelSources` option', function() { + it('supports `sources` option in `models.json`', function() { appdir.createConfigFilesSync({}, {}, { + _meta: { + sources: ['./custom-models'] + }, Car: { dataSource: 'db' } }); appdir.writeConfigFileSync('custom-models/car.json', { name: 'Car' }); appdir.writeFileSync('custom-models/car.js', ''); - var instructions = boot.compile({ - appRootDir: appdir.PATH, - modelSources: ['./custom-models'] - }); + var instructions = boot.compile(appdir.PATH); expect(instructions.models).to.have.length(1); expect(instructions.models[0]).to.eql({