loopback icon indicating copy to clipboard operation
loopback copied to clipboard

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

Open veekungx opened this issue 12 years ago • 10 comments

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;

veekungx avatar Sep 14 '14 14:09 veekungx

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

cajoy avatar Sep 19 '14 13:09 cajoy

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?

bajtos avatar Sep 20 '14 00:09 bajtos

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

bajtos avatar Sep 20 '14 00:09 bajtos

@bajtos It won't work currently. check discuss on https://github.com/strongloop/loopback/issues/410, https://github.com/strongloop/strong-remoting/issues/94, and PR https://github.com/strongloop/strong-remoting/pull/95

clark0x avatar Sep 20 '14 02:09 clark0x

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;

tonysoft avatar Oct 02 '14 18:10 tonysoft

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

raymondfeng avatar Oct 02 '14 18:10 raymondfeng

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 [email protected] 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 https://github.com/strongloop/loopback/issues/563#issuecomment-57675912.

tonysoft avatar Oct 02 '14 18:10 tonysoft

As of loopback 2.4.0 you can do:

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

@bajtos @tonysoft @clarkorz @cajoy

ritch avatar Oct 02 '14 22:10 ritch

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.

raymondfeng avatar Oct 03 '14 04:10 raymondfeng

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

veekungx avatar Oct 04 '14 08:10 veekungx