Skip to content

Commit

Permalink
changed order of options check + check if the content of options can …
Browse files Browse the repository at this point in the history
…be an id
  • Loading branch information
sdepold committed Nov 6, 2012
1 parent 2fec91a commit 8aa472c
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions lib/dao-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,27 +127,28 @@ module.exports = (function() {
var primaryKeys = this.primaryKeys;

// options is not a hash but an id
if(typeof options === 'string') {
if(typeof options === 'number') {
options = { where: options }
} else if (Utils._.size(primaryKeys) && Utils.argsArePrimaryKeys(arguments, primaryKeys)) {
var where = {}
, self = this
, keys = Utils._.keys(primaryKeys)

Utils._.each(arguments, function(arg, i) {
var key = keys[i]
where[key] = arg
})

options = { where: where }
} else if ((typeof options === 'string') && (parseInt(options, 10).toString() === options)) {
var parsedId = parseInt(options, 10);

if(!Utils._.isFinite(parsedId)) {
throw new Error('Invalid argument to find(). Must be an id or an options object.')
}
options = { where: parsedId }
}
else if(typeof options === 'number') {
options = { where: options }
} else if (Utils._.size(primaryKeys) && Utils.argsArePrimaryKeys(arguments, primaryKeys)) {
var where = {}
, self = this
, keys = Utils._.keys(primaryKeys)

Utils._.each(arguments, function(arg, i) {
var key = keys[i]
where[key] = arg
})

options = { where: where }
} else if((typeof options === 'object') && (options.hasOwnProperty('include'))) {
options = { where: parsedId }
} else if ((typeof options === 'object') && (options.hasOwnProperty('include'))) {
var includes = options.include
hasJoin = true;

Expand Down Expand Up @@ -185,7 +186,7 @@ module.exports = (function() {

return this.QueryInterface.rawSelect(this.tableName, options, 'min')
}

DAOFactory.prototype.build = function(values, options) {
var instance = new DAO(values, Utils._.extend(this.options, { hasPrimaryKeys: this.hasPrimaryKeys, factory: this }))
, self = this
Expand Down

0 comments on commit 8aa472c

Please sign in to comment.