- clear() ⇒
MySQL
Clear current values in Query Builder
- select(key, as) ⇒
MySQL
Add select parameter
- from(table_name) ⇒
MySQL
Add table name to select from
- where(column, value, condition, refrence) ⇒
MySQL
Add where parameter
- whereGroupStart(refrence) ⇒
MySQL
Grouping where with brackets
- set(column, value) ⇒
MySQL
Add set into update
- value(column, value) ⇒
MySQL
Add value to insert / replace
- insert(table) ⇒
MySQL
Insert query
- update(table) ⇒
MySQL
Update query
- replace(table) ⇒
MySQL
Replace query
- delete(table) ⇒
MySQL
Delete query
- innerJoin(table) ⇒
MySQL
Add inner join to select
- leftJoin(table) ⇒
MySQL
Add left join to select
- on(table) ⇒
MySQL
Set columns of join
- query() ⇒
string
Return generated query based on current data
- values() ⇒
Array.<(string|number)>
Get value array based on current data
- exec(start_transaction) ⇒
MySQLResult
Execute query based on current data
- commit() ⇒
boolean
Commit current queries. You need to use .exec(true)
- rollback(error) ⇒
boolean
Rollback current queries. You need to use .exec(true)
- config(config)
Configure MySQL server and database info
- initialize()
Initialize MySQL pool manually. It will automatically initiate. You don't have to use it manually.
- getConnection() ⇒
Object
Get new connection from MySQL pool
- beginTransaction(connection) ⇒
Object
Begin new transaction If you give connection, it will start transaction on given connection. else it will create new connection and return it.
- rollback(connection) ⇒
Object
Rollback transaction
- commit(connection) ⇒
Object
Commit transaction
- execute(sql, timeout, values, connection, rollback) ⇒
MySQLResult
Execute query
- escape(str) ⇒
string
Escape strings for use in queries
- MySQLResult :
Object
- MySQLConfig :
Object
Clear current values in Query Builder
Kind: global function
Example
db.clear();
Add select parameter
Kind: global function
Param | Type | Description |
---|---|---|
key | string |
Column name |
as | string | number |
Used as select as [?] |
Example
db.select('username', 'uname'); // username AS uname
Add table name to select from
Kind: global function
Param | Type | Description |
---|---|---|
table_name | string |
Table name |
Example
// SELECT * FROM test
db
.select('*')
.from('test');
Add where parameter
Kind: global function
Default: AND
Param | Type | Description |
---|---|---|
column | string |
Column name |
value | string | number |
Value to check |
condition | string |
Condition to where (=, <, >, !=, <=, >=) |
refrence | string |
Referenced to previous where |
Example
// SELECT * FROM test WHERE name = ?
db
.from('test')
.where('name', 'test');
Example
// SELECT * FROM test WHERE price > ?
db
.from('test')
.where('price', 30, '>');
Example
// SELECT * FROM test WHERE price > ? AND name = ?
db
.from('test')
.where('price', 30, '>')
.where('name', 'test');
Example
// SELECT * FROM test WHERE price > ? OR name = ?
db
.from('test')
.where('price', 30, '>')
.where('name', 'test', '=', 'OR');
Grouping where with brackets
Kind: global function
Param | Type | Description |
---|---|---|
refrence | string |
Referenced to previous where |
Example
// SELECT * FROM test WHERE price = ? AND (name = ? OR name = ?)
db
.from('test')
.where('price', 30)
.whereGroupStart()
.where('name', 't1')
.where('name', 't2')
.whereGroupEnd();
Example
// SELECT * FROM test WHERE price = ? OR (name = ? OR name = ?)
db
.from('test')
.where('price', 30)
.whereGroupStart('OR')
.where('name', 't1')
.where('name', 't2')
.whereGroupEnd();
Add set into update
Kind: global function
Param | Type | Description |
---|---|---|
column | string |
Column name |
value | string | number |
Value to set |
Add value to insert / replace
Kind: global function
Param | Type | Description |
---|---|---|
column | string |
Column name |
value | string | number |
Value |
Insert query
Kind: global function
Param | Type | Description |
---|---|---|
table | string |
Table name |
Example
// INSERT INTO test (name, age) VALUES (?, ?)
db
.insert('test')
.value('name', 'John')
.value('age', 21);
Update query
Kind: global function
Param | Type | Description |
---|---|---|
table | string |
Table name |
Example
// UPDATE test SET name = ?, age = ? WHERE id = ?
db
.update('test')
.set('name', 'John')
.set('age', 21)
.where('id', 1);
Replace query
Kind: global function
Param | Type | Description |
---|---|---|
table | string |
Table name |
Example
// REPLACE INTO test (name, age) VALUES (?, ?)
db
.replace('test')
.value('name', 'John')
.value('age', 21);
Delete query
Kind: global function
Param | Type | Description |
---|---|---|
table | string |
Table name |
Example
// DELETE FROM test WHERE id = ?
db
.delete('test')
.where('id', 1);
Add inner join to select
Kind: global function
Param | Type | Description |
---|---|---|
table | string |
Table name |
Example
// SELECT * FROM test INNER JOIN profile ON test.id = profile.tid WHERE 1
db
.from('test')
.innerJoin('profile')
.on('test.id', 'profile.tId');
Add left join to select
Kind: global function
Param | Type | Description |
---|---|---|
table | string |
Table name |
Example
// SELECT * FROM test LEFT JOIN profile ON test.id = profile.tid WHERE 1
db
.from('test')
.leftJoin('profile')
.on('test.id', 'profile.tId');
Set columns of join
Kind: global function
Param | Type | Description |
---|---|---|
table | string |
Table name |
Example
// SELECT * FROM test INNER JOIN profile ON test.id = profile.tid WHERE 1
db
.from('test')
.innerJoin('profile')
.on('test.id', 'profile.tId');
Return generated query based on current data
Kind: global function
Example
// returns => 'SELECT * FROM test INNER JOIN profile ON test.id = profile.tid WHERE 1'
db
.from('test')
.innerJoin('profile')
.on('test.id', 'profile.tId')
.query();
Get value array based on current data
Kind: global function
Example
// ['John', 21, 1]
db
.update('test')
.set('name', 'John')
.set('age', 21)
.where('id', 1)
.values();
exec(start_transaction) ⇒ MySQLResult
Execute query based on current data
Kind: global function
Param | Type | Description |
---|---|---|
start_transaction | boolean |
If true, MySQL will start new transaction. |
Commit current queries. You need to use .exec(true)
Rollback current queries. You need to use .exec(true)
Kind: global function
Param | Type | Description |
---|---|---|
error | Error |
Error will throw after rollback. |
Configure MySQL server and database info
Kind: global function
Param | Type | Description |
---|---|---|
config | MySQLConfig |
Config Data |
Initialize MySQL pool manually. It will automatically initiate. You don't have to use it manually.
Get new connection from MySQL pool
Kind: global function
Returns: Object
- MySQL Connection
Begin new transaction If you give connection, it will start transaction on given connection. else it will create new connection and return it.
Kind: global function
Returns: Object
- connection MySQL Connection
Param | Type | Description |
---|---|---|
connection | Object |
MySQL Connection |
Rollback transaction
Kind: global function
Returns: Object
- connection MySQL Connection
Param | Type | Description |
---|---|---|
connection | Object |
MySQL Connection |
Commit transaction
Kind: global function
Returns: Object
- connection MySQL Connection
Param | Type | Description |
---|---|---|
connection | Object |
MySQL Connection |
execute(sql, timeout, values, connection, rollback) ⇒ MySQLResult
Execute query
Kind: global function
Param | Type | Description |
---|---|---|
sql | string |
Query |
timeout | number |
Timeout for query execution (default: 40000) = 40 mins. |
values | Array.<(string|number)> |
Values for query Question marks (?) |
connection | Object |
If connection given it will be used. else it will get new one from pool and execute. |
rollback | boolean |
If true it will rollback on faliure |
Escape strings for use in queries
Kind: global function
Param | Type | Description |
---|---|---|
str | string |
String to escape |
Kind: global typedef
Properties
Name | Type | Description |
---|---|---|
results | Array.<string> |
Array of results of query |
fields | Array.<string> |
Array of columns in result |
Kind: global typedef
Properties
Name | Type | Description |
---|---|---|
host | string |
Hostname of MySQL Server (default: 'localhost') |
port | number |
Port of MySQL Server (default: 3306) |
user | string |
Username of MySQL Server (default: 'root') |
password | string |
Password of MySQL Server (default: '') |
database | string |
Name of the database |