Backport from 2.0: create model from config, configure model#321
Merged
Backport from 2.0: create model from config, configure model#321
Conversation
added 8 commits
June 12, 2014 10:41
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,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
Use `instanceof` operator instead: ModelCtor.prototype instanceof loopback.Model dataSource instanceof loopback.DataSource
Move isBrowser and isServer from lib/loopback to a new file lib/runtime. Move all Model and DataSource related methods like `createModel` and `createDataSource` to lib/registry. Remove the circular dependency between lib/application and lib/loopback, by loading lib/registry and/or lib/runtime instead of lib/loopback where appropriate This commit is only moving the code around, the functionality should not be changed at all.
Add debug logs to troubleshoot two unit tests failing on the CI server only.
Add missing names.
Merge the two methods `loopback.createModel` and `loopback.createModelFromConfig` into a single method `createModel`.
Fix the problem where `registry.defaultDataSources` has two instances:
- `require('loopback').defaultDataSources` used by
`loopback.autoAttach()`
- `require('./registry').defaultDataSources` used by
`app.dataSource`.
I am intentionally leaving out unit-tests as the whole `autoAttach`
feature is going to be deleted before 2.0 is released.
Expose the juggler's DataSource constructor as `loopback.DataSource`. The DataSource constructor is most useful to check for `instanceof DataSource`, but it also makes the loopback API more consistent, since the API is already exposing all pre-built Models.
Member
|
LGTM |
bajtos
added a commit
that referenced
this pull request
Jun 12, 2014
…onfig Backport from 2.0: create model from config, configure model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a back-port of #304 and #308 to 1.x.
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,
app.model(name, config)calls bothcreateModeland
configureModel./to @ritch please review. There was a lot of conflicts to resolve, I was carefully comparing diffs to ensure no new stuff from 2.0 would sneak into 1.x.
This patch is needed to make loopback 1.x compatible with loopback-boot 2.0 and the new project layout.