2014-01-28 20:54:41 +00:00
|
|
|
var loopback = require('../../');
|
|
|
|
var app = loopback();
|
2016-04-01 09:14:26 +00:00
|
|
|
var db = app.dataSource('db', { connector: loopback.Memory });
|
|
|
|
var Color = app.model('color', { dataSource: 'db', options: { trackChanges: true }});
|
|
|
|
var Color2 = app.model('color2', { dataSource: 'db', options: { trackChanges: true }});
|
2014-01-28 20:54:41 +00:00
|
|
|
var target = Color2;
|
|
|
|
var source = Color;
|
|
|
|
var SPEED = process.env.SPEED || 100;
|
|
|
|
var conflicts;
|
|
|
|
|
|
|
|
var steps = [
|
|
|
|
|
|
|
|
createSomeInitialSourceData,
|
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
replicateSourceToTarget,
|
|
|
|
list.bind(this, source, 'current SOURCE data'),
|
|
|
|
list.bind(this, target, 'current TARGET data'),
|
2014-01-28 20:54:41 +00:00
|
|
|
|
|
|
|
updateSomeTargetData,
|
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
replicateSourceToTarget,
|
|
|
|
list.bind(this, source, 'current SOURCE data '),
|
|
|
|
list.bind(this, target, 'current TARGET data (includes conflicting update)'),
|
2014-01-28 20:54:41 +00:00
|
|
|
|
|
|
|
updateSomeSourceDataCausingAConflict,
|
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
replicateSourceToTarget,
|
|
|
|
list.bind(this, source, 'current SOURCE data (now has a conflict)'),
|
|
|
|
list.bind(this, target, 'current TARGET data (includes conflicting update)'),
|
2014-01-28 20:54:41 +00:00
|
|
|
|
|
|
|
resolveAllConflicts,
|
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
replicateSourceToTarget,
|
|
|
|
list.bind(this, source, 'current SOURCE data (conflict resolved)'),
|
|
|
|
list.bind(this, target, 'current TARGET data (conflict resolved)'),
|
2014-01-28 20:54:41 +00:00
|
|
|
|
|
|
|
createMoreSourceData,
|
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
replicateSourceToTarget,
|
|
|
|
list.bind(this, source, 'current SOURCE data'),
|
|
|
|
list.bind(this, target, 'current TARGET data'),
|
2014-01-28 20:54:41 +00:00
|
|
|
|
|
|
|
createEvenMoreSourceData,
|
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
replicateSourceToTarget,
|
|
|
|
list.bind(this, source, 'current SOURCE data'),
|
|
|
|
list.bind(this, target, 'current TARGET data'),
|
2014-01-28 20:54:41 +00:00
|
|
|
|
|
|
|
deleteAllSourceData,
|
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
replicateSourceToTarget,
|
|
|
|
list.bind(this, source, 'current SOURCE data (empty)'),
|
|
|
|
list.bind(this, target, 'current TARGET data (empty)'),
|
2014-01-28 20:54:41 +00:00
|
|
|
|
|
|
|
createSomeNewSourceData,
|
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
replicateSourceToTarget,
|
|
|
|
list.bind(this, source, 'current SOURCE data'),
|
|
|
|
list.bind(this, target, 'current TARGET data'),
|
2014-01-28 20:54:41 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
run(steps);
|
|
|
|
|
|
|
|
function createSomeInitialSourceData() {
|
|
|
|
Color.create([
|
2016-04-01 09:14:26 +00:00
|
|
|
{ name: 'red' },
|
|
|
|
{ name: 'blue' },
|
|
|
|
{ name: 'green' },
|
2014-01-28 20:54:41 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function replicateSourceToTarget() {
|
|
|
|
Color.replicate(0, Color2, {}, function(err, replicationConflicts) {
|
|
|
|
conflicts = replicationConflicts;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function resolveAllConflicts() {
|
2016-04-01 09:14:26 +00:00
|
|
|
if (conflicts.length) {
|
2014-01-28 20:54:41 +00:00
|
|
|
conflicts.forEach(function(conflict) {
|
|
|
|
conflict.resolve();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateSomeTargetData() {
|
|
|
|
Color2.findById(1, function(err, color) {
|
|
|
|
color.name = 'conflict';
|
|
|
|
color.save();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function createMoreSourceData() {
|
2016-04-01 09:14:26 +00:00
|
|
|
Color.create({ name: 'orange' });
|
2014-01-28 20:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function createEvenMoreSourceData() {
|
2016-04-01 09:14:26 +00:00
|
|
|
Color.create({ name: 'black' });
|
2014-01-28 20:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateSomeSourceDataCausingAConflict() {
|
|
|
|
Color.findById(1, function(err, color) {
|
|
|
|
color.name = 'red!!!!';
|
|
|
|
color.save();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteAllSourceData() {
|
|
|
|
Color.destroyAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
function createSomeNewSourceData() {
|
|
|
|
Color.create([
|
2016-04-01 09:14:26 +00:00
|
|
|
{ name: 'violet' },
|
|
|
|
{ name: 'amber' },
|
|
|
|
{ name: 'olive' },
|
2014-01-28 20:54:41 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function list(model, msg) {
|
|
|
|
console.log(msg);
|
|
|
|
model.find(function(err, items) {
|
|
|
|
items.forEach(function(item) {
|
|
|
|
console.log(' -', item.name);
|
|
|
|
});
|
|
|
|
console.log();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function run(steps) {
|
|
|
|
setInterval(function() {
|
|
|
|
var step = steps.shift();
|
2016-04-01 09:14:26 +00:00
|
|
|
if (step) {
|
2014-01-28 20:54:41 +00:00
|
|
|
console.log(step.name);
|
|
|
|
step();
|
|
|
|
}
|
|
|
|
}, SPEED);
|
|
|
|
}
|