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

Commit 8f92bf4e by Sascha Depold

increment and decrement should use an object as second parameter

1 parent bacbaa8b
Showing with 43 additions and 13 deletions
...@@ -360,35 +360,65 @@ module.exports = (function() { ...@@ -360,35 +360,65 @@ module.exports = (function() {
}).run() }).run()
} }
DAO.prototype.increment = function(fields, count) { DAO.prototype.increment = function(fields, countOrOptions) {
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : { id: this.id }, Utils.validateParameter(countOrOptions, Object, {
values = {} optional: true,
deprecated: 'number',
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 }
, values = {}
if (countOrOptions === undefined) {
countOrOptions = { by: 1, transaction: null }
} else if (typeof countOrOptions === 'number') {
countOrOptions = { by: countOrOptions, transaction: null }
}
if (count === undefined) { if (countOrOptions.by === undefined) {
count = 1; countOrOptions.by = 1
} }
if (Utils._.isString(fields)) { if (Utils._.isString(fields)) {
values[fields] = count; values[fields] = countOrOptions.by
} else if (Utils._.isArray(fields)) { } else if (Utils._.isArray(fields)) {
Utils._.each(fields, function (field) { Utils._.each(fields, function (field) {
values[field] = count values[field] = countOrOptions.by
}) })
} else { // Assume fields is key-value pairs } else { // Assume fields is key-value pairs
values = fields; values = fields
} }
return this.QueryInterface.increment(this, this.QueryInterface.QueryGenerator.addSchema(this.__factory.tableName, this.__factory.options.schema), values, identifier) return this.QueryInterface.increment(this, this.QueryInterface.QueryGenerator.addSchema(this.__factory.tableName, this.__factory.options.schema), values, identifier, countOrOptions)
} }
DAO.prototype.decrement = function (fields, count) { DAO.prototype.decrement = function (fields, countOrOptions) {
Utils.validateParameter(countOrOptions, Object, {
optional: true,
deprecated: 'number',
deprecationWarning: "Decrement expects an object as second parameter. Please pass the decrementor as option! ~> instance.decrement(" + JSON.stringify(fields) + ", { by: " + countOrOptions + " })"
})
if (countOrOptions === undefined) {
countOrOptions = { by: 1, transaction: null }
} else if (typeof countOrOptions === 'number') {
countOrOptions = { by: countOrOptions, transaction: null }
}
if (countOrOptions.by === undefined) {
countOrOptions.by = 1
}
if (!Utils._.isString(fields) && !Utils._.isArray(fields)) { // Assume fields is key-value pairs if (!Utils._.isString(fields) && !Utils._.isArray(fields)) { // Assume fields is key-value pairs
Utils._.each(fields, function (value, field) { Utils._.each(fields, function (value, field) {
fields[field] = -value; fields[field] = -value
}); })
} }
return this.increment(fields, 0 - count); countOrOptions.by = 0 - countOrOptions.by
return this.increment(fields, countOrOptions)
} }
DAO.prototype.equals = function(other) { DAO.prototype.equals = function(other) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!