@skuyjs/query-builder
v0.0.2
Published
SQL query builder for NodeJS
Downloads
2
Readme
@hamjs/query-builder
SQL query builder for NodeJS
Compatibility
This query builder is compatible with following Database Management Systems.
Installation
You can install by run this command inside your project by using terminal.
npm i https://github.com/hadihammurabi/hamjs-query-builder
Usage
Connecting to database server
Type this following lines in your .js
file and you can run it.
const Database = require('@hamjs/query-builder');
const db = new Database({
dialect: 'mysql',
username: 'root',
password: 'root',
database: 'test',
});
console.log(db.getDialect());
It will give you mysql
(your dialect) as output.
As dialect you choose, it have to installed first.
Example:
npm install mysql
for mysqlnpm install mysql2
for mysql2npm install mariadb
for mariadbnpm install pg
for pg
Querying
Select
Getting all data (select query) can do by use following lines.
...
db
.table('users')
.all()
.then(console.log)
.catch(console.log);
// prints all data in users table
Getting all data with specific column(s), see following example.
...
db
.table('users')
.get('fullname', 'email', 'password')
.then(console.log)
.catch(console.log);
// prints all data in users table, but
// only fullname, email, and password columns
Insert
Inserting a data can do by use following lines.
...
db
.table('users')
.insert([null, 'my fullname', '[email protected]', 'mypass123'])
.then(console.log)
.catch(console.log);
// inserting data into users table
// prints insertion result
Inserting a data with specific column(s) can do by use following lines.
...
db
.table('users')
.insert({
fullname: 'my fullname',
email: '[email protected]',
password: 'mypass123'
})
.then(console.log)
.catch(console.log);
// inserting data into users table
// prints insertion result
Update
Updating all data(s) can do by use following lines.
db
.table('users')
.update({ fullname: 'aye' })
.then(console.log)
.catch(console.log);
// updating all data in users table
// prints update result
Updating data(s) with specific row(s) can do by use following lines.
db
.table('users')
.where({ id: 1 })
.update({ fullname: 'aye' })
.then(console.log)
.catch(console.log);
// updating data(s) with 1 as id in users table
// prints update result
Delete
Deleting all datas can do by use following lines.
db
.table('users')
.del()
.then(console.log)
.catch(console.log);
// deleting all data in users table
// prints deletion result
Deleting data(s) with specific row(s) can do by use following lines.
db
.table('users')
.where({ id: 1 })
.del()
.then(console.log)
.catch(console.log);
// deleting data(s) with 1 as id in users table
// prints deletion result