Merge pull request #114 from lehni/fix/count-transaction

Support transactions in count()
This commit is contained in:
Kevin Delisle 2017-08-23 11:52:48 -04:00 committed by GitHub
commit fa6f51d198
3 changed files with 32 additions and 34 deletions

View File

@ -1518,7 +1518,7 @@ SQLConnector.prototype.count = function(model, where, options, cb) {
this.tableEscaped(model));
stmt = stmt.merge(this.buildWhere(model, where));
stmt = this.parameterize(stmt);
this.execute(stmt.sql, stmt.params,
this.execute(stmt.sql, stmt.params, options,
function(err, res) {
if (err) {
return cb(err);

View File

@ -212,28 +212,20 @@ TestConnector.prototype.rollback = function(tx, cb) {
TestConnector.prototype.executeSQL = function(sql, params, options, callback) {
var transaction = options.transaction;
var model = options.model;
if (transaction && transaction.connector === this && transaction.connection) {
if (sql.indexOf('INSERT') === 0) {
transaction.connection.data[model] =
transaction.connection.data[model] || [];
transaction.connection.data[model].push({sql: sql, params: params});
debug('INSERT', transaction.connection.data, sql,
transaction.connection.name);
callback(null, 1);
} else {
debug('SELECT', transaction.connection.data, sql,
transaction.connection.name);
callback(null, transaction.connection.data[model] || []);
}
var useTransaction = transaction && transaction.connector === this &&
transaction.connection;
var data = useTransaction ? transaction.connection.data : this.data;
var name = useTransaction ? transaction.connection.name : undefined;
if (sql.indexOf('INSERT') === 0) {
data[model] = data[model] || [];
data[model].push({sql: sql, params: params});
debug('INSERT', data, sql, name);
callback(null, 1);
} else {
if (sql.indexOf('INSERT') === 0) {
this.data[model] = this.data[model] || [];
this.data[model].push({sql: sql, params: params});
debug('INSERT', this.data, sql);
callback(null, 1);
} else {
debug('SELECT', this.data, sql);
callback(null, this.data[model] || []);
}
debug('SELECT', data, sql, name);
var instances = data[model] || [];
var result = sql.indexOf('count(*) as "cnt"') !== -1 ?
[{cnt: instances.length}] : instances;
callback(null, result);
}
};

View File

@ -92,19 +92,25 @@ describe('transactions', function() {
function(err, posts) {
if (err) return done(err);
expect(posts.length).to.be.eql(count);
if (count) {
// Find related reviews
options.model = 'Review';
// Please note the empty {} is required, otherwise, the options
// will be treated as a filter
posts[0].reviews({}, options, function(err, reviews) {
// Make sure both find() and count() behave the same way
Post.count(where, options,
function(err, result) {
if (err) return done(err);
expect(reviews.length).to.be.eql(count);
done();
expect(result).to.be.eql(count);
if (count) {
// Find related reviews
options.model = 'Review';
// Please note the empty {} is required, otherwise, the options
// will be treated as a filter
posts[0].reviews({}, options, function(err, reviews) {
if (err) return done(err);
expect(reviews.length).to.be.eql(count);
done();
});
} else {
done();
}
});
} else {
done();
}
});
};
}