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

Commit 6d37b085 by Sushant Committed by GitHub

restore setDataValue, fixed set issue with customGetter/Setter (#7328)

1 parent 8fd3fb65
...@@ -2993,12 +2993,11 @@ class Model { ...@@ -2993,12 +2993,11 @@ class Model {
* @param {any} value * @param {any} value
*/ */
setDataValue(key, value) { setDataValue(key, value) {
const originalValue = this.dataValues[key]; const originalValue = this._previousDataValues[key];
if (!Utils.isPrimitive(value) || value !== originalValue) { if (!Utils.isPrimitive(value) || value !== originalValue) {
this.changed(key, true); this.changed(key, true);
} }
this._previousDataValues[key] = originalValue;
this.dataValues[key] = value; this.dataValues[key] = value;
} }
...@@ -3142,6 +3141,10 @@ class Model { ...@@ -3142,6 +3141,10 @@ class Model {
// If not raw, and there's a custom setter // If not raw, and there's a custom setter
if (!options.raw && this._customSetters[key]) { if (!options.raw && this._customSetters[key]) {
this._customSetters[key].call(this, value, key); this._customSetters[key].call(this, value, key);
if (!Utils.isPrimitive(value) && value !== null || value !== originalValue) {
this._previousDataValues[key] = originalValue;
this.changed(key, true);
}
} else { } else {
// Check if we have included models, and if this key matches the include model names/aliases // Check if we have included models, and if this key matches the include model names/aliases
......
'use strict'; 'use strict';
/* jshint -W030 */ /* jshint -W030 */
var Support = require(__dirname + '/../support') const Support = require(__dirname + '/../support');
, DataTypes = require(__dirname + '/../../../lib/data-types') const DataTypes = require(__dirname + '/../../../lib/data-types');
, chai = require('chai') const chai = require('chai');
, expect = chai.expect const expect = chai.expect;
, Support = require(__dirname + '/../support') const current = Support.sequelize;
, current = Support.sequelize const _ = require('lodash');
, _ = require('lodash');
describe(Support.getTestDialectTeaser('Model'), function() { describe(Support.getTestDialectTeaser('Model'), function () {
describe('update', function () { describe('update', function () {
var Account;
beforeEach(function() { beforeEach(function() {
Account = this.sequelize.define('Account', { this.Account = this.sequelize.define('Account', {
ownerId: { ownerId: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
allowNull: false, allowNull: false,
...@@ -25,59 +21,53 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -25,59 +21,53 @@ describe(Support.getTestDialectTeaser('Model'), function() {
type: DataTypes.STRING type: DataTypes.STRING
} }
}); });
return Account.sync({force: true}); return this.Account.sync({force: true});
}); });
it('should only update the passed fields', function () { it('should only update the passed fields', function () {
return Account.create({ return this.Account
ownerId: 2 .create({ ownerId: 2 })
}).then(function (account) { .then(account => this.Account.update({
return Account.update({
name: Math.random().toString() name: Math.random().toString()
}, { }, {
where: { where: {
id: account.get('id') id: account.get('id')
} }
}); }));
});
}); });
if (_.get(current.dialect.supports, 'returnValues.returning')) { if (_.get(current.dialect.supports, 'returnValues.returning')) {
it('should return the updated record', function () { it('should return the updated record', function () {
return Account.create({ return this.Account.create({ ownerId: 2 }).then(account => {
ownerId: 2 return this.Account.update({ name: 'FooBar' }, {
}).then(function (account) {
return Account.update({
name: 'FooBar'
}, {
where: { where: {
id: account.get('id') id: account.get('id')
}, },
returning: true returning: true
}).spread(function(count, accounts) { }).spread((count, accounts) => {
var account = accounts[0]; const firstAcc = accounts[0];
expect(account.ownerId).to.be.equal(2); expect(firstAcc.ownerId).to.be.equal(2);
expect(account.name).to.be.equal('FooBar'); expect(firstAcc.name).to.be.equal('FooBar');
}); });
}); });
}); });
} }
if (current.dialect.supports['LIMIT ON UPDATE']) { if (current.dialect.supports['LIMIT ON UPDATE']) {
it('Should only update one row', function () { it('should only update one row', function () {
return Account.create({ return this.Account.create({
ownerId: 2, ownerId: 2,
name: 'Account Name 1' name: 'Account Name 1'
}) })
.then(() => { .then(() => {
return Account.create({ return this.Account.create({
ownerId: 2, ownerId: 2,
name: 'Account Name 2' name: 'Account Name 2'
}); });
}) })
.then(() => { .then(() => {
return Account.create({ return this.Account.create({
ownerId: 2, ownerId: 2,
name: 'Account Name 3' name: 'Account Name 3'
}); });
...@@ -89,13 +79,12 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -89,13 +79,12 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}, },
limit: 1 limit: 1
}; };
return Account.update({ name: 'New Name' }, options); return this.Account.update({ name: 'New Name' }, options);
}) })
.then(account => { .then(account => {
expect(account[0]).to.equal(1); expect(account[0]).to.equal(1);
}); });
}); });
} }
}); });
}); });
'use strict'; 'use strict';
/* jshint -W030 */ /* jshint -W030 */
var chai = require('chai') const chai = require('chai');
, expect = chai.expect const expect = chai.expect;
, Support = require(__dirname + '/../support') const Support = require(__dirname + '/../support');
, DataTypes = require(__dirname + '/../../../lib/data-types') const DataTypes = require(__dirname + '/../../../lib/data-types');
, current = Support.sequelize; const current = Support.sequelize;
describe(Support.getTestDialectTeaser('Instance'), function () { describe(Support.getTestDialectTeaser('Instance'), function () {
describe('previous', function () { describe('previous', function () {
it('should return correct previous value', function () { it('should return correct previous value', function () {
var Model = current.define('Model', { const Model = current.define('Model', {
text: { text: DataTypes.STRING,
textCustom: {
type: DataTypes.STRING, type: DataTypes.STRING,
get: function (name) { set(val) {
return this.getDataValue(name); this.setDataValue('textCustom', val);
}, },
set: function (value, name) { get() {
this.setDataValue(name, value); this.getDataValue('textCustom');
} }
} }
})
, instance
, shouldBeEmpty
, shouldBeA;
instance = Model.build({ text: 'a' }, {
isNewRecord: false
}); });
shouldBeEmpty = instance.previous('text'); const instance = Model.build({ text: 'a', textCustom: 'abc' });
expect(instance.previous('text')).to.be.not.ok;
expect(instance.previous('textCustom')).to.be.not.ok;
instance.set('text', 'b'); instance.set('text', 'b');
instance.set('textCustom', 'def');
shouldBeA = instance.previous('text'); expect(instance.previous('text')).to.be.equal('a');
expect(instance.previous('textCustom')).to.be.equal('abc');
expect(shouldBeEmpty).to.be.not.ok;
expect(shouldBeA).to.be.equal('a');
}); });
}); });
}); });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!