This library was written from simple work with databases. In current version supported next famous databases: sqlite3 and MySQL. In future, list with supported databases will be replenishing.
Before you use this module you must install it.
npm install sql-easy-lib --save
and you can include it!
const SQLEasy = require('sql-easy-lib');
Before work, you must know about twice main tools: get_from_key and Request.
const SQLEasy = require('sql-easy-lib');
var req = new SQLEasy.Request([{id: 1, value: 12}, {name: 'CirillaGif'}]);
/* This is boolean operation: "(id=1 AND value=12) OR (name=`CirillaGif`)" */
var get_from_key = SQLEasy.tools.get_from_key;
/* get_from_key function, about him in down. */
Request use from create logic expression example:
// ...
new SQLEasy.Request([{id: 1, value: 12}, {name: 'CirillaGif'}]);
(id=1 AND value=12) OR (name=`CirillaGif`)
get_from_key use for more efficient queries from buffer variables. example:
// Without use get_from_key
const SQLEasy = require('sql-easy-lib');
var database = new SQLEasy.SQLite3_database('/path/to/database.db');
var rolesData = database.get('users').map(i => {
return {
user: i,
role_data: database.get('role', new SQLEasy.Request([{id: i.role}]))
}
});
// With use get_from_key
const SQLEasy = require('sql-easy-lib');
var database = new SQLEasy.SQLite3_database('/path/to/database.db');
var roleData = database.get('role', new SQLEasy.Request([{id: i.role}]));
var rolesData = database.get('users').map(i => {
return {
user: i,
role_data: SQLEasy.tools.get_from_key(roleData, new SQLEasy.Request([{id: i.role}]))
}
});
In all databases methods is equally (except for the connection).
const SQLEasy = require('sql-easy-lib');
/* Method for connection sqlite3 database */
var sqlite3 = new SQLEasy.SQLite3_database('/path/to/database.db');
/* Method for connection MySQL database */
var mysql = new SQLEasy.MySQL_database({
host: "mysql.example.org",
user: "username",
password: "password"
});
mysql.set_db("Example_db"); // setting database in server
(for example, we use abstract database object): database
const SQLEasy = require('sql-easy-lib');
var database = new SQLEasy.AnyDatabase(args);
This is getting items from table:
SELECT
syntax:
database.get(
'table_name',
new SQLEasy.Request([{param: 'value'}]), // Not required: ...WHERE (CONDITION) in request
'*' // Not required: Items in table
);
SELECT * FROM table_name WHERE (param=`value`)
This is set values in items in table:
UPDATE .. SET
syntax:
database.set(
'table_name',
new SQLEasy.Request([{param: 'value_require_edit'}]), // Required: ...WHERE (CONDITION) in request
{param: 'value'} // Required: Items in table
);
UPDATE table_name SET param=`value` WHERE (param=`value_require_edit`)
This method for remove items into database.
DELETE
syntax:
database.remove(
'table_name',
new SQLEasy.Request([{param: 'value'}]) // Required: ...WHERE (CONDITION) in request
);
DELETE FROM table_name WHERE (param=`value_require_edit`)
Method for add items into database.
INSERT
syntax:
database.add(
'table_name',
[{param: 'value'}] // Required: ... Rows what you add in table.
);
INSERT INTO table_name (param) VALUES (`value`)