Comparing version 0.1.2 to 0.1.3
@@ -13,5 +13,5 @@ /** | ||
/** | ||
* | ||
* @param host | ||
* @returns {module.exports} | ||
* CONSTRUCTOR | ||
* Used to initialise mysql-json | ||
* @param {Object} host | ||
*/ | ||
@@ -18,0 +18,0 @@ module.exports = function(host) { |
@@ -11,5 +11,4 @@ /** | ||
module.exports = function (host) { | ||
module.exports = function(host) { | ||
var _self = this; | ||
@@ -20,5 +19,5 @@ | ||
* Used to open connection to host | ||
* @param callback | ||
* @param {Function} callback | ||
*/ | ||
_self.connect = function(callback) { | ||
_self.connect = function (callback) { | ||
var connection = Mysql.createConnection(host); | ||
@@ -30,3 +29,2 @@ connection.connect(function (err) { | ||
/** | ||
@@ -36,6 +34,6 @@ * QUERY | ||
* @param mysqlQuery | ||
* @param callback | ||
* @param {Function} callback | ||
*/ | ||
_self.query = function(mysqlQuery, callback) { | ||
_self.connect(function(err, connection) { | ||
_self.query = function (mysqlQuery, callback) { | ||
_self.connect(function (err, connection) { | ||
if (err) { | ||
@@ -45,3 +43,3 @@ callback(err, null); | ||
else { | ||
connection.query(mysqlQuery, function(err, response) { | ||
connection.query(mysqlQuery, function (err, response) { | ||
connection.end(); | ||
@@ -61,12 +59,46 @@ if (err) { | ||
/** | ||
* FIND BY PRIMARY KEY | ||
* Used to query for a single record by primary key | ||
* @param {String} tableName | ||
* @param {String|Number} id | ||
* @param {Function} callback | ||
*/ | ||
_self.findByPrimaryKey = function (tableName, id, callback) { | ||
if (tableName && id) { | ||
var primaryKeyQuery = 'SHOW KEYS FROM `' + tableName + '` WHERE Key_name = \'PRIMARY\''; | ||
_self.query(primaryKeyQuery, function (err, res) { | ||
if (err) { | ||
callback(err, null); | ||
} | ||
else { | ||
var primaryKeyId = Object.get(res, '0.Column_name') || 'id'; | ||
var mysqlQuery = 'SELECT * FROM `' + tableName + '` WHERE ' + primaryKeyId + '=' + JSON.stringify(id); | ||
_self.query(mysqlQuery, function(err, rows) { | ||
if (err) { | ||
callback(err, null); | ||
} | ||
else { | ||
callback(null, rows && rows.length ? rows[0] : []); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
else { | ||
callback(new Error('mysql-json [findByPrimaryKey]: Require at least a tableName and a valid id'), null); | ||
} | ||
}; | ||
/** | ||
* INSERT | ||
* Used to build and launch a Mysql insert query | ||
* @param tableName | ||
* @param dataToInsert | ||
* @param callback | ||
* @param {String} tableName | ||
* @param {Object} dataToInsert | ||
* @param {Function} callback | ||
*/ | ||
_self.insert = function(tableName, dataToInsert, callback) { | ||
_self.insert = function (tableName, dataToInsert, callback) { | ||
if (tableName && dataToInsert) { | ||
if (Object.size(dataToInsert) > 0) { | ||
var mysqlQuery = 'INSERT INTO ' + tableName + ' ('; | ||
var mysqlQuery = 'INSERT INTO `' + tableName + '` ('; | ||
var i = 0; | ||
@@ -100,11 +132,11 @@ for (var index in dataToInsert) { | ||
* Used to build and launch a Mysql update query | ||
* @param tableName | ||
* @param data | ||
* @param conditions | ||
* @param callback | ||
* @param {String} tableName | ||
* @param {Object} data | ||
* @param {Object} conditions | ||
* @param {Function} callback | ||
*/ | ||
_self.update = function(tableName, data, conditions, callback) { | ||
_self.update = function (tableName, data, conditions, callback) { | ||
if (tableName && data) { | ||
if (Object.size(data) > 0) { | ||
var mysqlQuery = 'UPDATE ' + tableName + ' SET '; | ||
var mysqlQuery = 'UPDATE `' + tableName + '` SET '; | ||
var i = 0; | ||
@@ -146,9 +178,9 @@ for (var index in data) { | ||
* Used to build and launch a delete Mysql query | ||
* @param tableName | ||
* @param conditions | ||
* @param callback | ||
* @param {String} tableName | ||
* @param {Object} conditions | ||
* @param {Function} callback | ||
*/ | ||
_self.delete = function(tableName, conditions, callback) { | ||
_self.delete = function (tableName, conditions, callback) { | ||
if (tableName) { | ||
var mysqlQuery = "DELETE FROM " + tableName + " "; | ||
var mysqlQuery = "DELETE FROM `" + tableName + "` "; | ||
if (conditions) { | ||
@@ -175,2 +207,2 @@ mysqlQuery += "WHERE "; | ||
}; | ||
}; |
{ | ||
"name": "mysql-json", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Simple Node.js module which allows insert, update and delete mysql queries (Using package mysql)", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -19,3 +19,3 @@ # mysql-json | ||
All methods takes a callback which is called with 2 parameters (err, response) | ||
All methods take a callback which is called with 2 parameters (err, response) | ||
@@ -33,2 +33,7 @@ ```javascript | ||
```javascript | ||
// Used to find a row by primary key | ||
mysqlJson.findByPrimaryKey(tableName, id, callback); | ||
``` | ||
```javascript | ||
// Used to insert a new row with JSON data | ||
@@ -79,2 +84,10 @@ mysqlJson.insert(tableName, dataToInsert, callback); | ||
```javascript | ||
// Find a document where Primary Key = 1 | ||
mysqlJson.findByPrimaryKey('myTable', 1, function(err, response) { | ||
if (err) throw err; | ||
console.log(response); | ||
}); | ||
``` | ||
```javascript | ||
// Insert new document with login=root, firstname=John, lastName=Doe, Age=45 | ||
@@ -81,0 +94,0 @@ mysqlJson.insert('myTable', { |
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
244
132
11637
7