Updating CouchDB document in nano

I need to get a document, change / insert / delete some fields and return it.

The put action requires the current revision of the document, but in nano I cannot find any function that takes the revision as a parameter and inserts the document back into the database.

How can I do this with nano?

+6
source share
1 answer

Note. This is a general algorithm, it is not specific to any library, since the nano insert() method does not offer anything automated for updating documents.

Get the document, save the current version, apply the changes and try to send the document with the saved revision number.

Be sure to handle the possible 409 conflict responses that occur when the document has been modified in the meantime.

In this case, you must restore the document, save the version number, reapply your changes, and then try sending it again with the new version.

So here is the algorithm:

  • Get a document
  • Save _rev
  • Apply Changes
  • Try sending an updated document with _rev saved
  • Go to step 1 in case of 409

Checkout CouchDB HTTP Document API PUT Section and CouchDB Replication and Conflicts for more information on this. You can also find How to update a document using Nano (CouchDB client for Node.js) .

+7
source

Source: https://habr.com/ru/post/921363/


All Articles