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

You need to sign in or sign up before continuing.
Commit 6343ed2f by Mick Hansen

feat(associations): add options.fields support to N:M#createAssoication (closes #2259)

1 parent c58bc413
...@@ -472,8 +472,15 @@ module.exports = (function() { ...@@ -472,8 +472,15 @@ module.exports = (function() {
HasMany.prototype.injectCreator = function(obj) { HasMany.prototype.injectCreator = function(obj) {
var association = this; var association = this;
obj[this.accessors.create] = function(values, fieldsOrOptions) { obj[this.accessors.create] = function(values, options) {
var instance = this; var instance = this;
options = options || {};
if (Array.isArray(options)) {
options = {
fields: options
};
}
if (values === undefined) { if (values === undefined) {
values = {}; values = {};
...@@ -482,17 +489,19 @@ module.exports = (function() { ...@@ -482,17 +489,19 @@ module.exports = (function() {
if (association.scope) { if (association.scope) {
Object.keys(association.scope).forEach(function (attribute) { Object.keys(association.scope).forEach(function (attribute) {
values[attribute] = association.scope[attribute]; values[attribute] = association.scope[attribute];
if (options.fields) options.fields.push(attribute);
}); });
} }
if (Object(association.through.model) === association.through.model) { if (Object(association.through.model) === association.through.model) {
// Create the related model instance // Create the related model instance
return association.target.create(values, fieldsOrOptions).then(function(newAssociatedObject) { return association.target.create(values, options).then(function(newAssociatedObject) {
return instance[association.accessors.add](newAssociatedObject, fieldsOrOptions).return(newAssociatedObject); return instance[association.accessors.add](newAssociatedObject, _.omit(options, ['fields'])).return(newAssociatedObject);
}); });
} else { } else {
values[association.identifier] = instance.get(association.source.primaryKeyAttribute); values[association.identifier] = instance.get(association.source.primaryKeyAttribute);
return association.target.create(values, fieldsOrOptions); if (options.fields) options.fields.push(association.identifier);
return association.target.create(values, options);
} }
}; };
......
...@@ -550,6 +550,31 @@ describe(Support.getTestDialectTeaser("HasMany"), function() { ...@@ -550,6 +550,31 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
return this.t.rollback(); return this.t.rollback();
}); });
}); });
it('supports passing the field option', function () {
var Article = this.sequelize.define('Article', {
'title': DataTypes.STRING
});
var Label = this.sequelize.define('Label', {
'text': DataTypes.STRING
});
Article.hasMany(Label);
return this.sequelize.sync({force: true}).then(function () {
return Article.create();
}).then(function (article) {
return article.createLabel({
text: 'yolo'
}, {
fields: ['text']
}).return(article);
}).then(function (article) {
return article.getLabels();
}).then(function (labels) {
expect(labels.length).to.be.ok;
});
});
}); });
describe("getting assocations with options", function() { describe("getting assocations with options", function() {
...@@ -1110,6 +1135,27 @@ describe(Support.getTestDialectTeaser("HasMany"), function() { ...@@ -1110,6 +1135,27 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
expect(userGroups[1].isAdmin).not.to.be.ok; expect(userGroups[1].isAdmin).not.to.be.ok;
}); });
}); });
it('supports using the field parameter', function () {
var User = this.sequelize.define('User', { username: DataTypes.STRING })
, Task = this.sequelize.define('Task', { title: DataTypes.STRING });
User.hasMany(Task);
Task.hasMany(User);
return this.sequelize.sync({ force: true }).then(function() {
return Task.create({ title: 'task' });
}).bind({}).then(function(task) {
this.task = task;
return task.createUser({ username: 'foo' }, {fields: ['username']});
}).then(function(createdUser) {
expect(createdUser.Model).to.equal(User);
expect(createdUser.username).to.equal('foo');
return this.task.getUsers();
}).then(function(_users) {
expect(_users).to.have.length(1);
});
});
}); });
describe('addAssociations', function() { describe('addAssociations', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!