Skip to content

How to hide relation API endpoints from explorer? ex. GET /parents/{pk}/childs #563

Closed

Description

@veekungx

I have two models Parent and Child and relation like this.

Parent --> hasAndBelongsToMany --> Child
Child --> hasAndBelongsToMany --> Parent

and I got

GET /Parents/{id}/students
GET /Parents/{id}/students/{pk}
GET /Parents/{id}/students/rel/{pk}
... etc.

I can hide GET /parents with.

Parent.sharedClass.find('find', true).shared = false;

How about relations to child model the code below not working?

 Parent.sharedClass.find('__get__students', true).shared = false;

OR

 Parent.sharedClass.find('__get__students', false).shared = false;

Activity

  1. changed the title [-]How to hide relation API endpoints from explorer? like GET /parents/{pk}/childs[/-] [+]How to hide relation API endpoints from explorer? ex. GET /parents/{pk}/childs[/+] on Sep 14, 2014
  2. cajoy commented on Sep 19, 2014

    @cajoy

    I have the same question.. Seems like there is no loopback developers.

  3. bajtos commented on Sep 20, 2014

    @bajtos
    Member

    AFAIK, the relation methods are created when the model is attached to a datasource, which happens later in the application lifecycle. To disable a relation method, you have to make the change after the model was attached.

    Parent.on('attached', function() {
      Parent.sharedClass.find('__get__students', false).shared = false;
    });

    @raymondfeng @ritch I remember one of you was commenting on this same problem elsewhere, could you please confirm the solution above is the correct one?

  4. bajtos commented on Sep 20, 2014

    @bajtos
    Member

    @crandmck please update the documentation once a working solution is confirmed.

  5. clark0x commented on Sep 20, 2014

    @clark0x
    Contributor

    @bajtos It won't work currently. check discuss on #410, strongloop/strong-remoting#94, and PR strongloop/strong-remoting#95

  6. tonysoft commented on Oct 2, 2014

    @tonysoft

    Parent.on('attached', function() {
    Parent.sharedClass.find('__get__students', false).shared = false;
    });

    Seems as though ".find" above returns a Class one level higher than needed for this purpose. I had to access ".fn" to get this to work.

    Parent.sharedClass.find('__get__students', false).fn.shared = false;

  7. tonysoft commented on Oct 2, 2014

    @tonysoft

    IT DOES WORK... Ignore my comment here below... I had omitted the second Argument that Ritchie mentions in his comment below...

    Sorry...


    I tested that and it DOES NOT work Raymond... I then use the technique I
    outlined in my comment and it does work...

    It certainly would be the cleaner way to do it though...

    On Thu, Oct 2, 2014 at 11:13 AM, Raymond Feng notifications@github.com
    wrote:

    Can you try
    https://github.com/strongloop/loopback/blob/master/lib/models/model.js#L392
    ?

    —
    Reply to this email directly or view it on GitHub
    #563 (comment).

  8. ritch commented on Oct 2, 2014

    @ritch
    Member

    As of loopback 2.4.0 you can do:

    var isStaticMethod = false;
    Parent.disableRemoteMethod('__get__students', isStaticMethod);

    @bajtos @tonysoft @clarkorz @cajoy

  9. raymondfeng commented on Oct 3, 2014

    @raymondfeng
    Member

    I just confirmed it works with latest loopback. In my case, I added common/models/post.js:

    module.exports = function(Post) {
      Post.disableRemoteMethod('__get__tags', false);
      Post.disableRemoteMethod('__create__tags', false);
      Post.disableRemoteMethod('__delete__tags', false);
    };

    The three relation methods to tags are now disabled in the api explorer.

  10. veekungx commented on Oct 4, 2014

    @veekungx
    Author

    WOW!!!! thx for your hard work. I wanna add more than 1 star on LB.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions