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

Commit 2e4b9b26 by Nuno Sousa Committed by Jan Aagaard Meier

Improve field mapping in `destroy()` and `increment()`

1 parent da57c5b5
Showing with 3 additions and 19 deletions
......@@ -821,18 +821,13 @@ Instance.prototype.destroy = function(options) {
return this.Model.runHooks('beforeDestroy', this, options);
}
}).then(function() {
var where;
var where = Utils.mapOptionFieldNames({ where: this.where() }, this.Model).where;
if (this.Model._timestampAttributes.deletedAt && options.force === false) {
this.setDataValue(this.Model._timestampAttributes.deletedAt, new Date());
return this.save(_.extend({}, options, {hooks : false}));
} else {
where = {};
var primaryKeys = this.Model.primaryKeyAttributes;
for(var i = 0; i < primaryKeys.length; i++) {
where[this.Model.rawAttributes[primaryKeys[i]].field] = this.get(primaryKeys[i], { raw: true });
}
return this.sequelize.getQueryInterface().delete(this, this.Model.getTableName(options), where, _.defaults(options, { type: QueryTypes.DELETE,limit: null}));
return this.sequelize.getQueryInterface().delete(this, this.Model.getTableName(options), where, _.assign({ type: QueryTypes.DELETE, limit: null }, options));
}
}).tap(function() {
// Run after hook
......@@ -906,22 +901,11 @@ Instance.prototype.restore = function(options) {
* @return {Promise<this>}
*/
Instance.prototype.increment = function(fields, options) {
var identifier = this.where()
var identifier = Utils.mapOptionFieldNames({ where: this.where() }, this.Model).where
, 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];
}
}
}
options = _.defaults({}, options, {
by: 1,
attributes: {},
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!