Skip to content

Related model shared = false vs. REST exposure #346

Closed

Description

@fabien

It seems that Model.setup doesn't take into account when a model is not shared - the REST API endpoints are created regardless. Also, setting the generated relation method to shared = false has no effect (this is the method outlined in the docs):

Project.prototype['__create__history'].shared = false;

Activity

  1. raymondfeng commented on Jul 7, 2014

    @raymondfeng
    Member

    Do you expect Model.setup() to skip the steps for remoting if shared is false? A typical flow is to configure the model first before calling app.model(MyModel). I would argue it's fine to build the remoting metadata/methods regardless.

    For 'Project.prototype['__create__history'].shared = false;', do you see the endpoint in the API explorer? Can you still invoke it? I wonder if you set the flag too late before REST routes are built? I think we should have a method to remove it from remoting after the fact.

  2. fabien commented on Jul 7, 2014

    @fabien
    ContributorAuthor

    Hi @raymondfeng - it might be the case that I'm setting the flag too late. Being able to selectively remove methods from REST exposure would certainly be helpful.

    How would you recommend 'registering' models, so they're available from app.loopback.getModel(), yet are not exposed? I find myself using getModel in a few places, even for 'embedded' models, and I like being able to have a unified way to reach them vs. using app.models[modelName] directly.

  3. bajtos commented on Jul 8, 2014

    @bajtos
    Member

    How would you recommend 'registering' models, so they're available from app.loopback.getModel(), yet are not exposed? I find myself using getModel in a few places, even for 'embedded' models, and I like being able to have a unified way to reach them vs. using app.models[modelName] directly.

    Both app.loopback.getModel(name) and app.models[name] are valid way of accessing the model. The first one is relying on a global static registry, which is something we would like to remove in favour of app.models[name] in the future.

    Whenever you call loopback.createModel or Model.extend, the model is registered in the global static registry and is available via loopback.getModel(name). Which means you don't have to register your models if you are accessing them via loopback.getModel.

    The method app.model(model) has two purposes:

    • modify the configuration of an existing model - attach to a datasource, add relations, etc.
    • register the model in app.models

    Here is my question: How do you setup your application models? Do you use loopback-boot or do you have your own bootstrapper?

  4. fabien commented on Jul 8, 2014

    @fabien
    ContributorAuthor

    @bajtos thanks for the clarification, that makes sense. I am still on the deprecated loopback-boot setup (the one where app is passed in to the model's module), but I'm hoping to switch over my code-base soon. I prefer to declare 'embedded' models in code only, using a memory datasource (as a convenience, items are never stored there).

    It would be nice to have 'one-off' models from time to time, not actually registered anywhere. Is that possible?

  5. bajtos commented on Jul 8, 2014

    @bajtos
    Member

    Thanks for the explanation.

    It would be nice to have 'one-off' models from time to time, not actually registered anywhere. Is that possible?

    Not at the moment.

  6. added this to the milestone on Jul 10, 2014
  7. self-assigned this
    on Jul 15, 2014
  8. raymondfeng commented on Jul 23, 2014

    @raymondfeng
    Member

    You can have the one-off models if you use juggler apis, for example:

    var modelBuider = new ModelBuilder();
    modelBuilder.define(...);
    
    var dataSource = new DataSource(name, settings, modelBuilder);
  9. modified the milestone: #Rel lb 2.0.0 on Sep 30, 2014
  10. bajtos commented on Apr 7, 2016

    @bajtos
    Member

    How would you recommend 'registering' models, so they're available from app.loopback.getModel(), yet are not exposed? I find myself using getModel in a few places, even for 'embedded' models, and I like being able to have a unified way to reach them vs. using app.models[modelName] directly.

    One can set shared: false in server/model-config.json to have the model available via app.models.MyModel and app.registry.getModel('MyModel') without exposing the model via the REST API.

    Here is an example of creating such model programatically:

    var MyModel = app.registry.createModel({ /* model definition */ });
    app.model(MyModel, { public: false });

    Being able to selectively remove methods from REST exposure would certainly be helpful.

    You can use disableRemoteMethod for that, see the docs.

    Example:

    var isStatic = true;
    MyModel.disableRemoteMethod('deleteById', isStatic);

    @fabien I hope that I have answered your questions, I am closing this issue. Please open a new issue if you have any more questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions