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

Commit 82403cfc by Mick Hansen

feat(model): improve findAndCountAll by having the count part only use the absol…

…utely required includes
1 parent ce6b7878
...@@ -12,6 +12,7 @@ var Utils = require('./utils') ...@@ -12,6 +12,7 @@ var Utils = require('./utils')
, Promise = require('./promise') , Promise = require('./promise')
, QueryTypes = require('./query-types') , QueryTypes = require('./query-types')
, Hooks = require('./hooks') , Hooks = require('./hooks')
, _ = require('lodash')
, associationsMixin = require('./associations/mixin'); , associationsMixin = require('./associations/mixin');
module.exports = (function() { module.exports = (function() {
...@@ -897,11 +898,34 @@ module.exports = (function() { ...@@ -897,11 +898,34 @@ module.exports = (function() {
* *
* @see {Model#findAll} for a specification of find and query options * @see {Model#findAll} for a specification of find and query options
* @return {Promise<Object>} * @return {Promise<Object>}
* @alias findAndCountAll
*/ */
Model.prototype.findAndCountAll = function(findOptions, queryOptions) { Model.prototype.findAndCount = function(findOptions, queryOptions) {
findOptions = findOptions || {};
var self = this var self = this
// no limit, offset, order, attributes for the options given to count() // no limit, offset, order, attributes for the options given to count()
, countOptions = Utils._.omit(findOptions ? Utils._.merge({}, findOptions) : {}, ['offset', 'limit', 'order', 'attributes']); , countOptions = _.omit(_.clone(findOptions), ['offset', 'limit', 'order', 'attributes']);
if (countOptions.include) {
conformOptions(countOptions);
countOptions.include = _.cloneDeep(countOptions.include, function (element) {
if (element instanceof Model) return element;
if (element instanceof Association) return element;
return undefined;
});
validateIncludedElements.call(this, countOptions);
var keepNeeded = function(includes) {
return includes.filter(function (include) {
if (include.include) include.include = keepNeeded(include.include);
return include.required || include.hasIncludeRequired;
});
};
countOptions.include = keepNeeded(countOptions.include);
}
return self.count(countOptions).then(function(count) { return self.count(countOptions).then(function(count) {
if (count === 0) { if (count === 0) {
...@@ -918,6 +942,8 @@ module.exports = (function() { ...@@ -918,6 +942,8 @@ module.exports = (function() {
}); });
}); });
}; };
Model.prototype.findAndCountAll = Model.prototype.findAndCount;
/** /**
* Find the maximum value of field * Find the maximum value of field
......
...@@ -1459,25 +1459,27 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -1459,25 +1459,27 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
Citizen.create({ name: 'Bob' }).done(function (err, bob) { Citizen.create({ name: 'Bob' }).done(function (err, bob) {
expect(err).not.be.ok expect(err).not.be.ok
Election.create({ name: 'Some election' }).done(function (err, election) { Election.create({ name: 'Some election' }).done(function (err, election) {
expect(err).not.be.ok Election.create({ name: 'Some other election' }).done(function (err, election) {
election.setCitizen(alice).done(function (err) {
expect(err).not.be.ok expect(err).not.be.ok
election.setVoters([alice, bob]).done(function (err) { election.setCitizen(alice).done(function (err) {
expect(err).not.be.ok expect(err).not.be.ok
election.setVoters([alice, bob]).done(function (err) {
var criteria = {
offset: 5,
limit: 1,
include: [
Citizen, // Election creator
{ model: Citizen, as: 'Voters' } // Election voters
]
}
Election.findAndCountAll(criteria).done(function (err, elections) {
expect(err).not.be.ok expect(err).not.be.ok
expect(elections.count).to.equal(2)
expect(elections.rows.length).to.equal(0) var criteria = {
done() offset: 5,
limit: 1,
include: [
Citizen, // Election creator
{ model: Citizen, as: 'Voters' } // Election voters
]
}
Election.findAndCountAll(criteria).done(function (err, elections) {
expect(err).not.be.ok
expect(elections.count).to.equal(2)
expect(elections.rows.length).to.equal(0)
done()
})
}) })
}) })
}) })
......
...@@ -5,6 +5,7 @@ var chai = require('chai') ...@@ -5,6 +5,7 @@ var chai = require('chai')
, Support = require(__dirname + '/../support') , Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + "/../../lib/data-types") , DataTypes = require(__dirname + "/../../lib/data-types")
, datetime = require('chai-datetime') , datetime = require('chai-datetime')
, Promise = require('bluebird');
chai.use(datetime) chai.use(datetime)
chai.config.includeStack = true chai.config.includeStack = true
...@@ -140,6 +141,40 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -140,6 +141,40 @@ describe(Support.getTestDialectTeaser("Include"), function () {
}) })
}) })
it('should count on a where and not use an uneeded include', function () {
var Project = this.sequelize.define('Project', {
id: { type: DataTypes.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true },
project_name: { type: DataTypes.STRING}
})
var User = this.sequelize.define('User', {
id: { type: DataTypes.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true },
user_name: { type: DataTypes.STRING }
})
User.hasMany(Project);
var userId = null
return User.sync({force: true}).then(function () {
return Project.sync({force: true});
}).then(function() {
return Promise.all([User.create(), Project.create(), Project.create(), Project.create()]);
}).then(function(results) {
var user = results[0];
userId = user.id;
return user.setProjects([results[1], results[2], results[3]]);
}).then(function() {
return User.findAndCountAll({
where: {id: userId},
include: [Project]
});
}).then(function(result) {
expect(result.rows.length).to.equal(1);
expect(result.rows[0].Projects.length).to.equal(3);
expect(result.count).to.equal(1);
});
});
}) })
}) })
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!