不要怂,就是干,撸起袖子干!

Commit 4585ba41 by Emil Janitzek

feat(model.set): Fixed model set for JSON with dot.separated key

1 parent a2cb0c13
# Next
- [FIXED] Calling set with dot.separated key on a JSON/JSONB attribute will not flag the entire object as changed [#4379](https://github.com/sequelize/sequelize/pull/4379)
# 3.9.0
- [ADDED] beforeRestore/afterRestore hooks [#4371](https://github.com/sequelize/sequelize/issues/4371)
- [ADDED] Map raw fields back to attributes names when using `mapToModel` or `returning` [#3995](https://github.com/sequelize/sequelize/pull/3995)
......
......@@ -322,8 +322,11 @@ Instance.prototype.set = function(key, value, options) { // testhint options:non
// If attribute is not in model definition, return
if (!this._isAttribute(key)) {
if (key.indexOf('.') > -1 && this.Model._isJsonAttribute(key.split('.')[0])) {
var previousDottieValue = Dottie.get(this.dataValues, key);
if (!_.isEqual(previousDottieValue, value)) {
Dottie.set(this.dataValues, key, value);
this.changed(key, true);
this.changed(key.split('.')[0], true);
}
}
return;
}
......
......@@ -94,5 +94,61 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
user.set('meta', meta);
expect(user.changed('meta')).to.equal(true);
});
it('should return true for JSON dot.separated key with changed values', function() {
var user = this.User.build({
meta: {
city: 'Stockholm'
}
}, {
isNewRecord: false,
raw: true
});
user.set('meta.city', 'Gothenburg');
expect(user.changed('meta')).to.equal(true);
});
it('should return false for JSON dot.separated key with same value', function() {
var user = this.User.build({
meta: {
city: 'Gothenburg'
}
}, {
isNewRecord: false,
raw: true
});
user.set('meta.city', 'Gothenburg');
expect(user.changed('meta')).to.equal(false);
});
it('should return true for JSON dot.separated key with object', function() {
var user = this.User.build({
meta: {
address: { street: 'Main street', number: '40' }
}
}, {
isNewRecord: false,
raw: true
});
user.set('meta.address', { street: 'Second street', number: '1' } );
expect(user.changed('meta')).to.equal(true);
});
it('should return false for JSON dot.separated key with same object', function() {
var user = this.User.build({
meta: {
address: { street: 'Main street', number: '40' }
}
}, {
isNewRecord: false,
raw: true
});
user.set('meta.address', { street: 'Main street', number: '40' } );
expect(user.changed('meta')).to.equal(false);
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!