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

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() {
}
DAOFactory.prototype.findAll = function(options) {
var hasJoin = false;
var hasJoin = false
var options = Utils._.clone(options)
if ((typeof options === 'object') && (options.hasOwnProperty('include'))) {
var includes = options.include
hasJoin = true;
hasJoin = true
options.include = {}
includes.forEach(function(daoName) {
......@@ -154,10 +155,10 @@ module.exports = (function() {
if (!options.include[daoName]) {
options.include[daoName] = this.getAssociationByAlias(daoName).target
}
}.bind(this));
}.bind(this))
// whereCollection is used for non-primary key updates
this.options.whereCollection = options.where || null;
// whereCollection is used for non-primary key updates
this.options.whereCollection = options.where || null
}
return this.QueryInterface.select(this, this.tableName, options, { type: 'SELECT', hasJoin: hasJoin })
......@@ -168,14 +169,14 @@ module.exports = (function() {
var optcpy = Utils._.clone(options)
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;
return this.QueryInterface.select(this, [this.tableName, joinTableName], optcpy, { type: 'SELECT' })
}
DAOFactory.prototype.find = function(options) {
var hasJoin = false;
var hasJoin = false
// no options defined?
// return an emitter which emits null
if ([null, undefined].indexOf(options) !== -1) {
......@@ -184,7 +185,7 @@ module.exports = (function() {
}).run()
}
var primaryKeys = this.primaryKeys;
var primaryKeys = this.primaryKeys
// options is not a hash but an id
if (typeof options === 'number') {
......@@ -200,30 +201,34 @@ module.exports = (function() {
})
options = { where: where }
} else if ((typeof options === 'string') && (parseInt(options, 10).toString() === options)) {
var parsedId = parseInt(options, 10);
} 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 === 'object') && (options.hasOwnProperty('include'))) {
var includes = options.include
hasJoin = true;
} else if (typeof options === 'object') {
var options = Utils._.clone(options)
var includes
options.include = {}
if (options.hasOwnProperty('include')) {
hasJoin = true
includes = options.include
options.include = {}
includes.forEach(function(daoName) {
options.include[daoName] = this.daoFactoryManager.getDAO(daoName)
includes.forEach(function(daoName) {
options.include[daoName] = this.daoFactoryManager.getDAO(daoName)
if (!options.include[daoName]) {
options.include[daoName] = this.getAssociationByAlias(daoName).target
}
}.bind(this));
if (!options.include[daoName]) {
options.include[daoName] = this.getAssociationByAlias(daoName).target
}
}.bind(this))
}
// whereCollection is used for non-primary key updates
this.options.whereCollection = options.where || null;
// whereCollection is used for non-primary key updates
this.options.whereCollection = options.where || null
}
options.limit = 1
......
......@@ -168,5 +168,37 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
}.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!