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

Commit 4e941ab0 by Mick Hansen Committed by Jan Aagaard Meier

refactor find & argsArePrimaryKeys a bit

1 parent 54163dc3
Showing with 17 additions and 13 deletions
...@@ -119,21 +119,24 @@ module.exports = (function() { ...@@ -119,21 +119,24 @@ module.exports = (function() {
var start = tk.time(); var start = tk.time();
// no options defined? // no options defined?
// return an emitter which emits null // return an emitter which emits null
if([null, undefined].indexOf(options) > -1) { if([null, undefined].indexOf(options) !== -1) {
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
setTimeout(function() { emitter.emit('success', null) }, 10) setTimeout(function() { emitter.emit('success', null) }, 10)
}).run() }).run()
} }
var primaryKeys = this.primaryKeys;
// options is not a hash but an id // options is not a hash but an id
if(typeof options === 'number') { if(typeof options === 'number') {
options = { where: options } options = { where: options }
} else if (Utils.argsArePrimaryKeys(arguments, this.primaryKeys)) { } else if (!Utils._.isEmpty(primaryKeys) && Utils.argsArePrimaryKeys(arguments, primaryKeys)) {
var where = {} var where = {}
, self = this , self = this
, keys = Utils._.keys(primaryKeys)
Utils._.each(arguments, function(arg, i) { Utils._.each(arguments, function(arg, i) {
var key = Utils._.keys(self.primaryKeys)[i] var key = keys[i]
where[key] = arg where[key] = arg
}) })
...@@ -231,7 +234,7 @@ module.exports = (function() { ...@@ -231,7 +234,7 @@ module.exports = (function() {
DAOFactory.prototype.__defineGetter__('primaryKeys', function() { DAOFactory.prototype.__defineGetter__('primaryKeys', function() {
var result = {} var result = {}
Utils._.each(this.attributes, function(dataTypeString, attributeName) { Utils._.each(this.attributes, function(dataTypeString, attributeName) {
if((attributeName != 'id') && (dataTypeString.indexOf('PRIMARY KEY') > -1)) { if((attributeName != 'id') && (dataTypeString.indexOf('PRIMARY KEY') !== -1)) {
result[attributeName] = dataTypeString result[attributeName] = dataTypeString
} }
}) })
......
...@@ -66,17 +66,18 @@ var Utils = module.exports = { ...@@ -66,17 +66,18 @@ var Utils = module.exports = {
].join(" ") ].join(" ")
}, },
argsArePrimaryKeys: function(args, primaryKeys) { argsArePrimaryKeys: function(args, primaryKeys) {
return false;
var result = (args.length == Utils._.keys(primaryKeys).length) var result = (args.length == Utils._.keys(primaryKeys).length)
Utils._.each(args, function(arg) { if (result) {
if(result) { Utils._.each(args, function(arg) {
if(['number', 'string'].indexOf(typeof arg) > -1) if(result) {
result = true if(['number', 'string'].indexOf(typeof arg) !== -1)
else result = true
result = (arg instanceof Date) else
result = (arg instanceof Date)
} }
}) })
}
return result return result
}, },
combineTableNames: function(tableName1, tableName2) { combineTableNames: function(tableName1, tableName2) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!