doSQL
Easy and intuitive way to do queries to a MySQL database using only JSON.
Install
Install doSQL using NPM:
npm install --save dosql
Usage
Connect(obj)
Use this for connect to a database. For example:
//Importvar db = ; //Connect to databasedb;
Do(obj, callback)
Run a query defined by obj
object, and then call your callback
function with the results.
The obj
object must contain the next keys:
in
: string with the table where you want to do the query.do
: action for do. It can beselect
,insert
,delete
orupdate
.
callback
will get only argument, called result
:
result
will benull
if an error occurred running the query.result
will be an array if no error occurred.
Examples
For select data, you can optionally add a where
key, containing an object with the keys that the rows must satisfy. On version 0.2.1
, the where
argument can be a string for a complex query.
//Example 1 - Select all rows from table // SELECT * FROM yourtabledb;
//Example2 - Select only rows that satisfy the conditions // SELECT * FROM yourtable WHERE key1="value1" AND key2="value2";db;
For insert data, you must add a values
key. It can be an object with the values for insert into your table, or an array with some objects. If no error occurred, the result
variable will be an array with the inserted rows.
//Example 1 - Insert only one row to your table // INSERT INTO yourtable (k1, k2, k3) VALUES ("v1", "v2", "v3");db;
//Example 2 - Insert two rows to your table // INSERT INTO yourtable (k1, k2, k3) VALUES ("v1", "v2", "v3"),("v4", "v5", "v6");var val = k1: 'v1' k2: 'v2' k3: 'v3'k1: 'v4' k2: 'v5' k3: 'v6';db;
For update rows, you must add a set
and where
keys.
//Example1 - Update some rows // UPDATE yourtable SET key1="va1" WHERE key2="va2"db;
For delete rows, you must add a where
key:
//Example1 - Delete some rows // DELETE yourtable WHERE key="value"db;
Tip
You can use if(result)
for check if something went wrong. For example,
db;
License
doSQL is under the MIT license.