Skip to content

Upsert method #4

Description

@leonardb

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"
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions