{"id":8114,"date":"2015-11-26T13:46:06","date_gmt":"2015-11-26T13:46:06","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/26\/pouchdb-how-to-resolve-conflicts-with-continuous-replication-open-source-projects-pouchdb-pouchdb\/"},"modified":"2022-08-30T15:03:00","modified_gmt":"2022-08-30T15:03:00","slug":"pouchdb-how-to-resolve-conflicts-with-continuous-replication-open-source-projects-pouchdb-pouchdb","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/26\/pouchdb-how-to-resolve-conflicts-with-continuous-replication-open-source-projects-pouchdb-pouchdb\/","title":{"rendered":"PouchDB: How to resolve conflicts with continuous replication?-open source projects pouchdb\/pouchdb"},"content":{"rendered":"<p>I&#8217;m new to both CouchDB and PouchDB and am using it to create a contact management system that syncs across mobile and desktop devices, and can be used offline. I am seeing that using PouchDB is infinitely easier than having to write a PHP\/MySQL backend.<\/p>\n<p>I have been using it successfully, and when I make conflicting changes on offline devices, CouchDB uses an algorithm to arbitrarily pick a winner and then correctly pushes it to all the devices.<\/p>\n<p>What I would like to do is implement a custom algorithm to merge conflicting records. Here is the algorithm I would like to use:<\/p>\n<ol>\n<li>If a record is deleted on one client and merely updated on another, the updated version wins, unless both clients agree on the delete.<\/li>\n<li>The record with the most recent &#8220;modified&#8221; timestamp becomes the master, and the older record becomes the secondary.<\/li>\n<li>Any fields that exist only in the secondary (or are empty in the master) are moved over to the master.<\/li>\n<li>The master revision is saved and the secondary is deleted.<\/li>\n<\/ol>\n<p>CouchDB&#8217;s guide has a good explanation, but I don&#8217;t have a clue how to implement it with the PouchDB API during a continuous replication. According to the PouchDB API, there is an &#8220;onChange&#8221; listener in the replicate options, but I don&#8217;t understand how to use it to intercept conflicts.<\/p>\n<p>If someone could write a brief tutorial including some sample code, myself and I&#8217;m sure many other PouchDB users would appreciate it!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m new to both CouchDB and PouchDB and am using it to create a contact management system that syncs across mobile and desktop devices, and can be used offline. I am seeing that using PouchDB is infinitely easier than having to write a PHP\/MySQL backend. I have been using it successfully, and when I make [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8114","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=8114"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8114\/revisions"}],"predecessor-version":[{"id":8611,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8114\/revisions\/8611"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=8114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=8114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=8114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}