@tanshenghu/exec-mysql
v1.0.5
Published
mysql、sqlite语法生成及执行工具
Downloads
4
Readme
@tanshenghu/exec-mysql
Install Package
npm install @tanshenghu/exec-mysql --save-dev
or
yarn add @tanshenghu/exec-mysql --dev
Support
- mysql
- sqlite
Example
mysql
// import package code
const { execMysql } = require('@tanshenghu/exec-mysql');
// options by config
const mysql = execMysql({
host : '127.0.0.1',
user : 'tanshenghu',
password : '******',
port : 3306,
database : 'web_front',
});
// Add Table Prefix
// mysql.addTablePrefix('mytable_');
// select SQL
const dbModel = await mysql
.select('admin', {prefix: 'custom_mytable_prefix_'})
// .count()
.where({username: 'tanshenghu'})
// .where([{username: 'tanshenghu'}, 'AND', {sex: 1}])
// .where({'username LIKE': '%sheng%'})
// .order('id')
// .limit([0, 10])
.end()
// .insert('admin')
// .setData({username: 'New My Name'})
// .end()
.exec();
// insert SQL
const dbModel = await mysql
.insert('admin')
.setData({
username: 'tanshenghu',
email: '[email protected]',
QQ: '511568692',
weixin: 'TanShenghu',
address: 'Hangzhou, Zhejiang, China'
})
.end()
.exec();
// update SQL
const dbModel = await mysql
.update('admin')
.set({
username: 'tanshenghu'
})
.where({id: 1})
.end()
.exec();
// delete SQL
const dbModel = await mysql
.delete('admin')
.where({id: 1})
.end()
.exec();
// custom edit SQL
const dbModel = await mysql
.delete('admin')
.where({id: 1})
.edit(function(sql){
// ...
const newSql = 'DELETE FROM admin WHERE id=2';
return newSql;
})
.end()
.exec();
// custom edit SQL
const dbModel = await mysql
.editSQL(function(){
return ['SELECT FROM ...', 'INSERT INTO ...'];
})
.exec();
sqlite
const { execSqlite } = require('@tanshenghu/exec-mysql');
const mysql = execSqlite();
// API with MySql
// ...
// ...
// ...
// mysql or sqlite More API call Me
Appreciate
Author
TanShenghu (福虎)