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

Commit c3fe9efb by Ricardo Graça

Enable reusing query option objects in find

1 parent bae5c971
Showing with 40 additions and 19 deletions
...@@ -176,7 +176,7 @@ module.exports = (function() { ...@@ -176,7 +176,7 @@ module.exports = (function() {
} }
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) {
...@@ -185,7 +185,7 @@ module.exports = (function() { ...@@ -185,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') {
...@@ -201,30 +201,34 @@ module.exports = (function() { ...@@ -201,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.forEach(function(daoName) { includes = options.include
options.include[daoName] = this.daoFactoryManager.getDAO(daoName) options.include = {}
if (!options.include[daoName]) { includes.forEach(function(daoName) {
options.include[daoName] = this.getAssociationByAlias(daoName).target options.include[daoName] = this.daoFactoryManager.getDAO(daoName)
}
}.bind(this)); if (!options.include[daoName]) {
options.include[daoName] = this.getAssociationByAlias(daoName).target
}
}.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
......
...@@ -184,4 +184,21 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() { ...@@ -184,4 +184,21 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
}.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!