mysqls为node.js而编写的sql语句生成插件 crud for mysql.

栏目: Node.js · 发布时间: 5年前

内容简介:It is written in JavaScript,crud for mysql.You can also use transactions very easily.mysqls 一款专为node.js生成sql语句的插件,链式调用,使用灵活。支持生成sql语法,也支持生成语法之后直接调用,支持事物等特性。API参考很流行的ThinkPHP模型API。

mysqls

It is written in JavaScript,crud for mysql.You can also use transactions very easily.

mysqls 一款专为node.js生成 sql 语句的插件,链式调用,使用灵活。支持生成sql语法,也支持生成语法之后直接调用,支持事物等特性。

API参考很流行的ThinkPHP模型API。

npm地址: www.npmjs.com/package/mys…

github: github.com/wangweiange…

mysqls是笔者在开发过程中为了更简单、更高效的开发效率而封装的一个库,年初做的,最近从新小修改和加强了一下。分享给有需要的人,感兴趣的也可以看看笔者的思路。因为个人使用过ThinkPHP,认为其对 mysql 的封装调用方式是非常友好和易用的,因此绝大部分api参考其中。如果你有其他的意见和建议也希望可以跟我分享。

安装:

npm install mysqls --save-dev复制代码

mysqls参数说明

  • init: sql初始化API
  • exec: 执行sql语句

  • sql: 链式调用生成sql语句,支持生成后直接执行sql语句

  • transaction: 执行事务API

项目使用:

//import方式
import { init, exec, sql, transaction } from 'mysqls'

//require方式
let { init, exec, sql, transaction } = require('mysqls')复制代码

mysql配置初始化:

// 可在项目的启动时初始化配置
init({
    host: 'localhost',
    user: 'root',
    password:'123456',
    database: 'test',
    port: 3306,
})复制代码

init 参数说明

  • ispool: 是否以连接池的方式初始化 (default:true)
  • host: host地址 (default:'127.0.0.1')
  • user: 用户名 (default:'root')
  • password: 数据库密码 (default:'root')
  • database: 使用的数据库 (default:'test')
  • port: 端口 (default:'3306')
  • waitConnection: 是否等待链接(连接池时使用) (default:true)
  • connectionLimit: 连接池大小 (default:10)
  • queueLimit: 排队限制 (default:0)

只生成sql语句

sql
    .table('node_table')
    .field('id,name')
    .where({id:1})
    .select()

// result
SELECT id,name FROM node_table WHERE id=1复制代码

使用exec函数执行sql语句

const sqlstr = sql
    .table('node_table')
    .field('id,name')
    .where({id:1})
    .select();

const result = await exec(sqlstr);复制代码

使用sql.prototype.exec链式调用

const result = sql
    .table('node_table')
    .field('id,name')
    .where({id:1})
    .select(true)
    .exec();复制代码
  • 链式调用执行sql时select方法需要传参数:true
  • 同样适合update(true),insert(true),delet(true),query(true)方法

使用Promise方式

//使用 exec 函数
exec(sql.table('web_pages').where({id:147}).select())
    .then(res=>{
        console.log(res)
    }).catch(err=>{
        console.log(err)
    })

// 使用 exec 方法
sql.table('web_pages').where({id:147}).select(true).exec()
    .then(res=>{
        console.log(res)
    }).catch(err=>{
        console.log(err)
    })复制代码

使用async/await

//使用 exec 函数
const result = await exec(sql.table('web_pages').where({id:147}).select())

// 使用 exec 方法
const result = await sql.table('web_pages').where({id:147}).select(true).exec()复制代码

处理事务

const tranSqlArr = [
    sql.table('table1').data({number:'number-5'}).update(),
    sql.table('table2').data({number:'number+5'}).update()
]
const result = await transaction(tranSqlArr)复制代码

生成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语句高级用法

//参数json多字段
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%`))) 


//UNION , UNION ALL 组合使用
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)复制代码

更多用法请查看详细文档:

github.com/wangweiange…


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Visual LISP程序设计

Visual LISP程序设计

李学志 / 清华大学 / 2006-5 / 29.00元

本书系统地介绍了AutoCAD最新版本(2006)的Visual LISP程序设计技术。全书共分13章。前3章介绍AutoLISP语言的基础知识,第4章介绍Visual LISP的开发环境,第5~7章介绍程序的编辑、调试和设计的方法与技巧,第8章介绍如何定义新的AutoCAD命令及创建图层、线型、文字样式、剖面线、尺寸标注等各种AutoCAD对象,以及如何实现参数化图形设计的方法和技术,第9章介绍......一起来看看 《Visual LISP程序设计》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

MD5 加密
MD5 加密

MD5 加密工具