Basic Db Queries for mysql
npm install sqlim
var sqlim=require('sqlim');
sqlim.dataBase.host='localhost';
sqlim.dataBase.user='root';
sqlim.dataBase.pass='';
sqlim.dataBase.name='';
sqlim.connect();
- select
- from
- where
- where_or
- where_like
- where_like_or
- join
- join_left
- join_right
- getSql
- orderby
- groupby
- clear
- get
- insert
- update
- delete
sqlim.select('field').select('field1 as f').select('sum(field) as total');
// SQL: select field,field1 as f,sum(field) as total
sqlim.from('table').from('table1 t1');
// SQL: from table,table t1
sqlim.where('field',1).where('field2','test');
// SQL: where field=1 and field2='test'
sqlim.where('field',1).where_or('field2','test');
// SQL: where field=1 or field2='test'
sqlim.where_like('field','%t1%').where_like('field2','test');
// SQL: where field like '%t1%' and field2 like 'test'
sqlim.where_like('field','%t1%').where_like_or('field2','test');
// SQL: where field like '%t1%' or field2 like 'test'
sqlim.join('tablename t','t.ID=m.FIELD');
// SQL: join tablename t on t.ID=m.FIELD
sqlim.join_left('tablename t','t.ID=m.FIELD');
// SQL: left join tablename t on t.ID=m.FIELD
sqlim.join_right('tablename t','t.ID=m.FIELD');
// SQL: right join tablename t on t.ID=m.FIELD
sqlim.select('ID,NAME,USERNAME')
.select('EMAIL')
.where('ID',2)
.where('ISADMIN',1)
.from('users');
var sql=sqlim.getSql();
// OUTPUT sql: select ID,NAME,USERNAME,EMAIL from users where ID=2 and ISADMIN=2
sqlim.orderby('ID').orderby('DATE','DESC');
// SQL: order by ID asc,DATE desc
sqlim.groupby('YEAR(datefield)').groupby('STOCKNAME');
// SQL: group by YEAR(datefield),STOCKNAME
Reset Sql
sqlim.clear();
// SQL: ""
Sql Results
var cb=function(err, results, fields){
console.log(results);
}
sqlim.get('tablename',cb);
// Running SQL: select * from tablename;
// OUTPUT: [{ID:1,FIELDNAME:'VALUE',ISTEST:'OK'}]
sqlim.select('ID,FIELDNAME').where('ISTEST','OK').get('tablename',cb);
// Running SQL: select ID,FIELDNAME from tablename;
// OUTPUT: [{ID:1,FIELDNAME:VALUE}]
sqlim.select('ID,FIELDNAME').where('ISTEST','OK').from('tablename').get(cb);
// Running SQL: select ID,FIELDNAME from tablename;
// OUTPUT: [{ID:1,FIELDNAME:VALUE}]
var cb=function(err, result) {
if (error) throw error;
console.log(result.insertId);// last insert id
}
var data={
NAME:'test',
SURNAME:'test'
};
sqlim.insert('tablename',data,cb);
// SQL: "insert into tablename set NAME='test',SURNAME='test'"
var cb=function(err, result) {
if (error) throw error;
console.log(result);
}
var data={
NAME:'test',
SURNAME:'test'
};
sqlim.update('tablename',data,cb);
// SQL: "update tablename set NAME='test',SURNAME='test'"
sqlim.where('ID',17).update('tablename',data,cb);
// SQL: "update tablename set NAME='test',SURNAME='test' where ID=17"
var cb=function(err, result) {
if (error) throw error;
console.log(result.affectedRows);
}
sqlim.delete('tablename',cb);
// SQL: "delete from tablename"
sqlim.where('ID',17).delete('tablename',cb);
// SQL: "delete from tablename where ID=17"