0.0.8 • Published 6 years ago

async-mysql-query v0.0.8

Weekly downloads
2
License
ISC
Repository
github
Last release
6 years ago

测试版本,尚不稳定

后续会加上 mysql按配置连接 为了测试 现在写死了!!!
直接通过传递配置项,生成实例,连接直接变成一个实例属性

Release Note

  version:0.0.2

  新增format和now实例方法
  新增cipher和hash实例方法
  version 0.0.2

  增加 raw_select(sql,arr)实例方法
  废弃 selectByParams 和 selectByPage实例方法

  新增直接生成带有连接的实例
  let conf1 ={
    host:your host,
    user:your username,
    password:your pwd,
    database:your chosed database
    ......
  }
  let db2 .....

  const db = new database(conf1)
  const db2 = new database(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 = new database(conf)

  async test_end(){
    let some = await db.somequery()
    //返回查询后再终结
    db.endPool()
    db.showPool()
  }

  test_end()

Install

npm install async-mysql-query

Introduction

引入

  const database = require('async-mysql-query')
  const db = new database()
  let obj = {
    id:'1234',
    hasGirlFriend:'yes',
    name:'joker'
  }
  //所有db实例的方法均会返回一个promise实例
  db.insert('user_info',obj)
  .then(t=>console.log(t))
  .catch(e=>console.log(e))

使用NodeJs8.0util.promisifyasync await 解决node-mysql模块的进行数据库操作时陷入callback hell

Async MySQL

insert

使用方法如下

//插入数据
db.raw_insert([arr])
usage:
    db.raw_insert(['user_info',['id','name','hasGirlFriend'],['1234','joker','yes']])
    .then(t=>console.log(t))
    .catch(err=>console.log(err))
execude as:
    let sql =
    `insert into user_info (id,name,hasGirlFriend) values ('1234','joker','1234')`

    pool.query(sql,(e,r)=>console.log(r))

db.insert(tableName,opt)
usage:
    let opt = {
      id:'1234',
      name:'joker',
      hasGirlFriend:'yes'
    }
    db.insert('user_info',opt)
    .then(t=>console.log(t))
    .catch(e=>console.log(e))

实例方法

方法参数说明
db.raw_insertarr
db.inserttableName,opt

select

db.selectByParams(sql,[arr])

usage:
    let sql =
    `select ?? from ?? where name = ? and hasGirlFriend = ? `
    let opt = [['id','name','age','hasGirlFriend'],'user_info','joker','nope']
    db.selectByParams(sql,opt)
    .then((t)=>console.log(t))
    .catch(e=>console.log(e))


db.selectByPages(sql,[arr])
//此方法可与上述方法合并

usage:
    let sql = `select ?? from ?? where name = ? and hasGirlFriend = ? limit ?,?`
    //此类分页方法扫描数据表时过慢  TODO  
    let opt = [['id','name','age','hasGirlFriend'],'user_info','joker','nope',0,5]
    db.selectByPages(sql,opt)
    .then(t=>console.log(t))
    .catch(e=>console.log(e))

db.countTable(tableName)

usage:

    db.countTable('user_info')
    .then(t=>console.log(t))
    .catch(e=>console.log(e))

实例方法

方法名参数说明
db.selectByParams sql,opt废弃
db.selectByPagessql,opt废弃
db.raw_select(sql,arr)sql,arr替代以上两方法
db.countTabletableName

update

  db.update(arr)
  //arr = [tableName,obj,params]
  //适用一个过滤条件
  usage:
      let opt = {
        name:'joker',
        hasGirlFriend:'no'
      }
      let params = {
        id:'12345'
      }
      db.update('user_info',opt,params)
      .then(t=>console.log(t))
      .catch(e=>console.log(e))

  db.raw_update(sql,opt)
  //自定义更新语句 适用多个过滤条件的
  usage:
      let sql =
      `update ?? set ? where name = ? and hasGirlFriend = ?`
      let opt = {
        age:19,
        hasDream:'no'
      }
      let arr = ['user_info',opt,'joker','yes']
      db.update(sql,arr)
      .then(t=>console.log(t))
      .catch(e=>console.log(e))

delete

  db.delete(arr)
  usage:
      let arr = ["user_info",{name:'joker'}]
  execude as:
      sql = `delete from user_info where name = 'joker'`
      pool.query(sql,……)


  db.raw_delete(sql,arr)
  usage:
      let sql = `delete from ?? where name = ? and age = ? `
      let arr = ["user_info",'joker','22']
      db.delete(sql,arr)
      .then(t=>console.log(t))
      .catch(e=>console.log(e))
0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago