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

Commit a335f846 by Daniel Durante

You can now use .find() on any single integer primary key when throwing just a n…

…umber as an argument.
1 parent 9a41b4ae
Showing with 49 additions and 8 deletions
...@@ -250,14 +250,21 @@ module.exports = (function() { ...@@ -250,14 +250,21 @@ module.exports = (function() {
} }
var primaryKeys = this.primaryKeys var primaryKeys = this.primaryKeys
, keys = Object.keys(primaryKeys)
, keysLength = keys.length
// 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 } var oldOption = options
options = { where: {} }
if (keysLength === 1) {
options.where[keys[0]] = oldOption
} else {
options.where.id = oldOption
}
} else if (Utils._.size(primaryKeys) && Utils.argsArePrimaryKeys(arguments, primaryKeys)) { } else if (Utils._.size(primaryKeys) && Utils.argsArePrimaryKeys(arguments, primaryKeys)) {
var where = {} var where = {}
, self = this , self = this
, keys = Object.keys(primaryKeys)
Utils._.each(arguments, function(arg, i) { Utils._.each(arguments, function(arg, i) {
var key = keys[i] var key = keys[i]
......
...@@ -1330,17 +1330,34 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -1330,17 +1330,34 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
{where: {id: 0}}, {where: {id: 0}},
{where: {id: '0'}} {where: {id: '0'}}
] ]
, done = _.after(2 * permutations.length, _done); , done = _.after(2 * permutations.length, _done)
this.User.bulkCreate([{username: 'jack'}, {username: 'jack'}]).success(function() { this.User.bulkCreate([{username: 'jack'}, {username: 'jack'}]).success(function() {
permutations.forEach(function(perm) { permutations.forEach(function(perm) {
self.User.find(perm).done(function(err, user) { self.User.find(perm).done(function(err, user) {
expect(err).to.be.null; expect(err).to.be.null
expect(user).to.be.null; expect(user).to.be.null
done(); done()
}).on('sql', function(s) { }).on('sql', function(s) {
expect(s.indexOf(0)).not.to.equal(-1); expect(s.indexOf(0)).not.to.equal(-1)
done(); done()
})
})
})
})
it('should allow us to find IDs using capital letters', function(done) {
var User = this.sequelize.define('User' + config.rand(), {
ID: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
Login: { type: Sequelize.STRING }
})
User.sync({ force: true }).success(function() {
User.create({Login: 'foo'}).success(function() {
User.find(1).success(function(user) {
expect(user).to.exist
expect(user.ID).to.equal(1)
done()
}) })
}) })
}) })
...@@ -2053,6 +2070,23 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -2053,6 +2070,23 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
}) })
it('should allow us to find IDs using capital letters', function(done) {
var User = this.sequelize.define('User' + config.rand(), {
ID: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
Login: { type: Sequelize.STRING }
})
User.sync({ force: true }).success(function() {
User.create({Login: 'foo'}).success(function() {
User.findAll({ID: 1}).success(function(user) {
expect(user).to.be.instanceof(Array)
expect(user).to.have.length(1)
done()
})
})
})
})
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!