[2.0] Add createModelFromConfig and configureModel()#304
Conversation
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
There was a problem hiding this comment.
@example isn't supported by strong-docs.
There was a problem hiding this comment.
I thought I've seen a pull request adding that support recently. I checked strong-docs commits and could not find it anymore.
Never mind, I'll fix this and use **Example** instead of @example.
There was a problem hiding this comment.
Nice to have:
Model.assertIsModel(Model)
|
Ad |
There was a problem hiding this comment.
....sigh
We need to fix this. You shoulnd't need to use a unique name here.
There was a problem hiding this comment.
See @raymondfeng comment in the discussion in loopbackio/loopback-datasource-juggler#119:
The model names have to be unique
How else do you want to conform to this requirement in tests that are creating (defining) many new models?
There was a problem hiding this comment.
I think its out of scope for this PR.
We should remove that requirement, that is my point.
This is a good idea. |
|
Somehow the tests are failing...? Otherwise this LGTM. Feel free to defer the minor changes to another PR. |
There are two problems:
|
[2.0] Add createModelFromConfig and configureModel()
|
Just joining this issue now. Looks like I'm late: I like the models/lib, models/index.js idea but the loading order probably doesn't need to be dependent on order of require statements. What do you think about building a priority system into models where each time a model extends another model Not sure if you're allowing access to the inherited model's prototype but that would allow access to late binding model methods? |
@henrytseng It will not work. If you want to define a I agree the order of var loopback = require('loopback');
// ensure the base class is defined
require('./vehicle');
var Car = module.exports = loobpack.createModel(require('./car.json'));
//etc.
The Car model in the example above has a copy of all Vehicle properties that were defined at the time when Did I answer your question? |
|
See also #324. |
Since this information (the ordering which models should be defined) is determinable, I think we should take that into account internally and require them in the correct order. The downside is that the algorithm to determine this priority isn't trivial. |
|
strongloop/loopback-boot#10 implemented a mechanism where the models are automatically required by the bootstrapper in correct order. See the pull request for more details. |
Add new API allowing developers to split the model definition and
configuration into two steps:
Build models from JSON config, export them for re-use:
Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
Rework
app.modelto useloopback.configureModelunder the hood.Here is the new usage:
In order to preserve backwards compatibility with loopback 1.x,
app.model(name, config)calls bothcreateModelFromConfigand
configureModel.See strongloop/loopback-example-offline-sync#13 for background of this change.
/to @ritch @raymondfeng please review