Skip to content

Commit

Permalink
Merge pull request #4790 from curiousdannii/aggregate-scopes
Browse files Browse the repository at this point in the history
Apply scopes to `aggregate`, fixes #4764
  • Loading branch information
janmeier committed Nov 2, 2015
2 parents c77e1eb + b2b863f commit b4ac961
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changelog.md
@@ -1,3 +1,6 @@
# Next
- [FIXED] Apply scopes to `aggregate` [#4764](https://github.com/sequelize/sequelize/issues/4764)

# 3.13.0
- [FIXED] timestamp columns are no longer undefined for associations loaded with `separate`. [#4740](https://github.com/sequelize/sequelize/issues/4740)
- [FIXED] Mark unscoped model as `.scoped`, to prevent injection of default scope on includes [#4663](https://github.com/sequelize/sequelize/issues/4663)
Expand Down
2 changes: 2 additions & 0 deletions lib/model.js
Expand Up @@ -1523,6 +1523,8 @@ Model.prototype.find = Model.prototype.findOne;
*/
Model.prototype.aggregate = function(field, aggregateFunction, options) {
options = Utils._.extend({ attributes: [] }, options || {});
conformOptions(options, this);
Model.$injectScope(this.$scope, options);

var aggregateColumn = this.sequelize.col(field);
if (options.distinct) {
Expand Down
73 changes: 73 additions & 0 deletions test/integration/model/scope/aggregate.test.js
@@ -0,0 +1,73 @@
'use strict';

/* jshint -W030 */
/* jshint -W110 */
var chai = require('chai')
, Sequelize = require('../../../../index')
, expect = chai.expect
, Support = require(__dirname + '/../../support');

describe(Support.getTestDialectTeaser('Model'), function() {
describe('scope', function () {
describe('aggregate', function () {
beforeEach(function () {
this.ScopeMe = this.sequelize.define('ScopeMe', {
username: Sequelize.STRING,
email: Sequelize.STRING,
access_level: Sequelize.INTEGER,
other_value: Sequelize.INTEGER
}, {
defaultScope: {
where: {
access_level: {
gte: 5
}
}
},
scopes: {
lowAccess: {
where: {
access_level: {
lte: 5
}
}
},
withOrder: {
order: 'username'
}
}
});

return this.sequelize.sync({force: true}).then(function() {
var records = [
{username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7},
{username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11},
{username: 'dan', email: 'dan@sequelizejs.com', access_level: 5, other_value: 10},
{username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7}
];
return this.ScopeMe.bulkCreate(records);
}.bind(this));
});

it('should apply defaultScope', function () {
return expect(this.ScopeMe.aggregate( '*', 'count' )).to.eventually.equal(2);
});

it('should be able to override default scope', function () {
return expect(this.ScopeMe.aggregate( '*', 'count', { where: { access_level: { gt: 5 }}})).to.eventually.equal(1);
});

it('should be able to unscope', function () {
return expect(this.ScopeMe.unscoped().aggregate( '*', 'count' )).to.eventually.equal(4);
});

it('should be able to apply other scopes', function () {
return expect(this.ScopeMe.scope('lowAccess').aggregate( '*', 'count' )).to.eventually.equal(3);
});

it('should be able to merge scopes with where', function () {
return expect(this.ScopeMe.scope('lowAccess').aggregate( '*', 'count', { where: { username: 'dan'}})).to.eventually.equal(1);
});
});
});
});

0 comments on commit b4ac961

Please sign in to comment.