-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
model.save only updates changed data
added deep-diff dependency
- Loading branch information
1 parent
c036a68
commit b796f5b
Showing
5 changed files
with
104 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
var MongoDiff = function(lhs, rhs, prefilter) { | ||
this._lhs = lhs; | ||
this._rhs = rhs; | ||
this._deepDiffs = DeepDiff.diff(lhs, rhs, prefilter); | ||
this.operators = {}; | ||
this.warnings = []; | ||
for (var i in this._deepDiffs) { | ||
this.addDeepDiff(this._deepDiffs[i]); | ||
} | ||
}; | ||
|
||
MongoDiff.prototype.addOperator = function(operator, path, val) { | ||
var obj = this.operators[operator] = this.operators[operator] || {}; | ||
obj[path] = val; | ||
}; | ||
|
||
MongoDiff.prototype.addDeepDiff = function(diff) { | ||
var path = diff.path.join('.'); | ||
switch (diff.kind) { | ||
case 'A': | ||
var set = this.operators.$set; | ||
if (!(set && set[path])) { | ||
this.addOperator("$set", path, Graviton.getProperty(this._rhs, path)); | ||
this.warnings.push("Will replace entire array at: '"+path+"'. Individual updates should probably be done manually instead."); | ||
} | ||
break; | ||
case 'E': | ||
case 'N': | ||
this.addOperator("$set", path, diff.rhs); | ||
break; | ||
case 'D': | ||
this.addOperator("$unset", path, ""); | ||
break; | ||
} | ||
}; | ||
|
||
Graviton.mongoDiff = function(lhs, rhs, prefilter) { | ||
return new MongoDiff(lhs, rhs, prefilter); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
var lhs = { | ||
name: 'my object', | ||
description: 'it\'s an object!', | ||
details: { | ||
it: 'has', | ||
an: 'array', | ||
with: ['a', 'few', 'elements'], | ||
array: ['inside', {another: ['array']}] | ||
} | ||
}; | ||
|
||
var rhs = { | ||
name: 'updated object', | ||
description: 'it\'s an object!', | ||
details: { | ||
it: 'has', | ||
an: 'array', | ||
with: ['a', 'few', 'more', 'elements', { than: 'before' }], | ||
array: ['inside', {another: ['array', 'should not be tested']}] | ||
} | ||
}; | ||
|
||
|
||
var expected = { | ||
$set: { | ||
name: 'updated object', | ||
'details.with': ['a', 'few', 'more', 'elements', { than: 'before' }], | ||
'details.array': ['inside', {another: ['array', 'should not be tested']}] | ||
} | ||
}; | ||
|
||
var mongoDiff = Graviton.mongoDiff(lhs, rhs); | ||
|
||
|
||
Tinytest.add('mongo diff', function(test) { | ||
test.isTrue(_.isEqual(mongoDiff.operators, expected)); | ||
test.equal(mongoDiff.warnings.length, 2); | ||
}); |