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

Commit 27d79f35 by Daniel Durante

Added private paranoidClause for DRY.

1 parent 802d4c77
Showing with 26 additions and 28 deletions
...@@ -339,15 +339,7 @@ module.exports = (function() { ...@@ -339,15 +339,7 @@ module.exports = (function() {
this.options.whereCollection = options.where || null this.options.whereCollection = options.where || null
} }
if (this.options.paranoid === true) { options = paranoidClause.call(this, options)
options = options || {}
options.where = options.where || {}
if (typeof options.where === "string") {
options.where += ' AND ' + this.QueryInterface.quoteIdentifier(this.options.deletedAt) + ' IS NULL '
} else {
options.where[this.options.deletedAt] = null
}
}
return this.QueryInterface.select(this, this.tableName, options, Utils._.defaults({ return this.QueryInterface.select(this, this.tableName, options, Utils._.defaults({
type: 'SELECT', type: 'SELECT',
...@@ -440,16 +432,7 @@ module.exports = (function() { ...@@ -440,16 +432,7 @@ module.exports = (function() {
} }
} }
if (this.options.paranoid === true) { options = paranoidClause.call(this, options)
options = options || {}
options.where = options.where || {}
if (typeof options.where === "string") {
options.where += ' AND ' + this.QueryInterface.quoteIdentifier(this.options.deletedAt) + ' IS NULL '
} else {
options.where[this.options.deletedAt] = null
}
}
options.limit = 1 options.limit = 1
return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({ return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({
...@@ -464,15 +447,7 @@ module.exports = (function() { ...@@ -464,15 +447,7 @@ module.exports = (function() {
options.attributes.push(['count(*)', 'count']) options.attributes.push(['count(*)', 'count'])
options.parseInt = true options.parseInt = true
if (this.options.paranoid === true) { options = paranoidClause.call(this, options)
options = options || {}
options.where = options.where || {}
if (typeof options.where === "string") {
options.where += ' AND ' + this.QueryInterface.quoteIdentifier(this.options.deletedAt) + ' IS NULL '
} else {
options.where[this.options.deletedAt] = null
}
}
return this.QueryInterface.rawSelect(this.getTableName(), options, 'count') return this.QueryInterface.rawSelect(this.getTableName(), options, 'count')
} }
...@@ -727,6 +702,29 @@ module.exports = (function() { ...@@ -727,6 +702,29 @@ module.exports = (function() {
// private // private
var paranoidClause = function(options) {
if (this.options.paranoid === true) {
options = options || {}
options.where = options.where || {}
// Don't overwrite our explicit deletedAt search value if we provide one
if (!!options.where[this.options.deletedAt]) {
return options
}
if (typeof options.where === "string") {
options.where += ' AND ' + this.QueryInterface.quoteIdentifier(this.options.deletedAt) + ' IS NULL '
}
else if (Array.isArray(options.where)) {
options.where[0] += ' AND ' + this.QueryInterface.quoteIdentifier(this.options.deletedAt) + ' IS NULL '
} else {
options.where[this.options.deletedAt] = null
}
}
return options
}
var addOptionalClassMethods = function() { var addOptionalClassMethods = function() {
var self = this var self = this
Utils._.each(this.options.classMethods || {}, function(fct, name) { self[name] = fct }) Utils._.each(this.options.classMethods || {}, function(fct, name) { self[name] = fct })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!