-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
beforeRemote hook executed after datasource access #2064
Comments
Hey @eggerdo, I was unable to reproduce the issue. Can you point me to the spot where When I attempted to reproduce, the behavior was the same no matter what method I was calling. Do you have to most recent version LoopBack? I need a little more detail to be able to help you out. It would be even better if you could fork loopback-sandbox and make an example of exactly what you are trying to do. |
Hey @richardpringle, I might have been a bit too unclear. The I created a fork of the example project which you can find here where I show the issue, without switching the databases. I wrote the error description and the cases in the readme there. If you want I can update the example to include the switching of the datasources based on the current user, but I don't think it is relevant to determine if this issue is a bug or not :) But for your information I will give you an overview:
This works so far except from the case described above: when I try to update an object using PUT /Model/{id} it first checks in the datasource which was last accessed. If that datasource does not have an object with this id, the call fails, even if the datasource that I actually want to query has it. And since there is no hook which triggers before the datasource is accessed, I cannot update it in time. |
Hey @eggerdo, first of all, thanks forking and adding the detailed description. There are a bunch of us who are trying to help solve the long list of LoopBack issues, and this makes our job much easier!...Which in turn means you get help faster. I believe that this remote hook is functioning as expected. A remote hook simply implies that the hook is triggered before the function associated with the request. For The issue with your scenario is that there is another method ( In the meantime... I'm not too sure how to implement a workaround. We're currently working on a feature to make endpoints a little easier to customize, but I'm not sure when it will be ready for release. For now, I think you're going to have to create your own middleware that runs before the Another solution would be to implement your own Sorry for the bad news... please post your solution here for any others who run into the same issue! Also, if you are submitting a PR, we can help you along the way. Good luck! |
Ok thanks. Yes I was thinking one could argue for either case. I understand your point that the remote hook is associated with the But to avoid this problem, I think the easiest solution for me is to disable the And if I have time I might even look into the But thanks for your help in any case. |
@eggerdo, I have an update for you. I was jumping to conclusions with that Let me know if that works for you! |
I would also be interested to see your implementation of using multiple datasources on the same model if possible... |
@richardpringle, sorry I had some other things on my plate and just managed to look into it again. I had already checked the |
@Amir-61, can you weigh in on this? @eggerdo, even though In any case, the way you are going to achieve your desired functionality will be a bit of a workaround, so I want to make sure we have a well defined feature request. |
@richardpringle, thanks I appreciate any input to this problem. You are correct. I got misled by the fact that However, there are still two problems with the
I can think of ways to solve item 2, by modifying the file |
@eggerdo: I may have found a solution for you, middleware to the rescue! DISCLAIMER: I am not sure what the security repercussions are. If @bajtos could comment, that would be great. Also, before implementing anything, I suggest reading the middleware documentation In your "auth": {
"loopback#token": {}
} Now you will get the As for switching datasources, this can be accomplished in a custom middleware function. How you implement that is up to you. I placed the following code in the app.middleware('routes:before', function(req, res, next) {
console.log(req.accessToken);
next();
}); obviously placing the code you have in your forked sandbox instead of a Let me know how it goes! |
@richardpringle: thanks you're awesome, this works like a charm! And as for a first impression, this looks to be much neater than I had it before in any case. Thanks a lot already :) One more question I have: Is it possible that one REST API call may interrupt another? Meaning, if I get a call, then update my datasources in the middleware function, is it possible that a second call will modify the datasources before the first call is completed? |
Hi, for LB 3.0, we are thinking about replacing I think he approach described above, which uses a middleware to select the datasource, is reasonable. The security implication is that the middleware selection is performed before any authentication and authorization takes place, because auth & auth is performed in
The calls will be interleaved. Because each model is a global singleton and each model can be attached to a single datasource, there is indeed a race condition in which an operation started with datasourceForUser1 can finish with datasourceForUser2. See my older StackOverflow answer for alternative solutions. |
Hi For switching data source at runtime, we have overridden the The approach is working. We have done some testing with concurrent requests We are just concerned about the overhead model.attach(data source) has in Bajtos, do you see any concern with this approach?
|
Thank you. The race condition is a bit worrying, and I will need to discuss internally how we should handle that. I'm glad to hear about the update for LB 3.0, but I'm not sure that will solve my problems either. Querying related models with an include filter, does not trigger any remote hooks in the related model, so I think I will always end up with a race condition when switching my datasources. So I might need to think that over. And the solution in stackoverflow seems fine, except that I would have to update the LB server every time I need to add a new group of models using a different datasource. And I don't think it will work correctly with related models queried through include filters either. But thanks in any case for your help and valuable input! |
Dominik,
|
I see the problem rather in the |
So what's the alternative you used to know the context (which data source I was facing one issue with getCurrentcontext () but that was because of
|
For remote method PUT /Models/{id}, the
beforeRemote('**')
hook is triggered after the datasource is accessed. Is this a bug?From the description of the beforeRemote hook I would expect that that hook is triggered before anything else happens. At least that's how it is with other remote operations like GET /Models/{id} or GET /Models/. I checked and the Problem is that it calls findById, probably to check if the item exists in the database, before the beforeRemote hook is triggered. But I need to modify my datasources depending on the current user, so I need a hook, which is triggered before any datasoure is accessed. However in this case, all the remote hooks, operation hooks and model hooks are triggered after the datasource is accessed.
The text was updated successfully, but these errors were encountered: