@hamjs/query-builder v0.0.2
@hamjs/query-builder
SQL query builder for NodeJS
TODOS:
- Go to NPMJS Registry
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-builderUsage
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 mysqlfor mysqlnpm install mysql2for mysql2npm install mariadbfor mariadbnpm install pgfor 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 tableGetting 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 columnsInsert
Inserting a data can do by use following lines.
...
db
.table('users')
.insert([null, 'my fullname', 'example@email.top', 'mypass123'])
.then(console.log)
.catch(console.log);
// inserting data into users table
// prints insertion resultInserting a data with specific column(s) can do by use following lines.
...
db
.table('users')
.insert({
fullname: 'my fullname',
email: 'example@email.top',
password: 'mypass123'
})
.then(console.log)
.catch(console.log);
// inserting data into users table
// prints insertion resultUpdate
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 resultUpdating 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 resultDelete
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 resultDeleting 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