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

Commit 85838c2e by Daniel Durante

Merge pull request #884 from janmeier/master

Properly copy the options objects passed to find and findall
2 parents 6fda1564 44261965
Showing with 31 additions and 4 deletions
...@@ -324,7 +324,8 @@ module.exports = (function() { ...@@ -324,7 +324,8 @@ module.exports = (function() {
DAOFactory.prototype.findAll = function(options, queryOptions) { DAOFactory.prototype.findAll = function(options, queryOptions) {
var hasJoin = false var hasJoin = false
var options = Utils._.clone(options)
options = optClone(options)
if (typeof options === 'object') { if (typeof options === 'object') {
if (options.hasOwnProperty('include')) { if (options.hasOwnProperty('include')) {
...@@ -378,6 +379,7 @@ module.exports = (function() { ...@@ -378,6 +379,7 @@ module.exports = (function() {
var primaryKeys = this.primaryKeys var primaryKeys = this.primaryKeys
, keys = Object.keys(primaryKeys) , keys = Object.keys(primaryKeys)
, keysLength = keys.length , keysLength = keys.length
options = optClone(options)
// options is not a hash but an id // options is not a hash but an id
if (typeof options === 'number') { if (typeof options === 'number') {
...@@ -804,6 +806,13 @@ module.exports = (function() { ...@@ -804,6 +806,13 @@ module.exports = (function() {
return attributes return attributes
} }
var optClone = function (options) {
return Utils._.cloneDeep(options, function (elem) {
// The DAOFactories used for include are pass by ref, so don't clone them. Otherwise return undefined, meaning, 'handle this lodash'
return elem instanceof DAOFactory ? elem : undefined
})
}
Utils._.extend(DAOFactory.prototype, require("./associations/mixin")) Utils._.extend(DAOFactory.prototype, require("./associations/mixin"))
return DAOFactory return DAOFactory
......
...@@ -1426,14 +1426,14 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -1426,14 +1426,14 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
var self = this var self = this
this.User.create({username: 'barfooz'}).success(function(user) { this.User.create({username: 'barfooz'}).success(function(user) {
self.UserPrimary = self.sequelize.define('UserPrimary', { self.UserPrimary = self.sequelize.define('UserPrimary', {
specialKey: { specialkey: {
type: DataTypes.STRING, type: DataTypes.STRING,
primaryKey: true primaryKey: true
} }
}) })
self.UserPrimary.sync({force: true}).success(function() { self.UserPrimary.sync({force: true}).success(function() {
self.UserPrimary.create({specialKey: 'a string'}).success(function() { self.UserPrimary.create({specialkey: 'a string'}).success(function() {
self.user = user self.user = user
done() done()
}) })
...@@ -1441,9 +1441,18 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -1441,9 +1441,18 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
it('does not modify the passed arguments', function (done) {
var options = { where: ['specialkey = ?', 'awesome']}
this.UserPrimary.find(options).success(function(user) {
expect(options).to.deep.equal({ where: ['specialkey = ?', 'awesome']})
done()
})
})
it('doesn\'t throw an error when entering in a non integer value for a specified primary field', function(done) { it('doesn\'t throw an error when entering in a non integer value for a specified primary field', function(done) {
this.UserPrimary.find('a string').success(function(user) { this.UserPrimary.find('a string').success(function(user) {
expect(user.specialKey).to.equal('a string') expect(user.specialkey).to.equal('a string')
done() done()
}) })
}) })
...@@ -2458,6 +2467,15 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -2458,6 +2467,15 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
it('does not modify the passed arguments', function (done) {
var options = { where: ['username = ?', 'awesome']}
this.User.findAll(options).success(function(user) {
expect(options).to.deep.equal({ where: ['username = ?', 'awesome']})
done()
})
})
it("finds all users matching the passed conditions", function(done) { it("finds all users matching the passed conditions", function(done) {
this.User.findAll({where: "id != " + this.users[1].id}).success(function(users) { this.User.findAll({where: "id != " + this.users[1].id}).success(function(users) {
expect(users.length).to.equal(1) expect(users.length).to.equal(1)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!