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"
  }
}
```
This commit is contained in:
Miroslav Bajtoš 2014-06-16 10:39:59 +02:00
parent a9a401ad56
commit b887b33b57
3 changed files with 23 additions and 19 deletions

View File

@ -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

View File

@ -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,

View File

@ -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({