Permalink
Browse files

feat: count in query

  • Loading branch information...
XadillaX committed Sep 30, 2016
1 parent 87a6882 commit 2b72ede2ba4411582c4d9a3734d7d21e06ecfe27
Showing with 20 additions and 0 deletions.
  1. +14 −0 lib/adapters/mysql.js
  2. +6 −0 lib/query.js
@@ -530,8 +530,13 @@ class MySQLAdapter extends Adapter {
}

makeSql(type, model, options) {
if(type === "count") {
options.count = true;
}

switch(type) {
case "find": return this.makeFind(model, options);
case "count": return this.makeFind(model, options);
case "update": return this.makeUpdate(model, options);
case "delete": return this.makeDelete(model, options);
default: return this.makeFind(model, options);
@@ -679,6 +684,15 @@ class MySQLAdapter extends Adapter {
return _options;
}

count(query, callback) {
const _options = this.queryToOptions(query, {});
const sql = this.makeSet("count", query.model, _options);
this.execute(sql, function(err, rows) {
if(err) return callback(err, undefined, sql);
return callback(undefined, (rows || [ { "COUNT(0)": 0 } ])[0]["COUNT(0)"], sql);
});
}

find(query, callback, options) {
const _options = this.queryToOptions(query, options);
if(!query.cache || options.noCache) {
@@ -150,6 +150,12 @@ class ToshihikoQuery {
return this;
}

count(callback) {
this.adapter.count(this, function(err, count, extra) {
return callback(err, count, extra);
});
}

find(callback, toJSON, options) {
if(typeof toJSON === "object") {
options = toJSON;

0 comments on commit 2b72ede

Please sign in to comment.