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

Commit f7493f6e by Mick Hansen

Merge pull request #450 from ricardograca/master

Fix issue #447
2 parents fe7703c6 3589147b
Showing with 59 additions and 22 deletions
...@@ -140,12 +140,13 @@ module.exports = (function() { ...@@ -140,12 +140,13 @@ module.exports = (function() {
} }
DAOFactory.prototype.findAll = function(options) { DAOFactory.prototype.findAll = function(options) {
var hasJoin = false; var hasJoin = false
var options = Utils._.clone(options)
if ((typeof options === 'object') && (options.hasOwnProperty('include'))) { if ((typeof options === 'object') && (options.hasOwnProperty('include'))) {
var includes = options.include var includes = options.include
hasJoin = true; hasJoin = true
options.include = {} options.include = {}
includes.forEach(function(daoName) { includes.forEach(function(daoName) {
...@@ -154,10 +155,10 @@ module.exports = (function() { ...@@ -154,10 +155,10 @@ module.exports = (function() {
if (!options.include[daoName]) { if (!options.include[daoName]) {
options.include[daoName] = this.getAssociationByAlias(daoName).target options.include[daoName] = this.getAssociationByAlias(daoName).target
} }
}.bind(this)); }.bind(this))
// whereCollection is used for non-primary key updates // whereCollection is used for non-primary key updates
this.options.whereCollection = options.where || null; this.options.whereCollection = options.where || null
} }
return this.QueryInterface.select(this, this.tableName, options, { type: 'SELECT', hasJoin: hasJoin }) return this.QueryInterface.select(this, this.tableName, options, { type: 'SELECT', hasJoin: hasJoin })
...@@ -168,14 +169,14 @@ module.exports = (function() { ...@@ -168,14 +169,14 @@ module.exports = (function() {
var optcpy = Utils._.clone(options) var optcpy = Utils._.clone(options)
optcpy.attributes = optcpy.attributes || [Utils.addTicks(this.tableName)+".*"] optcpy.attributes = optcpy.attributes || [Utils.addTicks(this.tableName)+".*"]
// whereCollection is used for non-primary key updates // whereCollection is used for non-primary key updates
this.options.whereCollection = optcpy.where || null; this.options.whereCollection = optcpy.where || null;
return this.QueryInterface.select(this, [this.tableName, joinTableName], optcpy, { type: 'SELECT' }) return this.QueryInterface.select(this, [this.tableName, joinTableName], optcpy, { type: 'SELECT' })
} }
DAOFactory.prototype.find = function(options) { DAOFactory.prototype.find = function(options) {
var hasJoin = false; var hasJoin = false
// 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) {
...@@ -184,7 +185,7 @@ module.exports = (function() { ...@@ -184,7 +185,7 @@ module.exports = (function() {
}).run() }).run()
} }
var primaryKeys = this.primaryKeys; 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') {
...@@ -200,30 +201,34 @@ module.exports = (function() { ...@@ -200,30 +201,34 @@ module.exports = (function() {
}) })
options = { where: where } options = { where: where }
} else if ((typeof options === 'string') && (parseInt(options, 10).toString() === options)) { } else if (typeof options === 'string' && parseInt(options, 10).toString() === options) {
var parsedId = parseInt(options, 10); var parsedId = parseInt(options, 10)
if (!Utils._.isFinite(parsedId)) { if (!Utils._.isFinite(parsedId)) {
throw new Error('Invalid argument to find(). Must be an id or an options object.') throw new Error('Invalid argument to find(). Must be an id or an options object.')
} }
options = { where: parsedId } options = { where: parsedId }
} else if ((typeof options === 'object') && (options.hasOwnProperty('include'))) { } else if (typeof options === 'object') {
var includes = options.include var options = Utils._.clone(options)
hasJoin = true; var includes
options.include = {} if (options.hasOwnProperty('include')) {
hasJoin = true
includes = options.include
options.include = {}
includes.forEach(function(daoName) { includes.forEach(function(daoName) {
options.include[daoName] = this.daoFactoryManager.getDAO(daoName) options.include[daoName] = this.daoFactoryManager.getDAO(daoName)
if (!options.include[daoName]) { if (!options.include[daoName]) {
options.include[daoName] = this.getAssociationByAlias(daoName).target options.include[daoName] = this.getAssociationByAlias(daoName).target
} }
}.bind(this)); }.bind(this))
}
// whereCollection is used for non-primary key updates // whereCollection is used for non-primary key updates
this.options.whereCollection = options.where || null; this.options.whereCollection = options.where || null
} }
options.limit = 1 options.limit = 1
......
...@@ -168,5 +168,37 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() { ...@@ -168,5 +168,37 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
}.bind(this)) }.bind(this))
}.bind(this)) }.bind(this))
}) })
it("can reuse query option objects", function(done) {
this.User.create({ username: 'fnord' }).success(function() {
var query = { where: { username: 'fnord' }}
this.User.findAll(query).success(function(users) {
expect(users[0].username).toEqual('fnord')
this.User.findAll(query).success(function(users) {
expect(users[0].username).toEqual('fnord')
done()
}.bind(this))
}.bind(this))
}.bind(this))
})
})
describe('find', function find() {
it("can reuse query option objects", function(done) {
this.User.create({ username: 'fnord' }).success(function() {
var query = { where: { username: 'fnord' }}
this.User.find(query).success(function(user) {
expect(user.username).toEqual('fnord')
this.User.find(query).success(function(user) {
expect(user.username).toEqual('fnord')
done()
}.bind(this))
}.bind(this))
}.bind(this))
})
}) })
}) })
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!