nodejs-mysql-querybuilder
v1.0.2
Published
Simple sync-mysql query builder
Downloads
6
Readme
QueryBuilder
Installation
npm install nodejs-mysql-querybuilder --save
Documentation
Initialisation
const QueryBuilder = require('nodejs-mysql-querybuilder');
const db = new QueryBuilder({
host: 'localhost',
user: 'user',
password: 'password',
database: 'database'
});
db.setTable('table');
db.connect();
Select
The select method take one string parameter
db.setTable('users');
db.select('username, password')
.where('id', 12)
.and('mail', '[email protected]');
db.execute();
const res = db.fetch();
Insert
The select method take an object as parameter
db.setTable('users');
db.insert({
username: 'johndoe',
password: 'foobar',
mail: '[email protected]'
});
db.execute();
Update
The Update method take an object as parameter and need a where filter
db.setTable('users');
db.update({
username: 'johndoe',
password: 'foobar',
mail: '[email protected]'
});
db.where('id', 12);
db.execute();
Delete
The Delete method take two parameters
db.setTable('users');
db.delete('id', 12);
db.execute();
Filters
Where
where filter is unique, you need to use and if you need another filter
db.setTable('users');
db.select('username, password').where('id', 12).execute();
let res = db.fetchAll();
And
db.setTable('users');
db.select('username, password').where('id', 12).and('mail', '[email protected]').execute();
let res = db.fetchAll();
Or
db.setTable('users');
db.select().where('id', 12).or('mail', '[email protected]').execute();
let res = db.fetchAll();
Join
db.setTable('users');
db.select('users.username, messages.message')
.join('INNER', 'messages')
.on('messages.iduser', 'users.id');