A Library which to generate mysql statements.
-
select
const sqlHelper = require('mysql-generator');
let selectSql = sqlHelper
.Select
.from('user', 'u')
.leftJoin('company', 'c', 'u.companyId = c.id')
.field('u.name')
.field('c.name', 'companyName')
.where('u.id = ?', 22)
.toString();
console.log(selectSql);
-
update
let updateSql = sqlHelper
.Update
.table('user')
.set('password', 'abcd')
.setFields({
name: 'kyo'
})
.useFunction('password', 'MD5')
.where('id IN (?)', [1, 2, 3])
.toString();
console.log(updateSql);
-
insert
let insertSql = sqlHelper
.Insert
.into('user')
.set('name', 'kyo')
.setFieldsRow({
sex: 1,
age: 20,
email: 'some@qq.com',
password: 'abcd'
})
.useFunction('password', 'SHA')
.toString();
console.log(insertSql);
insertSql = sqlHelper
.Insert
.into('user')
.setFieldsRows([{
sex: 1,
age: 20,
email: 'some1@qq.com',
password: 'abcd'
}, {
sex: 0,
age: 18,
email: 'some2@qq.com',
password: 'hjkl'
}])
.useFunction('password', 'MD5')
.toString();
console.log(insertSql);
-
delete
let deleteSql = sqlHelper
.Delete
.from('user')
.where('id IN (?)', [1, 2, 3])
.toString();
console.log(deleteSql);
We prepare some test, coming soon.
Contributors are welcome, please fork and send pull requests! If you have any ideas on how to make this project better then please submit an issue.