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

Commit 69e437e5 by Verdier

Expand and validate include in model.aggregate (fix #5121)

1 parent b0aa1063
...@@ -236,21 +236,23 @@ function conformOptions(options, self) { ...@@ -236,21 +236,23 @@ function conformOptions(options, self) {
self.$expandAttributes(options); self.$expandAttributes(options);
} }
if (options.include) { if (!options.include) {
return;
}
// if include is not an array, wrap in an array // if include is not an array, wrap in an array
if (!Array.isArray(options.include)) { if (!Array.isArray(options.include)) {
options.include = [options.include]; options.include = [options.include];
} else if (!options.include.length) {
delete options.include;
return;
} }
// convert all included elements to { model: Model } form // convert all included elements to { model: Model } form
options.include = options.include.map(function(include) { options.include = options.include.map(function(include) {
return conformInclude(include, self); include = conformInclude(include, self);
});
if (!options.include.length) { return include;
delete options.include; });
}
}
} }
Model.$conformOptions = conformOptions; Model.$conformOptions = conformOptions;
...@@ -1511,6 +1513,11 @@ Model.prototype.aggregate = function(attribute, aggregateFunction, options) { ...@@ -1511,6 +1513,11 @@ Model.prototype.aggregate = function(attribute, aggregateFunction, options) {
conformOptions(options, this); conformOptions(options, this);
this.$injectScope(options); this.$injectScope(options);
if (options.include) {
expandIncludeAll.call(this, options);
validateIncludedElements.call(this, options);
}
var attrOptions = this.rawAttributes[attribute] var attrOptions = this.rawAttributes[attribute]
, field = attrOptions && attrOptions.field || attribute , field = attrOptions && attrOptions.field || attribute
, aggregateColumn = this.sequelize.col(field); , aggregateColumn = this.sequelize.col(field);
......
...@@ -37,11 +37,11 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -37,11 +37,11 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
}); });
it('should counts rows', function () { it('should count rows', function () {
return expect(this.User.count()).to.eventually.equal(2); return expect(this.User.count()).to.eventually.equal(2);
}); });
it('should supports include', function () { it('should support include', function () {
return expect(this.User.count({ return expect(this.User.count({
include: [{ include: [{
model: this.Project, model: this.Project,
......
...@@ -5,12 +5,16 @@ ...@@ -5,12 +5,16 @@
var chai = require('chai') var chai = require('chai')
, Sequelize = require('../../../../index') , Sequelize = require('../../../../index')
, expect = chai.expect , expect = chai.expect
, Support = require(__dirname + '/../../support'); , Support = require(__dirname + '/../../support')
, Promise = require(__dirname + '/../../../../lib/promise');
describe(Support.getTestDialectTeaser('Model'), function() { describe(Support.getTestDialectTeaser('Model'), function() {
describe('scope', function () { describe('scope', function () {
describe('aggregate', function () { describe('aggregate', function () {
beforeEach(function () { beforeEach(function () {
this.Child = this.sequelize.define('Child', {
priority: Sequelize.INTEGER
});
this.ScopeMe = this.sequelize.define('ScopeMe', { this.ScopeMe = this.sequelize.define('ScopeMe', {
username: Sequelize.STRING, username: Sequelize.STRING,
email: Sequelize.STRING, email: Sequelize.STRING,
...@@ -34,9 +38,19 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -34,9 +38,19 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}, },
withOrder: { withOrder: {
order: 'username' order: 'username'
},
withInclude: {
include: [{
model: this.Child,
where: {
priority: 1
}
}]
} }
} }
}); });
this.Child.belongsTo(this.ScopeMe);
this.ScopeMe.hasMany(this.Child);
return this.sequelize.sync({force: true}).then(function() { return this.sequelize.sync({force: true}).then(function() {
var records = [ var records = [
...@@ -46,7 +60,18 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -46,7 +60,18 @@ describe(Support.getTestDialectTeaser('Model'), function() {
{username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7} {username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7}
]; ];
return this.ScopeMe.bulkCreate(records); return this.ScopeMe.bulkCreate(records);
}.bind(this)); }.bind(this)).then(function () {
return this.ScopeMe.findAll();
}.bind(this)).then(function (records) {
return Promise.all([
records[0].createChild({
priority: 1
}),
records[1].createChild({
priority: 2
})
]);
});
}); });
it('should apply defaultScope', function () { it('should apply defaultScope', function () {
...@@ -68,6 +93,18 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -68,6 +93,18 @@ describe(Support.getTestDialectTeaser('Model'), function() {
it('should be able to merge scopes with where', function () { 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); return expect(this.ScopeMe.scope('lowAccess').aggregate( '*', 'count', { where: { username: 'dan'}})).to.eventually.equal(1);
}); });
it('should be able to use where on include', function () {
return expect(this.ScopeMe.scope('withInclude').aggregate( 'ScopeMe.id', 'count', {
plain: true,
dataType: new Sequelize.INTEGER(),
includeIgnoreAttributes: false,
limit: null,
offset: null,
order: null,
attributes: []
})).to.eventually.equal(1);
});
}); });
}); });
}); });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!