-
Notifications
You must be signed in to change notification settings - Fork 128
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
Feature request: Document.prototype.update #113
Comments
For:
You can do
It's not exactly We could validate just the provided fields, but even that can raise a few issues. |
The problem with doc.merge().save() is that it isn't safe under concurrent edits, since save() does a replace for existing documents. If one process is updating field 'f' only and another is updating field 'g' only, they could easily clobber each other's edits. The update() version allows you to update just the fields that you need in a single atomic command. It could be particularly useful for appending things to lists without worrying about races, for example: doc.update({ tokens: r.row('tokens').append({ id: 1, ts: r.now() }) }); You can of course do all of this directly with: Model.get(doc.id).update({}, {returnChanges: true}).execute(); But it does not validate, and it does not repopulate the model or regenerate virtuals, so I think it may be convenient to expose this on the document. The way I imagine doing validation (I started, but haven't finished it yet) is to do doc.merge().validate(), but then only store the fields you changed. On successful write, replace the doc with new_val, on error restore old field values to their state before the merge. I figured validating individual fields isn't good enough since there is also a document-level validator which may have arbitrary data dependencies, so doc.merge().validate() seems like the only sensible way to do it. Not sure how to handle hooks yet, whether there should be a separate update hook or if it should trigger pre-save hooks as well (for example, I have a pre-save hook that updates the timestmap, so it would be handy to trigger that rather than having to duplicate it in a pre-update hook). |
Of course, doing anything more complicated than simple field sets will make it impossible for the validator to do its job anyway. At least it will validate when reading the results back. |
This issue is causing me a great deal of pain right now, as even with the non-atomic I'm very very close to giving up with Thinky, and just using plain javascript constructors with rethinkdbdash commands directly.. Which would be painful, but not as painful as this functionality missing. |
Keep in mind that if you are running doc.validate() yourself, it could be synchronous (and throw exceptions) or asynchronous (and return a promise), depending on the hooks/validators you have defined. I was bitten by that once. |
@miksago -- What do you mean by the validations don't run? |
As in, I have a function validator for an optional email address field. When doing merge/save, the fields I merge include an invalid email address, but my validation doesn't get triggered. |
(aside: I'm ditching thinky later in favour of a different approach to data modeling, most likely around immutable objects, got some research and thoughts here, but yeah. I don't need a lot of what thinky provides, I'm happy to do relations myself) |
There was a bug about Can you check the version of thinky? And if you run something more recent than 1.14.2, do you have a snippet to reproduce this? |
I'm on 1.15.0 |
I think it was because of how I was using the promises.. maybe I wasn't catching the error correctly. |
I see that da08ffe adds support for Model.get(doc.id).update({...}), which is awesome. Does that execute as a single query (i.e. no additional roundtrip to db to retrieve the doc again)? Do you think it still makes sense to also provide a convenience method on Document that just does that for the current doc? |
@simonratner -- I haven't implemented yet
|
Just wanted to check in here and see if there had been any further progress related to this issue. Thanks! |
Is |
It would be nice to have an update() method for concurrent-safe partial updates, not unlike ActiveRecord's update_attributes.
For example:
would execute something along the lines of:
Here's a working sketch, although the devil is obviously in the details (hooks, validation, etc):
The text was updated successfully, but these errors were encountered: