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

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() {
HasMany.prototype.injectCreator = function(obj) {
var association = this;
obj[this.accessors.create] = function(values, fieldsOrOptions) {
obj[this.accessors.create] = function(values, options) {
var instance = this;
options = options || {};
if (Array.isArray(options)) {
options = {
fields: options
};
}
if (values === undefined) {
values = {};
......@@ -482,17 +489,19 @@ module.exports = (function() {
if (association.scope) {
Object.keys(association.scope).forEach(function (attribute) {
values[attribute] = association.scope[attribute];
if (options.fields) options.fields.push(attribute);
});
}
if (Object(association.through.model) === association.through.model) {
// Create the related model instance
return association.target.create(values, fieldsOrOptions).then(function(newAssociatedObject) {
return instance[association.accessors.add](newAssociatedObject, fieldsOrOptions).return(newAssociatedObject);
return association.target.create(values, options).then(function(newAssociatedObject) {
return instance[association.accessors.add](newAssociatedObject, _.omit(options, ['fields'])).return(newAssociatedObject);
});
} else {
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() {
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() {
......@@ -1087,28 +1112,49 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
isAdmin: Sequelize.BOOLEAN
});
User.hasMany(Group, { through: UserGroups });
Group.hasMany(User, { through: UserGroups });
return this.sequelize.sync({ force: true }).then(function () {
return Group.create({});
}).then(function (group) {
return Promise.join(
group.createUser({ id: 1 }, { isAdmin: true }),
group.createUser({ id: 2 }, { isAdmin: false }),
function () {
return UserGroups.findAll();
}
);
}).then(function (userGroups) {
userGroups.sort(function (a, b) {
return a.userId < b.userId ? - 1 : 1;
});
expect(userGroups[0].userId).to.equal(1);
expect(userGroups[0].isAdmin).to.be.ok;
expect(userGroups[1].userId).to.equal(2);
expect(userGroups[1].isAdmin).not.to.be.ok;
User.hasMany(Group, { through: UserGroups });
Group.hasMany(User, { through: UserGroups });
return this.sequelize.sync({ force: true }).then(function () {
return Group.create({});
}).then(function (group) {
return Promise.join(
group.createUser({ id: 1 }, { isAdmin: true }),
group.createUser({ id: 2 }, { isAdmin: false }),
function () {
return UserGroups.findAll();
}
);
}).then(function (userGroups) {
userGroups.sort(function (a, b) {
return a.userId < b.userId ? - 1 : 1;
});
expect(userGroups[0].userId).to.equal(1);
expect(userGroups[0].isAdmin).to.be.ok;
expect(userGroups[1].userId).to.equal(2);
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);
});
});
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!