Comparing version 0.1.0 to 0.1.1
@@ -32,6 +32,6 @@ /** | ||
* Used to launch a Mysql query | ||
* @param request | ||
* @param mysqlQuery | ||
* @param callback | ||
*/ | ||
_self.query = function(request, callback) { | ||
_self.query = function(mysqlQuery, callback) { | ||
_self.connect(function(err, connection) { | ||
@@ -42,3 +42,3 @@ if (err) { | ||
else { | ||
connection.query(request, function(err, response) { | ||
connection.query(mysqlQuery, function(err, response) { | ||
connection.end(); | ||
@@ -61,23 +61,23 @@ if (err) { | ||
* @param tableName | ||
* @param data | ||
* @param dataToInsert | ||
* @param callback | ||
*/ | ||
_self.insert = function(tableName, data, callback) { | ||
if (tableName && data) { | ||
if (Object.size(data) > 0) { | ||
var request = 'INSERT INTO ' + tableName + ' ('; | ||
_self.insert = function(tableName, dataToInsert, callback) { | ||
if (tableName && dataToInsert) { | ||
if (Object.size(dataToInsert) > 0) { | ||
var mysqlQuery = 'INSERT INTO ' + tableName + ' ('; | ||
var i = 0; | ||
for (var index in data) { | ||
request += index; | ||
request += (i + 1 == Object.size(data) ? ') ' : ', '); | ||
for (var index in dataToInsert) { | ||
mysqlQuery += index; | ||
mysqlQuery += (i + 1 == Object.size(dataToInsert) ? ') ' : ', '); | ||
i++; | ||
} | ||
i = 0; | ||
request += 'VALUES ('; | ||
for (index in data) { | ||
request += typeof(data[index]) == 'string' ? '"' + data[index].replace(/"/g, '\\"') + '"' : data[index]; | ||
request += (i + 1 == Object.size(data) ? ') ' : ', '); | ||
mysqlQuery += 'VALUES ('; | ||
for (index in dataToInsert) { | ||
mysqlQuery += typeof(dataToInsert[index]) == 'string' ? '"' + dataToInsert[index].replace(/"/g, '\\"') + '"' : dataToInsert[index]; | ||
mysqlQuery += (i + 1 == Object.size(dataToInsert) ? ') ' : ', '); | ||
i++; | ||
} | ||
_self.query(request, callback); | ||
_self.query(mysqlQuery, callback); | ||
} | ||
@@ -105,14 +105,9 @@ else { | ||
if (Object.size(data) > 0) { | ||
var request = 'UPDATE ' + tableName + ' SET '; | ||
var mysqlQuery = 'UPDATE ' + tableName + ' SET '; | ||
var i = 0; | ||
for (var index in data) { | ||
request += index + ''; | ||
if (data[index].operator == '+=') { | ||
request += '=' + index + '+'; | ||
} | ||
else { | ||
request += (data[index].operator || '='); | ||
} | ||
request += data[index].value; | ||
request += (i + 1 == Object.size(data) ? ' ' : ', '); | ||
mysqlQuery += index + ''; | ||
mysqlQuery += '='; | ||
mysqlQuery += typeof(data[index]) == 'string' ? '"' + data[index].replace(/"/g, '\\"') + '"' : data[index]; | ||
mysqlQuery += (i + 1 == Object.size(data) ? ' ' : ', '); | ||
i++; | ||
@@ -122,14 +117,14 @@ } | ||
if (conditions) { | ||
request += 'WHERE '; | ||
mysqlQuery += 'WHERE '; | ||
for (index in conditions) { | ||
request += index; | ||
request += " "; | ||
request += (conditions[index].operator || '='); | ||
request += " "; | ||
request += conditions[index].value; | ||
request += (i + 1 == Object.size(conditions) ? ' ' : ' AND '); | ||
mysqlQuery += index; | ||
mysqlQuery += " "; | ||
mysqlQuery += (conditions[index].operator || '='); | ||
mysqlQuery += " "; | ||
mysqlQuery += typeof(conditions[index].value) == 'string' ? '"' + conditions[index].value.replace(/"/g, '\\"') + '"' : conditions[index].value; | ||
mysqlQuery += (i + 1 == Object.size(conditions) ? ' ' : ' AND '); | ||
i++; | ||
} | ||
} | ||
_self.query(request, callback); | ||
_self.query(mysqlQuery, callback); | ||
} | ||
@@ -155,17 +150,17 @@ else { | ||
if (tableName) { | ||
var request = "DELETE FROM " + tableName + " "; | ||
var mysqlQuery = "DELETE FROM " + tableName + " "; | ||
if (conditions) { | ||
request += "WHERE "; | ||
mysqlQuery += "WHERE "; | ||
var i = 0; | ||
for (var index in conditions) { | ||
request += index; | ||
request += " "; | ||
request += (conditions[index].operator || "="); | ||
request += " "; | ||
request += conditions[index].value; | ||
request += (i + 1 == Object.size(conditions) ? " " : " AND "); | ||
mysqlQuery += index; | ||
mysqlQuery += " "; | ||
mysqlQuery += (conditions[index].operator || "="); | ||
mysqlQuery += " "; | ||
mysqlQuery += typeof(conditions[index].value) == 'string' ? '"' + conditions[index].value.replace(/"/g, '\\"') + '"' : conditions[index].value; | ||
mysqlQuery += (i + 1 == Object.size(conditions) ? " " : " AND "); | ||
i++; | ||
} | ||
} | ||
_self.query(request, callback); | ||
_self.query(mysqlQuery, callback); | ||
} | ||
@@ -172,0 +167,0 @@ else { |
{ | ||
"name": "mysql-json", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Simple Node.js module which allows insert, update and delete mysql queries (Using package mysql)", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# mysql-json | ||
========= | ||
Simple Node.js mysql module | ||
Simple Node.js mysql module using [mysql] (https://github.com/mysqljs/mysql) | ||
## Installation | ||
npm install mysql-json | ||
npm install mysql-json | ||
## Declaration | ||
<pre> | ||
<code> | ||
var MysqlJson = require('mysql-json'); | ||
var mysqlJson = new MysqlJson(options); // Takes mysql package options | ||
</code> | ||
</pre> | ||
## Methods | ||
All methods takes a callback which is called with 2 parameters (err, response) | ||
<pre><code> | ||
// Used to return a mysql connection | ||
mysqlJson.connect(callback); | ||
</code></pre> | ||
<pre><code> | ||
// Used to launch a query to mysql server | ||
mysqlJson.query(mysqlQuery, callback); | ||
</code></pre> | ||
<pre><code> | ||
// Used to insert a new row with JSON data | ||
mysqlJson.insert(tableName, dataToInsert, callback); | ||
</code></pre> | ||
<pre><code> | ||
// Used to update some row(s) matching with JSON conditions | ||
mysqlJson.update(tableName, data, conditions, callback); | ||
</code></pre> | ||
<pre><code> | ||
// Used to delete some row(s) matching with JSON conditions | ||
mysqlJson.delete(tableName, conditions, callback); | ||
</code></pre> | ||
Condition Objects are builds has to be build with this schema : | ||
<pre><code> | ||
{ | ||
column1: {operator:'=', value:'test'}, | ||
column2: {operator:'>', value:29}, | ||
} | ||
</code></pre> | ||
## Usage | ||
@@ -46,8 +93,6 @@ | ||
// Update any documents and set lastname=foo, age=47 where login=root | ||
mysqlJson.update('myTable', { | ||
lastName:'Foo', | ||
age:27 | ||
}, { | ||
login:{operator:'=', value:'root'} | ||
}, function(err, response) { | ||
mysqlJson.update('myTable', | ||
{lastName:'Foo', age:27}, | ||
{login:{operator:'=', value:'root'}}, | ||
function(err, response) { | ||
if (err) throw err; | ||
@@ -61,3 +106,3 @@ console.log(response); | ||
mysqlJson.delete('myTable', { | ||
login:'root' | ||
login:{operator:'=', value:'root'} | ||
}, function(err, response) { | ||
@@ -64,0 +109,0 @@ if (err) throw err; |
Sorry, the diff of this file is not supported yet
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
43501
121
211