当前位置: 首页 > 后端技术 > Node.js

mysqls是一个专为node.js设计的生成sql语句的插件,链式调用,使用灵活,支持东西

时间:2023-04-03 13:47:00 Node.js

mysqls是用JavaScript写的,crudformysql。也可以很方便的使用事务。.js插件生成sql语句,链式调用,使用灵活。支持生成sql语法,也支持生成语法后直接调用,支持thing等特性。API参考了非常流行的ThinkPHP模型API。npm地址:https://www.npmjs.com/package...安装:npminstallmysqls--save-devmysqls参数说明init:sql初始化APIexec:执行sql语句sql:链式调用生成sql语句,支持generation然后直接执行sql语句transaction:ExecutethetransactionAPIprojectusing://importmethodimport{init,exec,sql,transaction}from'mysqls'//requiremethodlet{init,exec,sql,transaction}=require('mysqls')mysql配置初始化://可以在项目启动时初始化配置init({host:'localhost',user:'root',password:'123456',database:'test',port:3306,})init参数说明ispool:是否用连接池初始化(默认:true)host:主机地址(默认:'127.0.0.1')user:用户名(默认:'root')password:数据库密码(默认:'root')database:使用的数据库(default:'test')port:端口(default:'3306')waitConnection:是否等待连接(在连接池中使用)(默认:true)connectionLimit:连接池大小(默认:10)queueLimit:队列限制(默认:0)只生成sql语句casesql.table('node_table').field('id,name').where({id:1}).select()//resultSELECTid,nameFROMnode_tableWHEREid=1使用exec函数执行sql语句constsqlstr=sql.table('节点表')。字段('id,name').where({id:1}).select();constresult=awaitexec(sqlstr);使用sql.prototype.exec链调用constresult=sql.table('node_table').field('id,name').where({id:1}).select(true).exec();select方法在链式调用执行sql时需要传递参数:true也适用于update(true),insert(true),delet(true),query(true)方法usePromisemethod//useexecfunctionexec(sql.table('web_pages').where({id:147}).select()).then(res=>{console.log(res)}).catch(err=>{console.log(err)})//使用执行方法sql.table('web_pages').where({id:147}).select(true).exec().then(res=>{console.log(res)}).catch(err=>{console.log(err)})使用async/await//使用exec函数constresult=awaitexec(sql.table('web_pages').where({id:147}).select())//使用exec方法constresult=awaitsql.table('web_pages').where({id:147}).select(true).exec()处理事务consttranSqlArr=[sql.table('table1').data({number:'number-5'}).update(),sql.table('table2').data({number:'number+5'}).update()]constresult=awaittransaction(tranSqlArr)生成sql语句简单用法注意:sql调用方法的顺序内部已经排序,所以查询可以写成没有严格的sql语句顺序sql.table('node_table').field('id,name').where({id:1}).select()SELECTid,nameFROMnode_tableWHEREid=1insertsql.table('node_table').data({name:'zane',email:'752652@qq.com'}).insert()INSERTINTOnode_table(name,email)VALUES(`zane`,`752636052@qq.com`)更新sql.table('node_table').data({name:'zane',email:'752636052@qq.com'}).update()UPDATEnode_tableSETname=`zane`,email=`752636052@q??q.com`删除sql.table('node_table').where({name:'zane'}).delet();DELETEFROMnode_tableWHEREname=`zane`生成sql语句高级用法//参数json多字节sql.table('node_table').where({id:1,name:'zane'}).select()SELECT*FROMnode_tableWHEREid=1ANDname=`zane`//参数组letdata=[{id:1,name:'zhangsan',_type:'or'},{sex:1,number:3}]sql.table('node_table').where(data).select()SELECT*FROMnode_tableWHERE(id=1ORname=`zhangsan`)AND(sex=1ANDnumber=3)//多字段连接方式letdata=[{id:1,name:'zhangsan',_type:'or',_nexttype:'or'},{sex:1,number:3,_type:'and'}]sql.table('node_table').where(data).select()SELECT*FROMnode_tableWHERE(id=1ORname=`zhangsan`)OR(sex=1ANDnumber=3)//表达式查询letdata={id:{eq:100,egt:10,_type:'or'},name:'zhangshan'}sql.table('node_table').where(data).select()SELECT*FROMnode_tableWHERE((id=100)OR(id>=10))ANDname=`zhangshan`//混合查询letdata=[{id:{eq:100,egt:10,_type:'or'},name:'张山',_nexttype:'or'},{status:1,name:{like:'%zane%'}}]sql.table('node_table').where(data).select()SELECT*FROMnode_tableWHERE(((id=100)OR(id>=10))ANDname=`zhangshan`)OR(status=1AND((nameLIKE`%zane%`)))//UNION,UNIONALL联合使用sql.union('SELECT*FROMthink_user_1',true).union('SELECT*FROMthink_user_2',true).union(['SELECT*FROMthink_user_3','SELECTnameFROMthink_user_4']).union('SELECT*FROMthink_user_5',true).select()得到(SELECT*FROMthink_user_1)UNIONALL(SELECT*FROMthink_user_2)UNIONALL(SELECT*FROMthink_user_3)UNION(SELECTnameFROMthink_user_4)UNION(SELECT*FROMthink_user_5)请参考详细文档更多使用目录1.介绍1.1.参数说明及交易2.链式操作2.1.WHERE2.2.TABLE2.3.ALIAS2.4.DATA2.5.FIELD2.6.ORDER2.7.LIMIT2.8.PAGE2.9.GROUP2.10.HAVING2.11。UNION2.12.DISTINCT2.13.COMMENT3.CURD调用3.1.SELECT3.2.UPDATE3.3.INSERT3.4.DELETE4。查询方式4.1.基本查询4.2。表达式查询4.3.区间查询4.4.组合查询4.5.统计查询4.6.SQL查询4.7。子查询