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

Commit 9f32c4b8 by Mick Hansen

Merge branch 'quiddle-master'

2 parents 75304090 e3a3e563
......@@ -549,7 +549,7 @@ module.exports = (function() {
if (hookChanged) {
if (options.validate) {
// Validate again
options.skip = _.difference(Object.keys(this.Model.rawAttributes), hookChanged);
return Promise.bind(this).then(function () {
// hookValidate rejects with errors, validate returns with errors
......@@ -840,11 +840,25 @@ module.exports = (function() {
deprecationWarning: 'Increment expects an object as second parameter. Please pass the incrementor as option! ~> instance.increment(' + JSON.stringify(fields) + ', { by: ' + countOrOptions + ' })'
});
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : { id: this.id }
var identifier = this.primaryKeyValues
, updatedAtAttr = this.Model._timestampAttributes.updatedAt
, values = {}
, where;
if (identifier) {
for (var attrName in identifier) {
// Field name mapping
var rawAttribute = this.Model.rawAttributes[attrName];
if (rawAttribute.field && rawAttribute.field !== rawAttribute.fieldName) {
identifier[this.Model.rawAttributes[attrName].field] = identifier[attrName];
delete identifier[attrName];
}
}
}
if (identifier === null && this.__options.whereCollection !== null) {
identifier = this.__options.whereCollection;
}
if (countOrOptions === undefined) {
countOrOptions = { by: 1 };
} else if (typeof countOrOptions === 'number') {
......@@ -873,6 +887,14 @@ module.exports = (function() {
countOrOptions.attributes[updatedAtAttr] = this.Model.__getTimestamp(updatedAtAttr);
}
Object.keys(values).forEach(function(attr) {
// Field name mapping
if (this.Model.rawAttributes[attr] && this.Model.rawAttributes[attr].field && this.Model.rawAttributes[attr].field !== attr) {
values[this.Model.rawAttributes[attr].field] = values[attr];
delete values[attr];
}
}, this);
return this.QueryInterface.increment(this, this.Model.getTableName(countOrOptions), values, where, countOrOptions);
};
......
......@@ -29,6 +29,12 @@ describe(Support.getTestDialectTeaser('Model'), function() {
name: {
type: DataTypes.STRING,
field: 'full_name'
},
taskCount: {
type: DataTypes.INTEGER,
field: 'task_count',
defaultValue: 0,
allowNull: false
}
}, {
tableName: 'users',
......@@ -96,6 +102,11 @@ describe(Support.getTestDialectTeaser('Model'), function() {
},
full_name: {
type: DataTypes.STRING
},
task_count: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0
}
}),
queryInterface.createTable('tasks', {
......@@ -357,6 +368,12 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
});
it('should work with increment', function () {
return this.User.create().then(function (user) {
return user.increment('taskCount');
});
});
it('should work with a simple where', function() {
var self = this;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!