-
Notifications
You must be signed in to change notification settings - Fork 362
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
Add new hook 'loaded' #599
Conversation
If I understand the current implementation correctly, then the new "loaded" hook is basically the same as "after save", except that it is called for "DAO.find" too. It seems to me that such implementation may be less powerful. The way I have envisioned the Using the example of encrypting property values when stored in the database, here is a list of steps & hooks that I would like to be perform on update/create:
Having said that, this is only one of many possible solutions and use-cases and it may be suboptimal. @raymondfeng @fabien what's your take on this? |
@bajtos @raymondfeng I actually thought - just by the name of it - that this would provide the ability to handle the - raw - data right after it was fetched by the connector. That would have been a great hook to have IMHO. |
@fabien Thank you for chiming in! My vision is to invoke this hook after the data was fetched by the connector, before we create a model instance from that data. The reason for that is to allow hooks to decrypt data (for example). Is this the same what you were thinking about? If not, then could you please advise a better name that would make this distinction more clear? I.e. to make it more clear that this new hook is called with the raw database data, not a full model instance? Will the hook as I proposed it work for you, or do you actually need another hook to be called after a model instance is constructed? |
@bajtos it's exactly what I had in mind, would you be interested in implementing it? |
Good, I am glad we are on the same page here :)
@PradnyaBaviskar will work on the implementation, I'll help her as needed and review the code. |
@bajtos @PradnyaBaviskar great! Looking forward to it - I already have some use-cases lined up. |
7df8d9d
to
d1b6fa2
Compare
@PradnyaBaviskar @bajtos any progress on this? |
cd5acdb
to
1552fc4
Compare
Reworked on the |
}); | ||
|
||
// TODO (pradnya) - the, instance returned by `create` context | ||
// does not have values updated from `loaded` hook. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is a problem that affects the "persist" hook too. I am proposing to add a new model setting that will enable this behaviour (updating the created instance using the data returned from the connector). See #565 for an inspiration how to implement such option.
@PradnyaBaviskar could you please add tests to verify the order in which the hooks are called by individual methods? I think it's important to clarify whether "loaded" is called before or after the "after save" hook. |
33eac1c
to
4dadf60
Compare
Made following changes
@bajtos, Can you please review? |
4dadf60
to
51128bd
Compare
@PradnyaBaviskar you are on the right track. I think it's better to call
That would be nice, but I am afraid it will cause too much performance overhead, especially in the case where the data was not changed at all. Let's keep that out of the initial implementation. I have one more question for you. As I am reading the implementation of |
@slnode ok to test |
The name |
Regarding -
Should this setting be affecting the About your question,
That was a mistake on my part! In the test which asserted that the updates made by |
51128bd
to
11726be
Compare
Changed the following -
Can you please review? |
}; | ||
|
||
context = { | ||
Model: Model, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a forgotten code from "loaded" hook that was not removed completely. Could you please remove/revert? I think there should be no changes in the updateAll
method.
👍 IMO, it's ok to affect the "loaded" hook only, because that's the hook which is affecting data used to initalize the model instance. The persist hook is manipulating the data stored in the database. @raymondfeng @fabien This pull request is almost ready to be merged, there are only few minor coding-style issues to fix. I think it would be good if you could take at least a quick look at the solution we have arrived at and let us now if there are any major issues we didn't spot ourselves. If you don't leave any comments by Wednesday morning CEST, I'll go ahead and land this. |
@slnode test please |
11726be
to
20cf67d
Compare
@bajtos LGTM @PradnyaBaviskar thanks! |
Done with the fixes for coding-style issues. |
Landed, thank you! |
+1 for ctx.data https://docs.strongloop.com/display/public/LB/Operation+hooks#Operationhooks-loaded User.observe('loaded', function observeLoaded(ctx, next) {
var loopback = require('loopback');
var context = loopback.getCurrentContext();
var user = context.get('currentUser'); According to the docs the ctx.data property should be there. The user exists. In any other cases I can get a hold of the user reference. In this case user is undefined. |
In fact, in any case I add an observer to the User model the above wont work, e.g. I cannot access the currently logged in user. It works with other models. |
Connect to #441
List of methods that are triggering the hook:
create
,updateOrCreate
,findOrCreate
,find
(and friends),updateAttributes
.Caveat: by default,
create
andupdateAttributes
do not apply database updates to the model instance returned to the callback, therefore any changes made by "loaded" hooks are discarded. Users can set a per-model optionupdateOnLoad: true
to change this behaviour./to @raymondfeng, can you please review?
/cc @bajtos