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

Commit 6876dfcd by Mick Hansen

feat(has-many): countAssociations

1 parent ccff0d8f
......@@ -156,7 +156,17 @@ var HasMany = function(source, target, options) {
* @return {Promise}
* @method hasAssociations
*/
hasAll: 'has' + plural
hasAll: 'has' + plural,
/**
* Count everything currently associated with this, using an optional where clause
*
* @param {Object} [options]
* @param {Object} [options.where] An optional where clause to limit the associated models
* @param {String|Boolean} [options.scope] Apply a scope on the related model, or remove its default scope by passing false
* @return {Promise<Int>}
* @method countAssociations
*/
count: 'count' + plural
};
if (this.options.counterCache) {
......@@ -233,6 +243,22 @@ HasMany.prototype.injectGetter = function(obj) {
return Model.all(options);
};
obj[this.accessors.count] = function(options) {
var model = association.target
, sequelize = model.sequelize;
options = association.target.__optClone(options) || {};
options.attributes = [
[sequelize.fn('COUNT', sequelize.col(model.primaryKeyAttribute)), 'count']
];
options.raw = true;
options.plain = true;
return obj[association.accessors.get].call(this, options).then(function (result) {
return parseInt(result.count, 10);
});
};
obj[this.accessors.hasSingle] = obj[this.accessors.hasAll] = function(instances, options) {
var where = {};
......
......@@ -415,11 +415,22 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
beforeEach(function() {
var self = this;
this.User = this.sequelize.define('User', { username: DataTypes.STRING });
this.Task = this.sequelize.define('Task', { title: DataTypes.STRING, active: DataTypes.BOOLEAN });
this.User = this.sequelize.define('User', {
username: DataTypes.STRING
});
this.Task = this.sequelize.define('Task', {
title: DataTypes.STRING,
active: DataTypes.BOOLEAN
});
this.UserTask = this.sequelize.define('UserTask', {
started: {
type: DataTypes.BOOLEAN,
defaultValue: false
}
});
this.User.belongsToMany(this.Task, { through: 'UserTasks' });
this.Task.belongsToMany(this.User, { through: 'UserTasks' });
this.User.belongsToMany(this.Task, { through: this.UserTask });
this.Task.belongsToMany(this.User, { through: this.UserTask });
return this.sequelize.sync({ force: true }).then(function() {
return Promise.all([
......@@ -449,7 +460,7 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
it('should count scoped associations', function () {
this.User.belongsToMany(this.Task, {
as: 'activeTasks',
through: 'UserTasks',
through: this.UserTask,
scope: {
active: true
}
......@@ -457,6 +468,35 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
return expect(this.user.countActiveTasks({})).to.eventually.equal(1);
});
it('should count scoped through associations', function () {
var user = this.user;
this.User.belongsToMany(this.Task, {
as: 'startedTasks',
through: {
model: this.UserTask,
scope: {
started: true
}
}
});
return Promise.join(
this.Task.create().then(function (task) {
return user.addTask(task, {
started: true
})
}),
this.Task.create().then(function (task) {
return user.addTask(task, {
started: true
})
})
).then(function () {
return expect(user.countStartedTasks({})).to.eventually.equal(2);
});
});
});
describe('setAssociations', function() {
......
......@@ -634,6 +634,54 @@ describe(Support.getTestDialectTeaser('HasMany'), function() {
});
});
describe('countAssociations', function () {
beforeEach(function() {
var self = this;
this.User = this.sequelize.define('User', { username: DataTypes.STRING });
this.Task = this.sequelize.define('Task', { title: DataTypes.STRING, active: DataTypes.BOOLEAN });
this.User.hasMany(self.Task, {
foreignKey: 'userId'
});
return this.sequelize.sync({ force: true }).then(function() {
return Promise.all([
self.User.create({ username: 'John'}),
self.Task.create({ title: 'Get rich', active: true}),
self.Task.create({ title: 'Die trying', active: false})
]);
}).spread(function(john, task1, task2) {
self.user = john;
return john.setTasks([task1, task2]);
});
});
it('should count all associations', function () {
return expect(this.user.countTasks({})).to.eventually.equal(2);
});
it('should count filtered associations', function () {
return expect(this.user.countTasks({
where: {
active: true
}
})).to.eventually.equal(1);
});
it('should count scoped associations', function () {
this.User.hasMany(this.Task, {
foreignKey: 'userId',
as: 'activeTasks',
scope: {
active: true
}
});
return expect(this.user.countActiveTasks({})).to.eventually.equal(1);
});
});
describe('selfAssociations', function() {
it('should work with alias', function() {
var Person = this.sequelize.define('Group', {});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!