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

Commit f9c142b6 by Thaddeus Quintin

Added intelligence to model.count function to automatically set 'plain' to false…

… when specifying 'group'. Added documentation and integration test.
1 parent af489905
Showing with 21 additions and 2 deletions
...@@ -803,8 +803,9 @@ module.exports = (function() { ...@@ -803,8 +803,9 @@ module.exports = (function() {
* @param {DataType|String} [options.dataType] The type of the result. If `field` is a field in this Model, the default will be the type of that field, otherwise defaults to float. * @param {DataType|String} [options.dataType] The type of the result. If `field` is a field in this Model, the default will be the type of that field, otherwise defaults to float.
* @param {boolean} [options.distinct] Applies DISTINCT to the field being aggregated over * @param {boolean} [options.distinct] Applies DISTINCT to the field being aggregated over
* @param {Transaction} [options.transaction] * @param {Transaction} [options.transaction]
* @param {boolean} [options.plain] When `true`, the first returned value of `aggregateFunction` is cast to `dataType` and returned. If additional attributes are specified, along with `group` clauses, set `plain` to `false` to return all values of all returned rows. Defaults to `true`
* *
* @return {Promise<options.dataType>} * @return {Promise<options.dataType|object>} Returns the aggregate result cast to `options.dataType`, unless `options.plain` is false, in which case the complete data result is returned.
*/ */
Model.prototype.aggregate = function(field, aggregateFunction, options) { Model.prototype.aggregate = function(field, aggregateFunction, options) {
options = Utils._.extend({ attributes: [] }, options || {}); options = Utils._.extend({ attributes: [] }, options || {});
...@@ -839,7 +840,9 @@ module.exports = (function() { ...@@ -839,7 +840,9 @@ module.exports = (function() {
* @param {Object} [options] * @param {Object} [options]
* @param {Object} [options.where] A hash of search attributes. * @param {Object} [options.where] A hash of search attributes.
* @param {Object} [options.include] Include options. See `find` for details * @param {Object} [options.include] Include options. See `find` for details
* @param {boolean} [options.distinct] Appliy COUNT(DISTINCT(col)) * @param {boolean} [options.distinct] Apply COUNT(DISTINCT(col))
* @param {Object} [options.attributes] Used in conjustion with `group`
* @param {Object} [options.group] For creating complex counts. Will return multiple rows as needed.
* *
* @return {Promise<Integer>} * @return {Promise<Integer>}
*/ */
...@@ -856,6 +859,7 @@ module.exports = (function() { ...@@ -856,6 +859,7 @@ module.exports = (function() {
Utils.mapOptionFieldNames(options, this); Utils.mapOptionFieldNames(options, this);
options.plain = options.group ? false : true;
options.dataType = new DataTypes.INTEGER(); options.dataType = new DataTypes.INTEGER();
options.includeIgnoreAttributes = false; options.includeIgnoreAttributes = false;
options.limit = null; options.limit = null;
......
...@@ -1583,6 +1583,21 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -1583,6 +1583,21 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
}); });
}); });
it('returns multiple rows when using group', function() {
var self = this;
return this.User.bulkCreate([
{username: 'user1', data: 'A'},
{username: 'user2', data: 'A'},
{username: 'user3', data: 'B'}
]).then(function() {
return self.User.count({
attributes: ['data'],
group: ['data']
}).then(function(count) {
expect(count.length).to.equal(2);
});
});
});
it('does not modify the passed arguments', function(done) { it('does not modify the passed arguments', function(done) {
var options = { where: ['username = ?', 'user1']}; var options = { where: ['username = ?', 'user1']};
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!