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

Commit e2777690 by Jan Aagaard Meier

Properly copy the options objects passed to find and findall

1 parent 4dbb571f
Showing with 29 additions and 1 deletions
......@@ -324,7 +324,8 @@ module.exports = (function() {
DAOFactory.prototype.findAll = function(options, queryOptions) {
var hasJoin = false
var options = Utils._.clone(options)
var options = optClone(options)
if (typeof options === 'object') {
if (options.hasOwnProperty('include')) {
......@@ -367,6 +368,8 @@ module.exports = (function() {
DAOFactory.prototype.find = function(options, queryOptions) {
var hasJoin = false
options = optClone(options)
// no options defined?
// return an emitter which emits null
if ([null, undefined].indexOf(options) !== -1) {
......@@ -804,6 +807,13 @@ module.exports = (function() {
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"))
return DAOFactory
......
......@@ -1441,6 +1441,15 @@ 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) {
this.UserPrimary.find('a string').success(function(user) {
expect(user.specialKey).to.equal('a string')
......@@ -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) {
this.User.findAll({where: "id != " + this.users[1].id}).success(function(users) {
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!