You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
LoopBack models should inherit config.options from the parent #157
When LB developer wants to access built-in LB models via app.models, he has to define subclasses in models.json. The current implementation requires that a lot of config options of the base model must be repeated in the subclass definition.
This has several disadvantages:
It's difficult to add a built-in model to your app, especially if you are new to loopback.
If we modify the built-in model (e.g. by adding another relation), existing application will break after upgrade.
Below are the use cases I discovered while writing E2E tests for loopback-angular.
assert.js:92
throw new assert.AssertionError({
^
AssertionError: MyUser is referencing a dataSource that does not exist: "undefined"
at modelFromConfig (/Users/bajtos/src/loopback/loopback/node_modules/loopback/lib/application.js:545:3)
at Function.app.model (/Users/bajtos/src/loopback/loopback/node_modules/loopback/lib/application.js:115:38)
[etc.]
Since the new model is a subclass of a built-in model, it should use the same datasource that was configured for the User model.
If the datasource of the base class was not resolved yet, but it has autoAttach property, then the process of attaching the subclass to a datasource should be deferred to loopback.autoAttach.
/Users/bajtos/src/loopback/loopback/lib/models/user.js:152
user.accessTokens.create({
^
TypeError: Cannot call method 'create' of undefined
at /Users/bajtos/src/loopback/loopback/lib/models/user.js:152:29
at /Users/bajtos/src/loopback/loopback/lib/models/user.js:202:7
at /Users/bajtos/src/loopback/loopback/node_modules/bcryptjs/bcrypt.min.js:40:101
[etc]
To fix this problem, one has to re-define User relations:
Let's bring this issue back to life. For the upcoming 3.0 release, I am proposing to modify juggler's model builder to inherit model settings when subclassing a new model.
As for inheriting relations, pure prototypal inheritance may not be enough. Consider this scenario:
We have a parent model User which has a relation
accessTokens: {model: 'AccessToken',type: 'hasMany',// this is omitted: foreignKey: 'userId'}
When attached to a datasource, foreignKey property is automatically inferred and AccessToken.userId property is defined by juggler.
Now when we create Customer extending User, the foreign key for relation accessTokens will be generated as AccessToken.customerId, which may break code relying on userId property name.
A possible solution is to always fill in foreignKey before inheriting model settings.
We agreed this required a bit of research to find out what and how should be inherited when subclassing a model. We don't have bandwidth to address it in 3.0 time.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.
Activity
raymondfeng commentedon Feb 28, 2014
The requirement seems to boil down to dependencies between models and have a systematic way to resolve/inject the dependencies.
glesage commentedon Apr 24, 2014
Any updates on this? Is the manual way still the best way?
bajtos commentedon Apr 24, 2014
@glesage No updates so far.
bajtos commentedon Nov 26, 2014
This is no longer relevant, LoopBack 2.0 decoupled datasources config from model definition.
This is still relevant. The implementation would most likely break backwards compatibility.
Related issue: #397 A better way for customizing built-in models and models from components
bajtos commentedon Feb 4, 2016
Let's bring this issue back to life. For the upcoming 3.0 release, I am proposing to modify juggler's model builder to inherit model settings when subclassing a new model.
7 remaining items
davidcheung commentedon Apr 28, 2016
Loosely related #1485
bajtos commentedon May 2, 2016
ACLs are not inherited either, see #2270.
As for inheriting relations, pure prototypal inheritance may not be enough. Consider this scenario:
We have a parent model
Userwhich has a relationWhen attached to a datasource,
foreignKeyproperty is automatically inferred andAccessToken.userIdproperty is defined by juggler.Now when we create
CustomerextendingUser, the foreign key for relationaccessTokenswill be generated asAccessToken.customerId, which may break code relying onuserIdproperty name.A possible solution is to always fill in
foreignKeybefore inheriting model settings.bajtos commentedon May 12, 2016
We agreed this required a bit of research to find out what and how should be inherited when subclassing a model. We don't have bandwidth to address it in 3.0 time.
stale commentedon Aug 23, 2017
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
stale commentedon Sep 6, 2017
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the
CODEOWNERSfile at the top-level of this repository.