SQL query builder for NodeJS
This query builder is compatible with following Database Management Systems.
You can install by run this command inside your project by using terminal.
npm i @skuyjs/query-builder
Type this following lines in your .js
file and you can run it.
const Database = require('@skuyjs/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
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
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 result
Inserting 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 result
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
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