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
A better way for customizing built-in models and models from components #397
Consider an application with a "User" model that has few extra properties or methods compared to the built-in User model.
At the moment, the recommended solution is to create a user model extending the built-in User model, which is rather silly.
What we would like to have instead
Support redefine:true as an alternative to base: "User". A redefined "User" model is defined in common/models/user.json and scaffolded by yo loopback. This will allow redefinition of common models in the server facet.
In the long term (loopback 3.x), we were discussing a mixin-based approach, where User would be just a thin wrapper mixing in login/logout and other features provided by the current User implementation.
Original issue description
Possible solutions:
Implement namespaces, move all built-in models to loopback namespace. Application models can live in the global namespace, i.e. User extends loopback.User.
Implement means allowing applications to modify the built-in models, so that they can use them directly.
A probably related problem: the canonical project layout has three facets - "common", "server", "client". In many cases it is needed to provide different implementation of model methods on the server. The current approach is to create a new "server" model extending the shared "common" model and make any changes in this subclass. While this keeps the design clean, it's also rather cumbersome.
Reacted by Blénesi Attila, Ricardo Berdejo, Andres Rodriguez, Miguel Fermin, Igor Moiseev, Ramon Boss, Phi Tran and Hamid
@bajtos How about this workaround: manually create a BaseUser model extending User model, just before app is created. Then create a User model extending BaseUser model via JSON file.
@bajtos I realize that this issue is about to discuss a "better way". While I am just finding and leveraging workarounds for the moment. Sorry about this.
BTW: create a user model extending the built-in User model This workaround requires use "user" as model name in all places, use "User" to retrieve model may return the built-in model in some case (loopback.findModel('User') !== loopback.findModel('user'), but app.models['user'] === app.models['User'], check #464).
Here's an idea: given the new dynamic mixin functionality, it could be beneficial to implement some of the core models' functionality as mixins. In general, mixins are to be used when inheritance proves difficult.
This is more than silly, it's a real pain when trying to use the passport stuff. Just to change the User model, I have to extend about 5 other models? Why can't we extend/modify the base model at least in code within models/user.js ? I tried that and it didn't seem to pick up the file when no user.json file was installed as well.
@frankcarey Are you using loopback.component-passport? It allows to setup a custom model as User model as long as it's a sub model of User.
And for extending built-in models, I'm currently using the second solution that @bajtos introduced. It's simple and works with loopback.component-passport, but you have to extend User model programmatically. You can create a boot script under server/boot to do that. E.g.:
That seems to be working.. currently just trying to add a relationship of hasMany to User.. why so hard?
Note: to be explicit for others using this technique, I called the script above to /server/boot/0-user-override.js so that it runs before anything else.
UPDATE:
For those looking for the specific code:
module.exports=function(app){varUser=app.models.User;varSomeOther=app.models.SomeOther;// Example to add a relationship to User to some "Other" ModelUser.hasMany(SomeOther,{as: 'others',foreignKey: 'userId'});//console.log('override user');};
It would be helpful to understand WHY we can't just extend the base User model while preserving the name? @clarkorz solution seems to be working well enough for me.
How is this feature coming up? Any timeframe for this to be implemented?
The lack of this feature is a big burden especially when you are crating a publicly available API where model names should make sense. Client is very different from User...
And using lower cased "user" breaks naming norms and integrations with third party libraries.
Reacted by Miguel Fermin, vizo, Steve Gilvarry, Cole Morrison and Tom Kirkpatrick
I was content to just leave it as user for now, despite the fact it's pretty goofy and causes confusion. But when just setting up something to autoupdate the base User model and the extended user model, both seem to automigrate each other. Therefore just wound up having to switch the user over to Client.
And @JonnyBGod is on point. Above anything else it creates a ridiculous goof in terms of naming and public facing APIs. As silly as it sounds, this one limitation is a constant pain point I tend to be annoyed with more than most other things in loopback.
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.
This is still a relevant issue that causes serious problems in terms of API design and integration with third-party libraries. Is a fix being planned?
Not in the current code base. We are working on a new version of LoopBack -
see https://github.com/strongloop/loopback-next - where issues like this one should be easier to address.
Activity
bajtos commentedon Aug 12, 2014
@altsang this issue is still present and we should address it. I am reopening the issue and also added an item to Roadmap brainstorming.
clark0x commentedon Aug 12, 2014
@bajtos How about this workaround: manually create a
BaseUsermodel extendingUsermodel, just beforeappis created. Then create aUsermodel extendingBaseUsermodel via JSON file.Detail:
bajtos commentedon Aug 12, 2014
I don't think that solves the problem. As @raymondfeng wrote in loopbackio/loopback-datasource-juggler#119, model names are unique and models must not be overriden/replaced.
clark0x commentedon Aug 13, 2014
@bajtos I realize that this issue is about to discuss a "better way". While I am just finding and leveraging workarounds for the moment. Sorry about this.
BTW: create a user model extending the built-in User model This workaround requires use
"user"as model name in all places, use"User"to retrieve model may return the built-in model in some case (loopback.findModel('User') !== loopback.findModel('user'), butapp.models['user'] === app.models['User'], check #464).fabien commentedon Aug 19, 2014
Here's an idea: given the new dynamic mixin functionality, it could be beneficial to implement some of the core models' functionality as mixins. In general, mixins are to be used when inheritance proves difficult.
For example, instead of:
Do:
Example mixin: user.js
frankcarey commentedon Sep 3, 2014
This is more than silly, it's a real pain when trying to use the passport stuff. Just to change the User model, I have to extend about 5 other models? Why can't we extend/modify the base model at least in code within models/user.js ? I tried that and it didn't seem to pick up the file when no user.json file was installed as well.
clark0x commentedon Sep 3, 2014
@frankcarey Are you using loopback.component-passport? It allows to setup a custom model as User model as long as it's a sub model of User.
And for extending built-in models, I'm currently using the second solution that @bajtos introduced. It's simple and works with loopback.component-passport, but you have to extend User model programmatically. You can create a boot script under server/boot to do that. E.g.:
Note the script name, make sure it runs before any other boot scripts.
frankcarey commentedon Sep 4, 2014
That seems to be working.. currently just trying to add a relationship of hasMany to User.. why so hard?
Note: to be explicit for others using this technique, I called the script above to /server/boot/0-user-override.js so that it runs before anything else.
UPDATE:
For those looking for the specific code:
frankcarey commentedon Sep 4, 2014
@clarkorz THANK YOU! I'd been banging my head against that for a few days.. even started switching to sails instead because I couldn't progress.
frankcarey commentedon Sep 8, 2014
It would be helpful to understand WHY we can't just extend the base User model while preserving the name? @clarkorz solution seems to be working well enough for me.
59 remaining items
DragonTorus commentedon Mar 15, 2016
Hi! Is there any oficial, normal way(not workaround) to redefine base Model and its relations in loopback 2.27.0 & jugler 2.45.2? Thanks.
JonnyBGod commentedon Dec 2, 2016
How is this feature coming up? Any timeframe for this to be implemented?
The lack of this feature is a big burden especially when you are crating a publicly available API where model names should make sense. Client is very different from User...
And using lower cased "user" breaks naming norms and integrations with third party libraries.
jcolemorrison commentedon May 17, 2017
I was content to just leave it as
userfor now, despite the fact it's pretty goofy and causes confusion. But when just setting up something to autoupdate the base User model and the extendedusermodel, both seem to automigrate each other. Therefore just wound up having to switch theuserover toClient.And @JonnyBGod is on point. Above anything else it creates a ridiculous goof in terms of naming and public facing APIs. As silly as it sounds, this one limitation is a constant pain point I tend to be annoyed with more than most other things in loopback.
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.MichaelPlaxico commentedon Oct 19, 2017
This is still a relevant issue that causes serious problems in terms of API design and integration with third-party libraries. Is a fix being planned?
bajtos commentedon Oct 24, 2017
Not in the current code base. We are working on a new version of LoopBack -
see https://github.com/strongloop/loopback-next - where issues like this one should be easier to address.
RishiMahendru commentedon Apr 5, 2018
Hi @bajtos is there a way to override persistedModel class methods and add our custom logic to used across all custom models
bajtos commentedon Apr 27, 2018
@RishiMahendru sure. Create your own base model inheriting from PersistedModel, and then use this model as a base for all your application models.
This question is pretty off-topic here, please post in on StackOverflow if you need more information.