mysqls是在node.js场景中使用mysql,根据传入的参数生成相应的sql语句。
插件本身只负责生成sql语句,不执行任何的增删改查,增删改查交给mysql。
API参考很流行的ThinkPHP模型API,因为它已经做够流行和好用了。非常感谢ThinkPHP文档,很多案例参考其文档
安装:
npm install mysqls --save-dev
项目使用:
import { execute,sql,transaction } from 'mysqls'
let { execute,sql,transaction } = require('mysqls')
参数说明
execute :执行单挑sql语句 参数:(config,sqlStr)
sql :链式调用生成sql语句 链式调用语法,参考后文
transaction :执行事务相关任务时使用 参数:(config,sqlArr)
定义一个公共的config配置
let config={
host:'localhost',
user:'root',
password:'123456',
database:'web-performance',
port:'3306',
}
使用Promise方式
let sqlstr = sql.table('web_pages').where({id:147}).select()
execute(config,sqlstr).then(res=>{
console.log(res)
}).catch(err=>{
console.log(err)
})
使用async/await
let sqlstr = sql.table('web_pages').where({id:147}).select()
let result = await execute(config,sqlstr)
console.log(result)
处理事务
let tranSqlArr = [
sql.table('table1').data({number:number-5}).update(),
sql.table('table2').data({number:number+5}).update()
]
let result = await transaction(config,tranSqlArr)
console.log(result)
生成sql语句简单用法
备注:sql调用方法的顺序内部已经做了排序,因此可以不按严格的sql语句顺序来写
查询
sql
.table('node_table')
.field('id,name')
.where({id:1})
.select()
SELECT id,name FROM node_table WHERE id=1
插入
sql
.table('node_table')
.data({name:'zane',email:'752636052@qq.com'})
.insert()
INSERT INTO node_table (name,email) VALUES (`zane`,`752636052@qq.com`)
更新
sql
.table('node_table')
.data({name:'zane',email:'752636052@qq.com'})
.update()
UPDATE node_table SET name=`zane`,email=`752636052@qq.com`
删除
sql .table('node_table')
.where({name:'zane'})
.delet();
DELETE FROM node_table WHERE name=`zane`
生成sql语句高级用法
sql
.table('node_table')
.where({id:1,name:'zane'})
.select()
SELECT * FROM node_table WHERE id=1 AND name=`zane`
let data=[
{id:1,name:'zhangsan',_type:'or'},
{sex:1,number:3}
]
sql.table('node_table').where(data).select()
SELECT * FROM node_table WHERE (id=1 OR name=`zhangsan` ) AND (sex=1 AND number=3 )
let data=[
{id:1,name:'zhangsan',_type:'or',_nexttype:'or'},
{sex:1,number:3,_type:'and'}
]
sql.table('node_table').where(data).select()
SELECT * FROM node_table WHERE (id=1 OR name=`zhangsan`) OR (sex=1 AND number=3)
let data={
id:{eq:100,egt:10,_type:'or'},
name:'zhangshan'
}
sql.table('node_table').where(data).select()
SELECT * FROM node_table WHERE ((id=100) OR (id>=10)) AND name=`zhangshan`
let data=[{
id:{eq:100,egt:10,_type:'or'},
name:'zhangshan',
_nexttype:'or'
},{
status:1,
name:{like:'%zane%'}
}]
sql.table('node_table').where(data).select()
SELECT * FROM node_table WHERE (((id=100) OR (id>=10)) AND name=`zhangshan`) OR (status=1 AND ((name LIKE `%zane%`)))
sql
.union('SELECT * FROM think_user_1',true)
.union('SELECT * FROM think_user_2',true)
.union(['SELECT * FROM think_user_3','SELECT name FROM think_user_4'])
.union('SELECT * FROM think_user_5',true)
.select()
得到
(SELECT * FROM think_user_1) UNION ALL
(SELECT * FROM think_user_2) UNION ALL
(SELECT * FROM think_user_3) UNION
(SELECT name FROM think_user_4) UNION
(SELECT * FROM think_user_5)
更多用法请查看详细文档
文档目录
项目运行
git clone https:
npm install
npm run dve
npm run build