Blame view

node_modules/loopback/common/models/change.js 21.4 KB
f7563de62   Palak Handa   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
  // Copyright IBM Corp. 2014,2016. All Rights Reserved.
  // Node module: loopback
  // This file is licensed under the MIT License.
  // License text available at https://opensource.org/licenses/MIT
  
  /*!
   * Module Dependencies.
   */
  
  var g = require('strong-globalize')();
  
  var PersistedModel = require('../../lib/loopback').PersistedModel;
  var loopback = require('../../lib/loopback');
  var utils = require('../../lib/utils');
  var crypto = require('crypto');
  var CJSON = {stringify: require('canonical-json')};
  var async = require('async');
  var assert = require('assert');
  var debug = require('debug')('loopback:change');
  var deprecate = require('depd')('loopback');
  
  /**
   * Change list entry.
   *
   * @property {String} id Hash of the modelName and ID.
   * @property {String} rev The current model revision.
   * @property {String} prev The previous model revision.
   * @property {Number} checkpoint The current checkpoint at time of the change.
   * @property {String} modelName Model name.
   * @property {String} modelId Model ID.
   * @property {Object} settings Extends the `Model.settings` object.
   * @property {String} settings.hashAlgorithm Algorithm used to create cryptographic hash, used as argument
   * to [crypto.createHash](http://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm).  Default is sha1.
   * @property {Boolean} settings.ignoreErrors By default, when changes are rectified, an error will throw an exception.
   * However, if this setting is true, then errors will not throw exceptions.
   * @class Change
   * @inherits {PersistedModel}
   */
  
  module.exports = function(Change) {
  
    /*!
     * Constants
     */
  
    Change.UPDATE = 'update';
    Change.CREATE = 'create';
    Change.DELETE = 'delete';
    Change.UNKNOWN = 'unknown';
  
    /*!
     * Conflict Class
     */
  
    Change.Conflict = Conflict;
  
    /*!
     * Setup the extended model.
     */
  
    Change.setup = function() {
      PersistedModel.setup.call(this);
      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
     */
  
    Change.rectifyModelChanges = function(modelName, modelIds, callback) {
      var Change = this;
      var errors = [];
  
      callback = callback || utils.createPromiseCallback();
  
      var tasks = modelIds.map(function(id) {
        return function(cb) {
          Change.findOrCreateChange(modelName, id, function(err, change) {
            if (err) return next(err);
            change.rectify(next);
          });
  
          function next(err) {
            if (err) {
              err.modelName = modelName;
              err.modelId = id;
              errors.push(err);
            }
            cb();
          }
        };
      });
  
      async.parallel(tasks, function(err) {
        if (err) return callback(err);
        if (errors.length) {
          var desc = errors
            .map(function(e) {
              return '#' + e.modelId + ' - ' + e.toString();
            })
            .join('
  ');
  
          var msg = g.f('Cannot rectify %s changes:
  %s', modelName, desc);
          err = new Error(msg);
          err.details = { errors: errors };
          return callback(err);
        }
        callback();
      });
      return callback.promise;
    };
  
    /**
     * 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
     */
  
    Change.findOrCreateChange = function(modelName, modelId, callback) {
      assert(this.registry.findModel(modelName), modelName + ' does not exist');
      callback = callback || utils.createPromiseCallback();
      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.debug('creating change');
          Change.updateOrCreate(ch, callback);
        }
      });
      return callback.promise;
    };
  
    /**
     * 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;
      var currentRev = this.rev;
  
      change.debug('rectify change');
  
      cb = cb || utils.createPromiseCallback();
  
      change.currentRevision(function(err, rev) {
        if (err) return cb(err);
  
        // avoid setting rev and prev to the same value
        if (currentRev === rev) {
          change.debug('rev and prev are equal (not updating anything)');
          return cb(null, change);
        }
  
        // FIXME(@bajtos) Allo callers to pass in the checkpoint value
        // (or even better - a memoized async function to get the cp value)
        // That will enable `rectifyAll` to cache the checkpoint value
        change.constructor.getCheckpointModel().current(
          function(err, checkpoint) {
            if (err) return cb(err);
            doRectify(checkpoint, rev);
          }
        );
      });
      return cb.promise;
  
      function doRectify(checkpoint, rev) {
        if (rev) {
          if (currentRev === rev) {
            change.debug('ASSERTION FAILED: Change currentRev==rev ' +
              'should have been already handled');
            return cb(null, change);
          } else {
            change.rev = rev;
            change.debug('updated revision (was ' + currentRev + ')');
            if (change.checkpoint !== checkpoint) {
              // previous revision is updated only across checkpoints
              change.prev = currentRev;
              change.debug('updated prev');
            }
          }
        } else {
          change.rev = null;
          change.debug('updated revision (was ' + currentRev + ')');
          if (change.checkpoint !== checkpoint) {
            // previous revision is updated only across checkpoints
            if (currentRev) {
              change.prev = currentRev;
            } else if (!change.prev) {
              change.debug('ERROR - could not determing prev');
              change.prev = Change.UNKNOWN;
            }
            change.debug('updated prev');
          }
        }
  
        if (change.checkpoint != checkpoint) {
          debug('update checkpoint to', checkpoint);
          change.checkpoint = checkpoint;
        }
  
        if (change.prev === Change.UNKNOWN) {
          // this occurs when a record of a change doesn't exist
          // and its current revision is null (not found)
          change.remove(cb);
        } else {
          change.save(cb);
        }
      }
    };
  
    /**
     * 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) {
      cb = cb || utils.createPromiseCallback();
      var model = this.getModelCtor();
      var id = this.getModelId();
      model.findById(id, function(err, inst) {
        if (err) return cb(err);
        if (inst) {
          cb(null, Change.revisionForInst(inst));
        } else {
          cb(null, null);
        }
      });
      return cb.promise;
    };
  
    /**
     * 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) {
      assert(inst, 'Change.revisionForInst() requires an instance object.');
      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) {
      if (!change) return false;
      var thisRev = this.rev || null;
      var thatRev = change.rev || null;
      return thisRev === thatRev;
    };
  
    /**
     * Does this change conflict with the given change.
     * @param  {Change} change
     * @return {Boolean}
     */
  
    Change.prototype.conflictsWith = function(change) {
      if (!change) return false;
      if (this.equals(change)) return false;
      if (Change.bothDeleted(this, change)) return false;
      if (this.isBasedOn(change)) return false;
      return true;
    };
  
    /**
     * Are both changes deletes?
     * @param  {Change} a
     * @param  {Change} b
     * @return {Boolean}
     */
  
    Change.bothDeleted = function(a, b) {
      return a.type() === Change.DELETE &&
        b.type() === Change.DELETE;
    };
  
    /**
     * 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) {
      callback = callback || utils.createPromiseCallback();
  
      if (!Array.isArray(remoteChanges) || remoteChanges.length === 0) {
        callback(null, {deltas: [], conflicts: []});
        return callback.promise;
      }
      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}
        }
      }, function(err, allLocalChanges) {
        if (err) return callback(err);
        var deltas = [];
        var conflicts = [];
        var localModelIds = [];
  
        var localChanges = allLocalChanges.filter(function(c) {
          return c.checkpoint >= since;
        });
  
        localChanges.forEach(function(localChange) {
          localChange = new Change(localChange);
          localModelIds.push(localChange.modelId);
          var remoteChange = remoteChangeIndex[localChange.modelId];
          if (remoteChange && !localChange.equals(remoteChange)) {
            if (remoteChange.conflictsWith(localChange)) {
              remoteChange.debug('remote conflict');
              localChange.debug('local conflict');
              conflicts.push(localChange);
            } else {
              remoteChange.debug('remote delta');
              deltas.push(remoteChange);
            }
          }
        });
  
        modelIds.forEach(function(id) {
          if (localModelIds.indexOf(id) !== -1) return;
  
          var d = remoteChangeIndex[id];
          var oldChange = allLocalChanges.filter(function(c) {
            return c.modelId === id;
          })[0];
  
          if (oldChange) {
            d.prev = oldChange.rev;
          } else {
            d.prev = null;
          }
  
          deltas.push(d);
        });
  
        callback(null, {
          deltas: deltas,
          conflicts: conflicts
        });
      });
      return callback.promise;
    };
  
    /**
     * Correct all change list entries.
     * @param {Function} cb
     */
  
    Change.rectifyAll = function(cb) {
      debug('rectify all');
      var Change = this;
      // this should be optimized
      this.find(function(err, changes) {
        if (err) return cb(err);
        async.each(
          changes,
          function(c, next) { c.rectify(next); },
          cb);
      });
    };
  
    /**
     * Get the checkpoint model.
     * @return {Checkpoint}
     */
  
    Change.getCheckpointModel = function() {
      var checkpointModel = this.Checkpoint;
      if (checkpointModel) return checkpointModel;
      // FIXME(bajtos) This code creates multiple different models with the same
      // model name, which is not a valid supported usage of juggler's API.
      this.Checkpoint = checkpointModel = loopback.Checkpoint.extend('checkpoint');
      assert(this.dataSource, 'Cannot getCheckpointModel(): ' + this.modelName +
        ' is not attached to a dataSource');
      checkpointModel.attachTo(this.dataSource);
      return checkpointModel;
    };
  
    Change.handleError = function(err) {
      deprecate('Change.handleError is deprecated, ' +
        'you should pass errors to your callback instead.');
  
      if (!this.settings.ignoreErrors) {
        throw err;
      }
    };
  
    Change.prototype.debug = function() {
      if (debug.enabled) {
        var args = Array.prototype.slice.call(arguments);
        args[0] = args[0] + ' %s';
        args.push(this.modelName);
        debug.apply(this, args);
        debug('\tid', this.id);
        debug('\trev', this.rev);
        debug('\tprev', this.prev);
        debug('\tcheckpoint', this.checkpoint);
        debug('\tmodelName', this.modelName);
        debug('\tmodelId', this.modelId);
        debug('\ttype', this.type());
      }
    };
  
    /**
     * Get the `Model` class for `change.modelName`.
     * @return {Model}
     */
  
    Change.prototype.getModelCtor = function() {
      return this.constructor.settings.trackModel;
    };
  
    Change.prototype.getModelId = function() {
      // TODO(ritch) get rid of the need to create an instance
      var Model = this.getModelCtor();
      var id = this.modelId;
      var m = new Model();
      m.setId(id);
      return m.getId();
    };
  
    Change.prototype.getModel = function(callback) {
      var Model = this.constructor.settings.trackModel;
      var id = this.getModelId();
      Model.findById(id, callback);
    };
  
    /**
     * When two changes conflict a conflict is created.
     *
     * **Note**: call `conflict.fetch()` to get the `target` and `source` models.
     *
     * @param {*} modelId
     * @param {PersistedModel} SourceModel
     * @param {PersistedModel} TargetModel
     * @property {ModelClass} source The source model instance
     * @property {ModelClass} target The target model instance
     * @class Change.Conflict
     */
  
    function Conflict(modelId, SourceModel, TargetModel) {
      this.SourceModel = SourceModel;
      this.TargetModel = TargetModel;
      this.SourceChange = SourceModel.getChangeModel();
      this.TargetChange = TargetModel.getChangeModel();
      this.modelId = modelId;
    }
  
    /**
     * Fetch the conflicting models.
     *
     * @callback {Function} callback
     * @param {Error} err
     * @param {PersistedModel} source
     * @param {PersistedModel} target
     */
  
    Conflict.prototype.models = function(cb) {
      var conflict = this;
      var SourceModel = this.SourceModel;
      var TargetModel = this.TargetModel;
      var source;
      var target;
  
      async.parallel([
        getSourceModel,
        getTargetModel
      ], done);
  
      function getSourceModel(cb) {
        SourceModel.findById(conflict.modelId, function(err, model) {
          if (err) return cb(err);
          source = model;
          cb();
        });
      }
  
      function getTargetModel(cb) {
        TargetModel.findById(conflict.modelId, function(err, model) {
          if (err) return cb(err);
          target = model;
          cb();
        });
      }
  
      function done(err) {
        if (err) return cb(err);
        cb(null, source, target);
      }
    };
  
    /**
     * 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) {
        var SourceModel = conflict.SourceModel;
        SourceModel.findLastChange(conflict.modelId, function(err, change) {
          if (err) return cb(err);
          sourceChange = change;
          cb();
        });
      }
  
      function getTargetChange(cb) {
        var TargetModel = conflict.TargetModel;
        TargetModel.findLastChange(conflict.modelId, 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.
     *
     * Set the source change's previous revision to the current revision of the
     * (conflicting) target change. Since the changes are no longer conflicting
     * and appear as if the source change was based on the target, they will be
     * replicated normally as part of the next replicate() call.
     *
     * This is effectively resolving the conflict using the source version.
     *
     * @callback {Function} callback
     * @param {Error} err
     */
  
    Conflict.prototype.resolve = function(cb) {
      var conflict = this;
      conflict.TargetModel.findLastChange(
        this.modelId,
        function(err, targetChange) {
          if (err) return cb(err);
          conflict.SourceModel.updateLastChange(
            conflict.modelId,
            { prev: targetChange.rev },
            cb);
        });
    };
  
    /**
     * Resolve the conflict using the instance data in the source model.
     *
     * @callback {Function} callback
     * @param {Error} err
     */
    Conflict.prototype.resolveUsingSource = function(cb) {
      this.resolve(function(err) {
        // don't forward any cb arguments from resolve()
        cb(err);
      });
    };
  
    /**
     * Resolve the conflict using the instance data in the target model.
     *
     * @callback {Function} callback
     * @param {Error} err
     */
    Conflict.prototype.resolveUsingTarget = function(cb) {
      var conflict = this;
  
      conflict.models(function(err, source, target) {
        if (err) return done(err);
        if (target === null) {
          return conflict.SourceModel.deleteById(conflict.modelId, done);
        }
        var inst = new conflict.SourceModel(
          target.toObject(),
          { persisted: true });
        inst.save(done);
      });
  
      function done(err) {
        // don't forward any cb arguments from internal calls
        cb(err);
      }
    };
  
    /**
     * Return a new Conflict instance with swapped Source and Target models.
     *
     * This is useful when resolving a conflict in one-way
     * replication, where the source data must not be changed:
     *
     * ```js
     * conflict.swapParties().resolveUsingTarget(cb);
     * ```
     *
     * @returns {Conflict} A new Conflict instance.
     */
    Conflict.prototype.swapParties = function() {
      var Ctor = this.constructor;
      return new Ctor(this.modelId, this.TargetModel, this.SourceModel);
    };
  
    /**
     * Resolve the conflict using the supplied instance data.
     *
     * @param {Object} data The set of changes to apply on the model
     * instance. Use `null` value to delete the source instance instead.
     * @callback {Function} callback
     * @param {Error} err
     */
  
    Conflict.prototype.resolveManually = function(data, cb) {
      var conflict = this;
      if (!data) {
        return conflict.SourceModel.deleteById(conflict.modelId, done);
      }
  
      conflict.models(function(err, source, target) {
        if (err) return done(err);
        var inst = source || new conflict.SourceModel(target);
        inst.setAttributes(data);
        inst.save(function(err) {
          if (err) return done(err);
          conflict.resolve(done);
        });
      });
  
      function done(err) {
        // don't forward any cb arguments from internal calls
        cb(err);
      }
    };
  
    /**
     * Determine the conflict type.
     *
     * Possible results are
     *
     *  - `Change.UPDATE`: Source and target models were updated.
     *  - `Change.DELETE`: 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);
      });
    };
  };