- Installing
- Usage
- Create table
- Select
- Select query
- Result
- Join
- Inner join
- Union
- Where
- Order by
- Group by
- Having
- Limit
- Offset
- Execute
- Update
$ npm i liqd-sql
const SQL = new (require('liqd-sql'))(
{
mysql :
{
host : 'localhost',
user : 'root',
password : '',
database : 'test'
}
});
config
{Object}table
{String}execute
{Boolean}
await SQL.query({
columns :
{
id : { type: 'BIGINT:UNSIGNED', increment: true },
name : { type: 'VARCHAR:255' },
surname : { type: 'VARCHAR:255' },
cityID : { type: 'BIGINT:UNSIGNED' }
},
indexes : {
primary : 'id',
unique : [],
index : [ 'city' ]
}
}, 'users' ).create_table( true );
await SQL.query({
columns :
{
id : { type: 'BIGINT:UNSIGNED', increment: true },
name : { type: 'VARCHAR:255' }
},
indexes : {
primary : 'id',
unique : [],
index : [ 'name' ]
}
}, 'cities' ).create_table( true );
columns
{String}data
{Any}
let data = await SQL.query( 'users' ).select_row();
Output
{
ok : true,
error : null,
affected_rows : 1,
changed_rows : 0,
inserted_id : null,
inserted_ids : [],
changed_id : null,
changed_ids : [],
row : { id: 1, name: 'John', surname: 'D.', cityID: 1 },
rows : [ { id: 1, name: 'John', surname: 'D.', cityID: 1 } ],
sql_time : 1,
time : 1,
query : 'SELECT * FROM `users` LIMIT 1'
};
columns
{String}data
{Any}
let data = await SQL.query( 'users' ).select();
Output
{
ok : true,
error : null,
affected_rows : 2,
changed_rows : 0,
inserted_id : null,
inserted_ids : [],
changed_id : null,
changed_ids : [],
row : { id: 1, name: 'John', surname: 'D.', cityID: 1 },
rows : [ { id: 1, name: 'John', surname: 'D.', cityID: 1 }, { id: 2, name: 'Mark', surname: 'T.', cityID: 1 } ],
sql_time : 1,
time : 1,
query : 'SELECT * FROM `users`'
};
columns
{String}data
{Any}alias
{String}
let data = await SQL.query( 'users' ).select_row_query();
Output
'SELECT * FROM `users` LIMIT 1'
columns
{String}data
{Any}alias
{String}
let data = await SQL.query( 'users' ).select_query();
Output
SELECT * FROM `users`
{
ok : true,
error : null,
affected_rows : 0,
changed_rows : 0,
inserted_id : null,
inserted_ids : [],
changed_id : null,
changed_ids : [],
row : null,
rows : [],
sql_time : 0,
time : 0,
query : ''
};
ok
{Boolean}error
{Object}affected_rows
{Number}changed_rows
{Number}inserted_id
inserted_ids
{Array}changed_id
changed_ids
{Array}row
{Object}rows
{Array}rows
{Array}sql_time
{Number}time
{Number}query
{String}
table
{String}condition
{String}data
{Any}
let data = await SQL.query( 'users u' ).join( 'cities c', 'u.cityID = c.id' ).select_query( '*' );
Output
SELECT * FROM `users` `u` LEFT JOIN `cities` `c` ON `u`.`cityID` = `c`.`id`
table
{String}condition
{String}data
{Any}
let data = await SQL.query( 'users u' ).inner_join( 'cities c', 'u.cityID = c.id' ).select_query( '*' );
Output
SELECT * FROM `users` `u` INNER JOIN `work` `w` ON `u`.`id` = `w`.`userID`
union
{String|Array|Query}
condition
{String}data
{Any}
let data = await SQL.query( 'users' ).where( ' id > 10 AND name = :?', 'John' ).select_query( '*' );
Output
SELECT * FROM `users` WHERE `id` > 10 AND `name` = 'John'
let data = await SQL.query( 'users' ).where( ' id > 10 ' ).where( 'name = :?', 'John' ).select_query( '*' );
Output
SELECT * FROM `users` WHERE ( `id` > 10 ) AND ( `name` = 'John' )
columns
{String}data
{Any}
let data = await SQL.query( 'users' ).order_by( 'name ASC, surname DESC' ).select_query( '*' );
Output
SELECT * FROM `users` ORDER BY `name` ASC, `surname` DESC
- use one time
columns
{String}data
{Any}
let data = await SQL.query( 'users' ).group_by( 'surname DESC' ).select_query( '*' );
Output
SELECT * FROM `users` GROUP BY `surname`
condition
{String}data
{Any}
let data = await SQL.query( 'users' ).having( 'id > 3' ).select_query( '*' );
Output
SELECT * FROM `users` HAVING id > 3
limit
{Number}
let data = await SQL.query( 'users' ).limit( 15 ).select_query( '*' );
Output
SELECT * FROM `users` LIMIT 15
offset
{Number}
let data = await SQL.query( 'users' ).limit( 15 ).offset( 15 ).select_query( '*' );
Output
SELECT * FROM `users` LIMIT 15 OFFSET 15
let data = await SQL.query( 'SELECT * FROM users' ).execute();
Output
{
ok : true,
error : null,
affected_rows : 2,
changed_rows : 0,
inserted_id : null,
inserted_ids : [],
changed_id : null,
changed_ids : [],
row : { id: 1, name: 'John', surname: 'D.', cityID: 1 },
rows : [ { id: 1, name: 'John', surname: 'D.', cityID: 1 }, { id: 2, name: 'Mark', surname: 'T.', cityID: 1 } ],
sql_time : 1,
time : 1,
query : 'SELECT * FROM `users`'
};
set
{String|Array|Object}data
{Any}
let data = await SQL.query( 'users' ).update( { id: 1, name: 'Johnson' } );
Output
{
ok : true,
error : null,
affected_rows : 1,
changed_rows : 1,
inserted_id : null,
inserted_ids : [],
changed_id : null,
changed_ids : [],
row : null,
rows : [],
sql_time : 1,
time : 1,
query : 'UPDATE `users` SET `id` = CASE WHEN `id` = 1 THEN 1 ELSE `id` END, `name` = CASE WHEN `id` = 1 THEN 'Johnson' ELSE `name` END WHERE ( `id` IN (1) )'
};
let data = await SQL.query( 'users' ).where( 'id = 1' ).update( { name: 'Johnson' } );
Output
{
ok : true,
error : null,
affected_rows : 1,
changed_rows : 1,
inserted_id : null,
inserted_ids : [],
changed_id : null,
changed_ids : [],
row : null,
rows : [],
sql_time : 1,
time : 1,
query : 'UPDATE `users` SET `name` = 'Johnson' WHERE `id` = 1 '
};
let data = await SQL.query( 'users' ).where( 'id = 1' ).update( 'name = :?', 'Johnson' } );
Output
{
ok : true,
error : null,
affected_rows : 1,
changed_rows : 1,
inserted_id : null,
inserted_ids : [],
changed_id : null,
changed_ids : [],
row : null,
rows : [],
sql_time : 1,
time : 1,
query : 'UPDATE `users` SET `name` = 'Johnson' WHERE `id` = 1 '
};
data
{Array|Object}ignore
{Boolean}
let data = await SQL.query( 'users' ).insert( { id: 1, name: 'John', surname: 'D.' } );
Output
{
ok : true,
error : null,
affected_rows : 1,
changed_rows : 1,
inserted_id : 1,
inserted_ids : [ 1 ],
changed_id : null,
changed_ids : [ 1 ],
row : null,
rows : [],
sql_time : 1,
time : 1,
query : 'INSERT INTO `users` ( id, name, surname ) VALUES ( 1, 'John', 'D.' )'
};
let data = await SQL.query( 'users' ).insert( { id: 1, name: 'John', surname: 'D.' }, true );
Output
{
ok : true,
error : null,
affected_rows : 1,
changed_rows : 1,
inserted_id : 1,
inserted_ids : [ 1 ],
changed_id : null,
changed_ids : [ 1 ],
row : null,
rows : [],
sql_time : 1,
time : 1,
query : 'INSERT IGNORE INTO `users` ( id, name, surname ) VALUES ( 1, 'John', 'D.' )'
};
data
{Array|Object}
let data = await SQL.query( 'users' ).set( { id: 1, name: 'John', surname: 'D.' } );
Output
{
ok : true,
error : null,
affected_rows : 1,
changed_rows : 1,
inserted_id : 1,
inserted_ids : [ 1 ],
changed_id : null,
changed_ids : [ 1 ],
row : null,
rows : [],
sql_time : 1,
time : 1
};