2014-01-26 21:20:19 +00:00
|
|
|
/**
|
|
|
|
* Module Dependencies.
|
|
|
|
*/
|
|
|
|
|
2014-04-30 00:17:49 +00:00
|
|
|
var DataModel = require('./data-model')
|
2014-01-26 21:20:19 +00:00
|
|
|
, loopback = require('../loopback')
|
|
|
|
, crypto = require('crypto')
|
|
|
|
, CJSON = {stringify: require('canonical-json')}
|
|
|
|
, async = require('async')
|
|
|
|
, assert = require('assert');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Properties
|
|
|
|
*/
|
|
|
|
|
|
|
|
var properties = {
|
|
|
|
id: {type: String, generated: true, id: true},
|
|
|
|
rev: {type: String},
|
|
|
|
prev: {type: String},
|
|
|
|
checkpoint: {type: Number},
|
|
|
|
modelName: {type: String},
|
|
|
|
modelId: {type: String}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Options
|
|
|
|
*/
|
|
|
|
|
|
|
|
var options = {
|
2014-01-28 20:54:41 +00:00
|
|
|
trackChanges: false
|
2014-01-26 21:20:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change list entry.
|
|
|
|
*
|
|
|
|
* @property id {String} Hash of the modelName and id
|
|
|
|
* @property rev {String} the current model revision
|
|
|
|
* @property prev {String} the previous model revision
|
|
|
|
* @property checkpoint {Number} the current checkpoint at time of the change
|
|
|
|
* @property modelName {String} the model name
|
|
|
|
* @property modelId {String} the model id
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @inherits {Model}
|
|
|
|
*/
|
|
|
|
|
2014-04-30 00:17:49 +00:00
|
|
|
var Change = module.exports = DataModel.extend('Change', properties, options);
|
2014-01-26 21:20:19 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.UPDATE = 'update';
|
|
|
|
Change.CREATE = 'create';
|
|
|
|
Change.DELETE = 'delete';
|
|
|
|
Change.UNKNOWN = 'unknown';
|
|
|
|
|
2014-01-28 20:54:41 +00:00
|
|
|
/*!
|
|
|
|
* Conflict Class
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.Conflict = Conflict;
|
|
|
|
|
2014-01-26 21:20:19 +00:00
|
|
|
/*!
|
|
|
|
* Setup the extended model.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.setup = function() {
|
2014-05-14 16:20:04 +00:00
|
|
|
DataModel.setup.call(this);
|
2014-01-26 21:20:19 +00:00
|
|
|
var Change = this;
|
|
|
|
|
|
|
|
Change.getter.id = function() {
|
|
|
|
var hasModel = this.modelName && this.modelId;
|
|
|
|
if(!hasModel) return null;
|
|
|
|
|
|
|
|
return Change.idForModel(this.modelName, this.modelId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Change.setup();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Track the recent change of the given modelIds.
|
|
|
|
*
|
|
|
|
* @param {String} modelName
|
|
|
|
* @param {Array} modelIds
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {Array} changes Changes that were tracked
|
|
|
|
*/
|
|
|
|
|
2014-02-05 23:27:58 +00:00
|
|
|
Change.rectifyModelChanges = function(modelName, modelIds, callback) {
|
2014-01-26 21:20:19 +00:00
|
|
|
var tasks = [];
|
|
|
|
var Change = this;
|
|
|
|
|
|
|
|
modelIds.forEach(function(id) {
|
|
|
|
tasks.push(function(cb) {
|
2014-05-19 22:56:26 +00:00
|
|
|
Change.findOrCreateChange(modelName, id, function(err, change) {
|
2014-01-26 21:20:19 +00:00
|
|
|
if(err) return Change.handleError(err, cb);
|
|
|
|
change.rectify(cb);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
async.parallel(tasks, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an identifier for a given model.
|
|
|
|
*
|
|
|
|
* @param {String} modelName
|
|
|
|
* @param {String} modelId
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.idForModel = function(modelName, modelId) {
|
|
|
|
return this.hash([modelName, modelId].join('-'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find or create a change for the given model.
|
|
|
|
*
|
|
|
|
* @param {String} modelName
|
|
|
|
* @param {String} modelId
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {Change} change
|
|
|
|
* @end
|
|
|
|
*/
|
|
|
|
|
2014-05-19 22:56:26 +00:00
|
|
|
Change.findOrCreateChange = function(modelName, modelId, callback) {
|
2014-02-05 23:32:38 +00:00
|
|
|
assert(loopback.getModel(modelName), modelName + ' does not exist');
|
2014-01-26 21:20:19 +00:00
|
|
|
var id = this.idForModel(modelName, modelId);
|
|
|
|
var Change = this;
|
|
|
|
|
|
|
|
this.findById(id, function(err, change) {
|
|
|
|
if(err) return callback(err);
|
|
|
|
if(change) {
|
|
|
|
callback(null, change);
|
|
|
|
} else {
|
|
|
|
var ch = new Change({
|
|
|
|
id: id,
|
|
|
|
modelName: modelName,
|
|
|
|
modelId: modelId
|
|
|
|
});
|
|
|
|
ch.save(callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update (or create) the change with the current revision.
|
|
|
|
*
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {Change} change
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.prototype.rectify = function(cb) {
|
|
|
|
var change = this;
|
2014-01-28 20:54:41 +00:00
|
|
|
var tasks = [
|
|
|
|
updateRevision,
|
|
|
|
updateCheckpoint
|
|
|
|
];
|
|
|
|
|
|
|
|
if(this.rev) this.prev = this.rev;
|
|
|
|
|
|
|
|
async.parallel(tasks, function(err) {
|
|
|
|
if(err) return cb(err);
|
2014-01-26 21:20:19 +00:00
|
|
|
change.save(cb);
|
|
|
|
});
|
2014-01-28 20:54:41 +00:00
|
|
|
|
|
|
|
function updateRevision(cb) {
|
|
|
|
// get the current revision
|
|
|
|
change.currentRevision(function(err, rev) {
|
|
|
|
if(err) return Change.handleError(err, cb);
|
|
|
|
change.rev = rev;
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateCheckpoint(cb) {
|
|
|
|
change.constructor.getCheckpointModel().current(function(err, checkpoint) {
|
|
|
|
if(err) return Change.handleError(err);
|
2014-05-10 00:19:32 +00:00
|
|
|
change.checkpoint = checkpoint;
|
2014-01-28 20:54:41 +00:00
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
2014-01-26 21:20:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a change's current revision based on current data.
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {String} rev The current revision
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.prototype.currentRevision = function(cb) {
|
|
|
|
var model = this.getModelCtor();
|
2014-05-16 00:27:02 +00:00
|
|
|
var id = this.getModelId();
|
|
|
|
model.findById(id, function(err, inst) {
|
2014-01-26 21:20:19 +00:00
|
|
|
if(err) return Change.handleError(err, cb);
|
|
|
|
if(inst) {
|
|
|
|
cb(null, Change.revisionForInst(inst));
|
|
|
|
} else {
|
|
|
|
cb(null, null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a hash of the given `string` with the `options.hashAlgorithm`.
|
|
|
|
* **Default: `sha1`**
|
|
|
|
*
|
|
|
|
* @param {String} str The string to be hashed
|
|
|
|
* @return {String} The hashed string
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.hash = function(str) {
|
|
|
|
return crypto
|
|
|
|
.createHash(Change.settings.hashAlgorithm || 'sha1')
|
|
|
|
.update(str)
|
|
|
|
.digest('hex');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the revision string for the given object
|
|
|
|
* @param {Object} inst The data to get the revision string for
|
|
|
|
* @return {String} The revision string
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.revisionForInst = function(inst) {
|
|
|
|
return this.hash(CJSON.stringify(inst));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a change's type. Returns one of:
|
|
|
|
*
|
|
|
|
* - `Change.UPDATE`
|
|
|
|
* - `Change.CREATE`
|
|
|
|
* - `Change.DELETE`
|
|
|
|
* - `Change.UNKNOWN`
|
|
|
|
*
|
|
|
|
* @return {String} the type of change
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.prototype.type = function() {
|
|
|
|
if(this.rev && this.prev) {
|
|
|
|
return Change.UPDATE;
|
|
|
|
}
|
|
|
|
if(this.rev && !this.prev) {
|
|
|
|
return Change.CREATE;
|
|
|
|
}
|
|
|
|
if(!this.rev && this.prev) {
|
|
|
|
return Change.DELETE;
|
|
|
|
}
|
|
|
|
return Change.UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare two changes.
|
|
|
|
* @param {Change} change
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.prototype.equals = function(change) {
|
2014-04-30 00:17:49 +00:00
|
|
|
if(!change) return false;
|
2014-05-14 16:20:04 +00:00
|
|
|
var thisRev = this.rev || null;
|
|
|
|
var thatRev = change.rev || null;
|
|
|
|
return thisRev === thatRev;
|
2014-01-26 21:20:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the change is based on the given change.
|
|
|
|
* @param {Change} change
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.prototype.isBasedOn = function(change) {
|
|
|
|
return this.prev === change.rev;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the differences for a given model since a given checkpoint.
|
|
|
|
*
|
|
|
|
* The callback will contain an error or `result`.
|
|
|
|
*
|
|
|
|
* **result**
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* {
|
|
|
|
* deltas: Array,
|
|
|
|
* conflicts: Array
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* **deltas**
|
|
|
|
*
|
|
|
|
* An array of changes that differ from `remoteChanges`.
|
|
|
|
*
|
|
|
|
* **conflicts**
|
|
|
|
*
|
|
|
|
* An array of changes that conflict with `remoteChanges`.
|
|
|
|
*
|
|
|
|
* @param {String} modelName
|
|
|
|
* @param {Number} since Compare changes after this checkpoint
|
|
|
|
* @param {Change[]} remoteChanges A set of changes to compare
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {Object} result See above.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.diff = function(modelName, since, remoteChanges, callback) {
|
|
|
|
var remoteChangeIndex = {};
|
|
|
|
var modelIds = [];
|
|
|
|
remoteChanges.forEach(function(ch) {
|
|
|
|
modelIds.push(ch.modelId);
|
|
|
|
remoteChangeIndex[ch.modelId] = new Change(ch);
|
|
|
|
});
|
|
|
|
|
|
|
|
// normalize `since`
|
|
|
|
since = Number(since) || 0;
|
|
|
|
this.find({
|
|
|
|
where: {
|
|
|
|
modelName: modelName,
|
|
|
|
modelId: {inq: modelIds},
|
2014-05-10 00:19:32 +00:00
|
|
|
checkpoint: {gte: since}
|
2014-01-26 21:20:19 +00:00
|
|
|
}
|
|
|
|
}, function(err, localChanges) {
|
|
|
|
if(err) return callback(err);
|
|
|
|
var deltas = [];
|
|
|
|
var conflicts = [];
|
2014-01-28 20:54:41 +00:00
|
|
|
var localModelIds = [];
|
|
|
|
|
2014-01-26 21:20:19 +00:00
|
|
|
localChanges.forEach(function(localChange) {
|
2014-04-30 00:17:49 +00:00
|
|
|
localChange = new Change(localChange);
|
2014-01-28 20:54:41 +00:00
|
|
|
localModelIds.push(localChange.modelId);
|
2014-01-26 21:20:19 +00:00
|
|
|
var remoteChange = remoteChangeIndex[localChange.modelId];
|
2014-04-30 00:17:49 +00:00
|
|
|
if(remoteChange && !localChange.equals(remoteChange)) {
|
2014-01-26 21:20:19 +00:00
|
|
|
if(remoteChange.isBasedOn(localChange)) {
|
|
|
|
deltas.push(remoteChange);
|
|
|
|
} else {
|
|
|
|
conflicts.push(localChange);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-01-28 20:54:41 +00:00
|
|
|
modelIds.forEach(function(id) {
|
|
|
|
if(localModelIds.indexOf(id) === -1) {
|
|
|
|
deltas.push(remoteChangeIndex[id]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-01-26 21:20:19 +00:00
|
|
|
callback(null, {
|
|
|
|
deltas: deltas,
|
|
|
|
conflicts: conflicts
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2014-01-28 20:54:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Correct all change list entries.
|
|
|
|
* @param {Function} callback
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.rectifyAll = function(cb) {
|
2014-05-06 20:31:23 +00:00
|
|
|
var Change = this;
|
2014-01-28 20:54:41 +00:00
|
|
|
// this should be optimized
|
|
|
|
this.find(function(err, changes) {
|
|
|
|
if(err) return cb(err);
|
|
|
|
changes.forEach(function(change) {
|
2014-05-06 20:31:23 +00:00
|
|
|
change = new Change(change);
|
2014-01-28 20:54:41 +00:00
|
|
|
change.rectify();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the checkpoint model.
|
|
|
|
* @return {Checkpoint}
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.getCheckpointModel = function() {
|
|
|
|
var checkpointModel = this.Checkpoint;
|
|
|
|
if(checkpointModel) return checkpointModel;
|
|
|
|
this.checkpoint = checkpointModel = require('./checkpoint').extend('checkpoint');
|
2014-04-16 14:33:17 +00:00
|
|
|
assert(this.dataSource, 'Cannot getCheckpointModel(): ' + this.modelName
|
|
|
|
+ ' is not attached to a dataSource');
|
2014-01-28 20:54:41 +00:00
|
|
|
checkpointModel.attachTo(this.dataSource);
|
|
|
|
return checkpointModel;
|
|
|
|
}
|
|
|
|
|
2014-05-06 20:31:23 +00:00
|
|
|
Change.handleError = function(err) {
|
|
|
|
if(!this.settings.ignoreErrors) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
2014-01-28 20:54:41 +00:00
|
|
|
|
2014-05-16 00:27:02 +00:00
|
|
|
/**
|
|
|
|
* Get the `Model` class for `change.modelName`.
|
|
|
|
* @return {Model}
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.prototype.getModelCtor = function() {
|
|
|
|
return this.constructor.settings.trackModel;
|
|
|
|
}
|
|
|
|
|
2014-05-10 00:19:32 +00:00
|
|
|
Change.prototype.getModelId = function() {
|
|
|
|
// TODO(ritch) get rid of the need to create an instance
|
2014-05-16 00:27:02 +00:00
|
|
|
var Model = this.getModelCtor();
|
2014-05-10 00:19:32 +00:00
|
|
|
var id = this.modelId;
|
|
|
|
var m = new Model();
|
|
|
|
m.setId(id);
|
|
|
|
return m.getId();
|
|
|
|
}
|
|
|
|
|
2014-05-14 16:20:04 +00:00
|
|
|
Change.prototype.getModel = function(callback) {
|
|
|
|
var Model = this.constructor.settings.trackModel;
|
|
|
|
var id = this.getModelId();
|
|
|
|
Model.findById(id, callback);
|
|
|
|
}
|
|
|
|
|
2014-01-28 20:54:41 +00:00
|
|
|
/**
|
|
|
|
* When two changes conflict a conflict is created.
|
|
|
|
*
|
|
|
|
* **Note: call `conflict.fetch()` to get the `target` and `source` models.
|
|
|
|
*
|
2014-05-14 16:20:04 +00:00
|
|
|
* @param {*} sourceModelId
|
|
|
|
* @param {*} targetModelId
|
|
|
|
* @property {ModelClass} source The source model instance
|
|
|
|
* @property {ModelClass} target The target model instance
|
2014-01-28 20:54:41 +00:00
|
|
|
*/
|
|
|
|
|
2014-05-14 16:20:04 +00:00
|
|
|
function Conflict(modelId, SourceModel, TargetModel) {
|
|
|
|
this.SourceModel = SourceModel;
|
|
|
|
this.TargetModel = TargetModel;
|
|
|
|
this.SourceChange = SourceModel.getChangeModel();
|
|
|
|
this.TargetChange = TargetModel.getChangeModel();
|
|
|
|
this.modelId = modelId;
|
2014-01-28 20:54:41 +00:00
|
|
|
}
|
|
|
|
|
2014-05-14 16:20:04 +00:00
|
|
|
/**
|
|
|
|
* Fetch the conflicting models.
|
|
|
|
*
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error}
|
|
|
|
* @param {DataModel} source
|
|
|
|
* @param {DataModel} target
|
|
|
|
*/
|
|
|
|
|
|
|
|
Conflict.prototype.models = function(cb) {
|
2014-01-28 20:54:41 +00:00
|
|
|
var conflict = this;
|
2014-05-14 16:20:04 +00:00
|
|
|
var SourceModel = this.SourceModel;
|
|
|
|
var TargetModel = this.TargetModel;
|
|
|
|
var source;
|
|
|
|
var target;
|
|
|
|
|
|
|
|
async.parallel([
|
2014-01-28 20:54:41 +00:00
|
|
|
getSourceModel,
|
|
|
|
getTargetModel
|
2014-05-14 16:20:04 +00:00
|
|
|
], done);
|
2014-01-28 20:54:41 +00:00
|
|
|
|
2014-05-14 16:20:04 +00:00
|
|
|
function getSourceModel(cb) {
|
|
|
|
SourceModel.findById(conflict.modelId, function(err, model) {
|
2014-01-28 20:54:41 +00:00
|
|
|
if(err) return cb(err);
|
2014-05-14 16:20:04 +00:00
|
|
|
source = model;
|
2014-01-28 20:54:41 +00:00
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTargetModel(cb) {
|
2014-05-14 16:20:04 +00:00
|
|
|
TargetModel.findById(conflict.modelId, function(err, model) {
|
2014-01-28 20:54:41 +00:00
|
|
|
if(err) return cb(err);
|
2014-05-14 16:20:04 +00:00
|
|
|
target = model;
|
2014-01-28 20:54:41 +00:00
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
2014-05-14 16:20:04 +00:00
|
|
|
|
|
|
|
function done(err) {
|
|
|
|
if(err) return cb(err);
|
|
|
|
cb(null, source, target);
|
|
|
|
}
|
2014-01-28 20:54:41 +00:00
|
|
|
}
|
|
|
|
|
2014-05-14 16:20:04 +00:00
|
|
|
/**
|
|
|
|
* Get the conflicting changes.
|
|
|
|
*
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {Change} sourceChange
|
|
|
|
* @param {Change} targetChange
|
|
|
|
*/
|
|
|
|
|
|
|
|
Conflict.prototype.changes = function(cb) {
|
|
|
|
var conflict = this;
|
|
|
|
var sourceChange;
|
|
|
|
var targetChange;
|
|
|
|
|
|
|
|
async.parallel([
|
|
|
|
getSourceChange,
|
|
|
|
getTargetChange
|
|
|
|
], done);
|
|
|
|
|
|
|
|
function getSourceChange(cb) {
|
|
|
|
conflict.SourceChange.findOne({
|
|
|
|
modelId: conflict.sourceModelId
|
|
|
|
}, function(err, change) {
|
|
|
|
if(err) return cb(err);
|
|
|
|
sourceChange = change;
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTargetChange(cb) {
|
|
|
|
debugger;
|
|
|
|
conflict.TargetChange.findOne({
|
|
|
|
modelId: conflict.targetModelId
|
|
|
|
}, function(err, change) {
|
|
|
|
if(err) return cb(err);
|
|
|
|
targetChange = change;
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function done(err) {
|
|
|
|
if(err) return cb(err);
|
|
|
|
cb(null, sourceChange, targetChange);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve the conflict.
|
|
|
|
*
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
*/
|
|
|
|
|
2014-01-28 20:54:41 +00:00
|
|
|
Conflict.prototype.resolve = function(cb) {
|
2014-05-14 16:20:04 +00:00
|
|
|
var conflict = this;
|
|
|
|
conflict.changes(function(err, sourceChange, targetChange) {
|
|
|
|
if(err) return callback(err);
|
|
|
|
sourceChange.prev = targetChange.rev;
|
|
|
|
sourceChange.save(cb);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the conflict type.
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* // possible results are
|
|
|
|
* Change.UPDATE // => source and target models were updated
|
|
|
|
* Change.DELETE // => the source and or target model was deleted
|
|
|
|
* Change.UNKNOWN // => the conflict type is uknown or due to an error
|
|
|
|
* ```
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {String} type The conflict type.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Conflict.prototype.type = function(cb) {
|
|
|
|
var conflict = this;
|
|
|
|
this.changes(function(err, sourceChange, targetChange) {
|
|
|
|
if(err) return cb(err);
|
|
|
|
var sourceChangeType = sourceChange.type();
|
|
|
|
var targetChangeType = targetChange.type();
|
|
|
|
if(sourceChangeType === Change.UPDATE && targetChangeType === Change.UPDATE) {
|
|
|
|
return cb(null, Change.UPDATE);
|
|
|
|
}
|
|
|
|
if(sourceChangeType === Change.DELETE || targetChangeType === Change.DELETE) {
|
|
|
|
return cb(null, Change.DELETE);
|
|
|
|
}
|
|
|
|
return cb(null, Change.UNKNOWN);
|
|
|
|
});
|
2014-01-28 20:54:41 +00:00
|
|
|
}
|