PouchDB: How to resolve conflicts with continuous replication?-open source projects pouchdb/pouchdb
I’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 conflicting changes on offline devices, CouchDB uses an algorithm to arbitrarily pick a winner and then correctly pushes it to all the devices.
What I would like to do is implement a custom algorithm to merge conflicting records. Here is the algorithm I would like to use:
- If a record is deleted on one client and merely updated on another, the updated version wins, unless both clients agree on the delete.
- The record with the most recent “modified” timestamp becomes the master, and the older record becomes the secondary.
- Any fields that exist only in the secondary (or are empty in the master) are moved over to the master.
- The master revision is saved and the secondary is deleted.
CouchDB’s guide has a good explanation, but I don’t have a clue how to implement it with the PouchDB API during a continuous replication. According to the PouchDB API, there is an “onChange” listener in the replicate options, but I don’t understand how to use it to intercept conflicts.
If someone could write a brief tutorial including some sample code, myself and I’m sure many other PouchDB users would appreciate it!