php-couchdb icon indicating copy to clipboard operation
php-couchdb copied to clipboard

Upsert method

Open leonardb opened this issue 13 years ago • 0 comments

Would love to see a utility method for handling upsert pattern and automated conflict resolution. Feature should allow for 'partial update' of document. I know it's not part of the Couch API, but I think it's something that everyone finds to be an irritation and handling in the library would make life easier.

Upsert outline: if duplicate key on insert, pull existing document with conflicting key, merge new document into existing document replacing existing values, save merged document and return with new _rev.

Auto conflict resolution: if when saving, newer version exists (conflict), pull newer version of document, merge data from request into new document, save merged document and return new _rev

Partial update description: given existing document in couchdb { "_id": "doc1", "_rev": "AAA", "type": "mytype", "value": "ZZZ" }

now we have the _id of that doc from a result set from a view, and just want to append a new property/attribute to the doc without having to jump through exception handling hoops in client code every time,

$update_doc = array("_id"=>"doc1","mynewfield"=>"mynewvalue");
$res = $conn->upsertDoc($update_doc);

we end up with $res == array("ok"=>1, "id" => "doc1", "rev"=>"BBB");

and in couchdb:

{
  "_id": "doc1",
  "_rev": "BBB",
  "type": "mytype",
  "value": "ZZZ",
  "mynewfield": "mynewvalue"
}

leonardb avatar Mar 30 '11 12:03 leonardb