测试版本,尚不稳定
后续会加上 mysql按配置连接 为了测试 现在写死了!!!
直接通过传递配置项,生成实例,连接直接变成一个实例属性
Release Note
version:002 新增format和now实例方法 新增cipher和hash实例方法
version 002 增加 实例方法 废弃 selectByParams 和 selectByPage实例方法 新增直接生成带有连接的实例 let conf1 = host:your host user:your username password:your pwd database:your chosed database ...... let db2 ... const db = conf1 const db2 = conf2 //此时db1 和 db2 为两个不同的连接,都具有自己的实例方法,独立进行不同数据库间的操作 新增showPool和endPool实例方法,用于显示pool实例和关闭pool实例 注意(3遍) 官方说endPool会等到队列结束才会平滑关闭 实测会立即关闭和destroy效果差不多 官方原话: The end method takes an optional callback that you can use to know once all the connections have ended The connections end gracefully so all pending queries will still complete and the time to end the pool will vary //建议像下面这样使用 const db = conf async { let some = await db //返回查询后再终结 db db }
Install
npm install async-mysql-query
Introduction
引入
const database = const db = let obj = id:'1234' hasGirlFriend:'yes' name:'joker' //所有db实例的方法均会返回一个promise实例 db
使用NodeJs8.0
的util.promisify
和async await
解决node-mysql
模块的进行数据库操作时陷入callback hell
MySQL
语法和node-mysql
语法(比如其中的各种占位符)
Async MySQL
insert
使用方法如下
//插入数据dbusage: db execude as: let sql = `insert into user_info (id,name,hasGirlFriend) values ('1234','joker','1234')` pool dbusage: let opt = id:'1234' name:'joker' hasGirlFriend:'yes' db
实例方法
方法 | 参数 | 说明 |
---|---|---|
db.raw_insert | arr | |
db.insert | tableName,opt |
select
db usage: let sql = `select ?? from ?? where name = ? and hasGirlFriend = ? ` let opt = 'id''name''age''hasGirlFriend''user_info''joker''nope' db db//此方法可与上述方法合并 usage: let sql = `select ?? from ?? where name = ? and hasGirlFriend = ? limit ?,?` //此类分页方法扫描数据表时过慢 TODO let opt = 'id''name''age''hasGirlFriend''user_info''joker''nope'05 db db usage: db
实例方法
方法名 | 参数 | 说明 |
---|---|---|
sql,opt | 废弃 | |
sql,opt | 废弃 | |
db.raw_select(sql,arr) | sql,arr | 替代以上两方法 |
db.countTable | tableName |
update
db //arr = [tableName,obj,params] //适用一个过滤条件 usage: let opt = name:'joker' hasGirlFriend:'no' let params = id:'12345' db db //自定义更新语句 适用多个过滤条件的 usage: let sql = `update ?? set ? where name = ? and hasGirlFriend = ?` let opt = age:19 hasDream:'no' let arr = 'user_info'opt'joker''yes' db
delete
db usage: let arr = "user_info"name:'joker' execude as: sql = `delete from user_info where name = 'joker'` pool db usage: let sql = `delete from ?? where name = ? and age = ? ` let arr = "user_info"'joker''22' db