-
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
Add ctx. isNewInstance to "before save" and "after save" hooks #838
Comments
I have same question. Currently, I fire a I lookup the PR history, this loopbackio/loopback-datasource-juggler#65 introduces To @ritch so, what do you think? |
+1 for created event |
This should be a reasonably easy change, would you (@pungoyal or @clarkorz) mind to contribute it? |
A state other than |
It is probably better to defer this enhancement until loopbackio/loopback-datasource-juggler#367 is implemented. |
The new Operation hooks implemented by loopbackio/loopback-datasource-juggler#367 provide However, if you are able to come up with a way how to distinguish between Another alternative is to let Thoughts? |
@bajtos loopback-connector-mongodb implements For loopback-connector-mysql, I am not familiar with mysql, but I do find a hint in mysql document:
I don't study all others connectors, but can we tolerate that some connectors could distinguish between |
@clarkorz thank you for making the research. Based on what you have described above, I am proposing the following implementation: extend This new property will have one of the following values for
The benefit of such solution is that a simple check Of course, methods that already know what kind of write is being performed ( Thoughts? /cc @raymondfeng @ritch |
LGTM Besides the new hooks, should we complete the event system in advance? To specific, should we replace |
We need to preserve the The choice is then |
Sorry, but I have a newbie question, since then how do old model hook |
It is called only when you invoke |
See #1148 for an example of multiple different approaches for converting the old "beforeCreate" hook into the new "before save" hook. |
@bajtos I would go with |
@bajtos could you please summarize which context property name and values you expect? I think I'll need this soon as well. |
I am not sure what you are asking me. Your previous comment described exactly what I would like to see implemented:
I am ok with a partial implementation that does not include necessary changes in all connectors, as long as the behaviour is reasonable in the case when the connector does not provide the flag yet. It's also ok to implement this flag for one of the hooks only, e.g. "before save", and leave the other hook for later. Last but not least, there is no need to implement this for the "changed" event, since it has been already deprecated in favour of the new Operation hooks. |
As part of this I would love to see the operation hooks expanded to include |
I have landed loopbackio/loopback-datasource-juggler#515 which added |
Connectors supported:
I am closing this issue are solved, we will keep track of the remaining work in connector-specific issues. |
Extend
before save
andafter save
hooks and add a new propertyctx.isNewInstance
(or similar).This new property will have one of the following values for
Model.updateOrCreate
:before save
:after save
:A thing to figure out: how to handle atomic
findOrCreate
, as it may fire thebefore save
hook even if the model already exists?The original issue description
Does it make sense to have a
created
event onmodel
? Currently we havechanged
, but that fires for any changes to the model.The text was updated successfully, but these errors were encountered: