From e253d39901510698272b74ab7950aea8a3e45fe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Wed, 23 Aug 2017 16:35:19 +0200 Subject: [PATCH] Add support for transactions in count() --- lib/sql.js | 2 +- test/connectors/test-sql-connector.js | 36 +++++++++++---------------- test/transaction.test.js | 28 +++++++++++++-------- 3 files changed, 32 insertions(+), 34 deletions(-) diff --git a/lib/sql.js b/lib/sql.js index f323be9..20790ed 100644 --- a/lib/sql.js +++ b/lib/sql.js @@ -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); diff --git a/test/connectors/test-sql-connector.js b/test/connectors/test-sql-connector.js index e88e2e3..999bb1b 100644 --- a/test/connectors/test-sql-connector.js +++ b/test/connectors/test-sql-connector.js @@ -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); } }; diff --git a/test/transaction.test.js b/test/transaction.test.js index f586c35..3e254dc 100644 --- a/test/transaction.test.js +++ b/test/transaction.test.js @@ -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(); - } }); }; }