see on GitHub

Conflict Resolution



The Conflicts Resolution View

Figure 1. Conflicts Resolution View

Conflict Resolution

  1. Save, Add, Modify or Delete conflict resolution scripts.

  2. Set default behaviour - Conflicts will be created only if:

    • This option is unchecked and no script is defined
    • This option is unchecked and the script is defined but returns null
  3. Resolution Script (optional)

    • Supply a javaScript function to resolve the conflicting documents.

    • The object that is returned by the script will be used as the conflict resolution in the document.

    • The sript is defined per collection

    • Note: in case the script returns null (either intentionally, or upon some error),
      the server will resolve the conflict according to the defined default behaviour
      (i.e. resolve by using latest version or creating a conflict for the user to resolve).

    • Script Variables:

      • docs - the conflicted documents objects array
      • hasTombstone - true if either of the conflicted documents was deleted
      • resolveToTombstone - return this value from script if the resolution wanted is to delete this document

Script Examples

  • 1. Resolve according to field content - return the highest value of the field

// First conflicting document
{
    "Name": "John",
    "MaxRecord": 43
}

// Second conflicting document
{
    "Name": "John",
    "MaxRecord": 80
}

// The resolving script:
var maxRecord = 0;
for (var i = 0; i < docs.length; i++) {
    maxRecord = Math.max(docs[i].maxRecord, maxRecord);
}
docs[0].MaxRecord = maxRecord;

return docs[0];

  • 2. Resolve by deleting the document

if (hasTombstone) {
    return resolveToTombstone;
}

  • 3. The metadata can also be accessed - return the document that has the largest number of attachments

var result = docs[0];

for (var i = 1; i < docs.length; i++) {
     if (docs[i]["@metadata"]["@attachments"].length > result["@metadata"]["@attachments"].length)
        result = docs[i];
    }
}

return result;