mysql-activerecord
Advanced tools
Comparing version 0.7.3 to 0.7.4
21
index.js
@@ -117,3 +117,3 @@ /** | ||
} | ||
else if (typeof dataSet[key] === 'object' && dataSet[key] instanceof Array && dataSet[key].length > 0) { | ||
else if (typeof dataSet[key] === 'object' && Object.prototype.toString.call(dataSet[key]) === '[object Array]' && dataSet[key].length > 0) { | ||
queryString += escapeFieldName(key) + ' in ("' + dataSet[key].join('", "') + '")'; | ||
@@ -192,3 +192,3 @@ } | ||
} | ||
else if ((typeof whereSet === 'string' || typeof whereSet === 'number') && typeof whereValue === 'object' && whereValue instanceof Array && whereValue.length > 0) { | ||
else if ((typeof whereSet === 'string' || typeof whereSet === 'number') && typeof whereValue === 'object' && Object.prototype.toString.call(whereValue) === '[object Array]' && whereValue.length > 0) { | ||
whereClause[whereSet] = whereValue; | ||
@@ -203,2 +203,15 @@ } | ||
this.count = function(tableName, responseCallback) { | ||
if (typeof tableName === 'string') { | ||
var combinedQueryString = 'SELECT COUNT(*) as count FROM ' + escapeFieldName(tableName) | ||
+ buildJoinString() | ||
+ buildDataString(whereClause, ' AND ', 'WHERE'); | ||
connection.query(combinedQueryString, function(err, res) { responseCallback(null, res[0]['count'])}); | ||
resetQuery(combinedQueryString); | ||
} | ||
return that; | ||
} | ||
this.join = function(tableName, relation, direction) { | ||
@@ -214,3 +227,3 @@ joinClause.push({ | ||
this.select = function(selectSet) { | ||
if (selectSet instanceof Array) { | ||
if (Object.prototype.toString.call(selectSet) === '[object Array]') { | ||
for (var i = 0; i < selectSet.length; i++) { | ||
@@ -233,3 +246,3 @@ selectClause.push(selectSet[i]); | ||
var clause = ''; | ||
if (Object.prototype.toString.call(set) !== '[object Array]') { | ||
if (Object.prototype.toString.call(set) === '[object Array]') { | ||
clause = set.join(', '); | ||
@@ -236,0 +249,0 @@ } |
{ | ||
"name" : "mysql-activerecord", | ||
"version": "0.7.3", | ||
"version": "0.7.4", | ||
"author": "Martin Tajur <martin@tajur.ee>", | ||
@@ -5,0 +5,0 @@ "description": "MySQL ActiveRecord pattern implementation on top of the mysql module.", |
@@ -51,2 +51,3 @@ MySQL ActiveRecord Adapter for Node.js | ||
* GROUP BY | ||
* COUNT | ||
@@ -208,2 +209,8 @@ Methods | ||
## .count(tableName, responseCallback) | ||
Produces and executes a SELECT query with count. | ||
db.get('people', function(err, rows, fields) { ... }); | ||
// This would produce: SELECT count(*) FROM people … | ||
## .query(sqlQueryString, responseCallback) | ||
@@ -294,2 +301,14 @@ Produces and executes a raw query. Note that while no set query conditions will be used in this query, they will all be reset nevertheless with the execution. | ||
Basic counting | ||
------------------------------------------------------ | ||
db | ||
.where({ | ||
'people.name': 'Martin', | ||
'songs.title': 'Yesterday' | ||
}) | ||
.count('people', function(err, results, fields) { | ||
console.log(results); | ||
}); | ||
SELECT query with custom fields and GROUP BY | ||
@@ -342,3 +361,3 @@ -------------------------------------------- | ||
.delete('records', function(err) { | ||
if (err) { | ||
if (!err) { | ||
console.log('Deleted!') | ||
@@ -345,0 +364,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23467
4
358
367