How to resolve conflicts with continuous replication

前端 未结 1 1714
独厮守ぢ
独厮守ぢ 2020-12-13 16:31

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

相关标签:
1条回答
  • 2020-12-13 16:56

    Writing an article with examples of exactly how to manage conflict resolution is a really good idea, it can be confusing, but with the lack of one

    The idea is the exact same as CouchDB, to resolve conflicts you delete the revisions that didnt win (and write a new winner if needed)

    #1 is how CouchDB conflict resolution works anyway, so you dont need to worry about that, deleted leafs dont conflict

    function onChange(doc) { 
      if (!doc._conflicts) return;
        collectConflicts(doc._conflicts, function(docs) {
          var master = docs.sort(byTime).unshift();
          for (var doc in docs) {
            for (var prop in doc) {
              if (!(prop in master)) { 
                master[prop] = doc[prop];
              } 
            }
          }
          db.put(master, function(err) {
            if (!err) { 
              for (var doc in docs) {
                db.remove(doc);
              }
            }
          });     
        });
      }
    }
    
    db.changes({conflicts: true, onChange: onChange});
    

    This will need error handling etc and could be written much nicer, was just a quick napkin drawing of what the code could look like

    0 讨论(0)
提交回复
热议问题