-
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
updateAttributes operational hooks: use instance, not where #481
Comments
I ran into the same confusion too. |
Sounds great @raymondfeng - it's a blocking issue for me at the moment. Any chance of pushing this soon? |
@fabien Do you have time to submit a patch? |
Hmm, I'll try tonight - but I'm guessing @bajtos is more familiar with the ins-and-outs. |
@raymondfeng working on it - #474 as well |
@raymondfeng this is a first stab at it - I'll try to tackle #474 later tonight - please review |
@raymondfeng I'm thinking it makes sense to propagate an
|
Let me explain the reasons behind the current decision and why I think we should take a different approach.
Here is my proposal: add a new method called e.g.
+1, thought I would use a different property name than "operation". @fabien please note that your code contains a race condition: it's possible that the list of models returned by
|
@bajtos having Any thoughts on the Thanks for the pointers on my example:
|
It won't be a breaking change, because the old "updateAttributes" (
Here is my understanding of
That's why updateAttributes don't necessarily have a complete instance, and why I am proposing to add a new method "update" that will have the complete instance. See the code - it passes only the properties from the request to the connector, not the full instance data. inst._adapter().updateAttributes(model, getIdValue(inst.constructor, inst),
inst.constructor._forDB(typedData), function (err) {...}); |
@bajtos You misunderstood the UPDATE MyModel SET name='Jane', age=24 It's also the case for mongodb. The updateAttribute is patching instead of replacing. I'm open to introduce a new instance method for replacing. |
@raymondfeng thanks for chiming in - yes, an additional method would be OK though. |
Maybe we need to have enough information in the ctx to tell if a
There are other combinations too, for example,
Depending on the connector implementations, the compound operations can be atomic too. |
That's better. Few more alternatives to consider: "appData", "hookData", "hookState".
Consider the following ordering of database changes for "deleteAll where color is red":
Using my proposal, the step1 changes the query from
Fair enough, as long as you have a good retry mechanism for failed deletes. Let's move the discussion about delete hooks to #474 please. |
@raymondfeng @fabien I am afraid you are wrong and my understanding of Now that we have clarified the real behaviour of "updateAttributes" as observed by running a real code, is it clear why I made the decision to pass "data" in "before save" hook and why I am very reluctant to pass the "instance"?
There are two steps involved:
"updateAttributes" applies a patch and makes a partial update. The way how I envision the new "update" method is to apply a patch in the first step, but run a full update in the second step: function update(data, cb) {
for (var k in data) { this[k] = data[k]; }
data = this.toObject();
// note: keep undefined properties, they should be reset in the database
connector.updateAttributes(model, id, data, cb...);
} |
@bajtos sorry, I didn't explain myself correctly - I was aware that So finally, I would like proceed with what I propose there. I really need to have a working solution this morning - I'm resolving all the deprecations with a massive code base that relies heavily on the old behavior. As I said, updating is non-trivial at best - it's a blocking issue in some cases even. |
BTW I opened a new issue for implementing hook state - see #484 |
Fixed by #488 which added |
what is the proper way or the best practice to do update operation? |
Because it operates on an instance, you know ...
The text was updated successfully, but these errors were encountered: