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

Commit d3bf7240 by Jan Aagaard Meier

Copy the options object in association getters

1 parent 86ca9b12
# Next
- [BUG] Add support for `field` named the same as the attribute in `reload`, `bulkCreate` and `save` [#2348](https://github.com/sequelize/sequelize/issues/2348)
- [BUG] Copy the options object in association getters. [#2311](https://github.com/sequelize/sequelize/issues/2311)
# 2.0.0-rc2
- [FEATURE] Added to posibility of using a sequelize object as key in `sequelize.where`. Also added the option of specifying a comparator
......
......@@ -100,7 +100,7 @@ module.exports = (function() {
instancePrototype[this.accessors.get] = function(params) {
var where = {};
params = params || {};
params = association.target.__optClone(params) || {};
params.where = (params.where && [params.where]) || [];
where[association.targetIdentifier] = this.get(association.identifier);
......
......@@ -323,7 +323,7 @@ module.exports = (function() {
var association = this;
obj[this.accessors.get] = function(options, queryOptions) {
options = options || {};
options = association.target.__optClone(options) || {};
queryOptions = queryOptions || {};
var Class = Object(association.through.model) === association.through.model ? HasManyDoubleLinked : HasManySingleLinked;
return new Class(association, this).injectGetter(options, queryOptions);
......
......@@ -94,7 +94,7 @@ module.exports = (function() {
instancePrototype[this.accessors.get] = function(params) {
var where = {};
params = params || {};
params = association.target.__optClone(params) || {};
params.where = (params.where && [params.where]) || [];
where[association.identifier] = this.get(association.sourceIdentifier);
......
......@@ -2009,7 +2009,7 @@ module.exports = (function() {
})(this, includes);
};
var optClone = function(options) {
var optClone = Model.prototype.__optClone = function(options) {
return Utils._.cloneDeep(options, function(elem) {
// The InstanceFactories used for include are pass by ref, so don't clone them.
if (elem &&
......
......@@ -57,6 +57,23 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
})
})
it('does not modify the passed arguments', function () {
var User = this.sequelize.define('user', {})
, Project = this.sequelize.define('project', {});
User.belongsTo(Project);
return this.sequelize.sync({ force: true }).bind(this).then(function () {
return User.create({});
}).then(function (user) {
this.options = {};
return user.getProject(this.options);
}).then(function () {
expect(this.options).to.deep.equal({});
});
});
it('should be able to handle a where object that\'s a first class citizen.', function() {
var User = this.sequelize.define('UserXYZ', { username: Sequelize.STRING, gender: Sequelize.STRING })
, Task = this.sequelize.define('TaskXYZ', { title: Sequelize.STRING, status: Sequelize.STRING })
......
......@@ -597,6 +597,16 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
});
});
it('does not modify the passed arguments', function () {
return this.User.create({}).bind(this).then(function (user) {
this.options = {};
return user.getTasks(this.options);
}).then(function () {
expect(this.options).to.deep.equal({});
});
});
it('should treat the where object of associations as a first class citizen', function() {
var self = this;
this.Article = this.sequelize.define('Article', {
......@@ -753,6 +763,16 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
});
});
it('does not modify the passed arguments', function () {
return this.User.create({}).bind(this).then(function (user) {
this.options = {};
return user.getTasks(this.options);
}).then(function () {
expect(this.options).to.deep.equal({});
});
});
it('supports transactions', function() {
return Support.prepareTransactionTest(this.sequelize).bind({}).then(function(sequelize) {
this.sequelize = sequelize;
......
......@@ -57,6 +57,23 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
})
})
it('does not modify the passed arguments', function () {
var User = this.sequelize.define('user', {})
, Project = this.sequelize.define('project', {});
User.hasOne(Project);
return this.sequelize.sync({ force: true }).bind(this).then(function () {
return User.create({});
}).then(function (user) {
this.options = {};
return user.getProject(this.options);
}).then(function () {
expect(this.options).to.deep.equal({});
});
});
it('should be able to handle a where object that\'s a first class citizen.', function(done) {
var User = this.sequelize.define('UserXYZ', { username: Sequelize.STRING })
, Task = this.sequelize.define('TaskXYZ', { title: Sequelize.STRING, status: Sequelize.STRING })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!