npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

shadow-mysql

v2.0.9

Published

Mysql interface package

Downloads

24

Readme

shadow-mysql

  • 对mysql进行了一层封装,mysql连接只是用连接池。
  • 添加了makeSQL,makeSQLSelect等简单方法。
  • 对conn没有正常release会打印日志。
  • 对于一个conn的事务是否开启多个做了错误判断。
  • 对于query,getConnection,beginTransaction,commit提供了promise方法,即在方法名后加Async(例如queryAsync)

##安装
npm install shadow-mysql

##使用说明 ###初始化

var mysql = require('shadow-mysql');
var options = {
    host: xx,
    user: xx,
    password: xx,
    database: xx,
    multipleStatements: 'true' //一次执行多条sql
}
var pool = new mysql.Pool(options);

###执行sql
提供两种query方式:

  • 连接池上直接query
pool.query(sql,function(err,rows,fields){
   ...
})
  • 连接池上获取连接,再query
//获取连接,connectionName为连接名,如若忘记归还连接,则会根据这个连接名提示
pool.getConnection(connectionName, function (err,conn) {
    conn.query(sql, function (err, data) {
        if(err) ...;
        //释放连接,若超过1min不归还,底层会提示
        conn.release();
    })
});

###事务处理

//connection需要通过pool.getConnection()获取
connection.beginTransaction(function(err) {
  if (err) { throw err; }
  connection.query('INSERT INTO posts SET title=?', title, function(err, result) {
    if (err) {
      return connection.rollback(function() {
        throw err;
      });
    }

    var log = 'Post ' + result.insertId + ' added';

    connection.query('INSERT INTO log SET data=?', log, function(err, result) {
      if (err) {
        return connection.rollback(function() {
          throw err;
        });
      }  
      connection.commit(function(err) {
        if (err) {
          return connection.rollback(function() {
            throw err;
          });
        }
        console.log('success!');
      });
    });
  });
});

###单表执行sql的简单方法(单表尽量用下面五种方法,解决了sql注入问题)

  • makeSQL
var mysql = require('shadow-mysql');
var sql = "select * from table where id = @id@";
sql = mysql.makeSQL(sql,{id:2});
  • makeSQLSelect
var sql = mysql.makeSQLSelect('hs_t',['id','name'],{id:3});
console.log(sql); //select id,name from hs_t where id = 3;
//makeSQLSelect 第4个参数为condition(object),代表除了=以外的条件查询
//暂时支持 lt:小于 gt:大于 lte:小于等于 gte:大于等于 like:模糊查询
var sql = mysql.makeSQLSelect('teams', ['*'], { id: 2 }, { a: { lt: 5, gt: 3 }, b: { like: 'tt' }, c: { lte: 2, gte: 6 } });
console.log(sql); //SELECT * FROM `teams` WHERE 1=1  AND id=2  AND a < 5 AND a > 3 AND b like '%tt%' AND c <= 2 AND c >= 6
  • makeSQLInsert
var sql = mysql.makeSQLInsert('hs_t',{id:3,name:'xc'});
console.log(sql);//insert into hs_t(id,name) values (3,'xc');
  • makeSQLUpdate
var sql = mysql.makeSQLUpdate('hs_t',{name:'xc'},{id:3});
console.log(sql);//update hs_t set name = 'xc' where id = 3;
//第4个参数同makeSQLSelect
  • makeSQLDelete
var sql = mysql.makeSQLDelete('hs_t',{name:'xc'});
console.log(sql);//delete from hs_t where name = 'xc';
//第3个参数同makeSQLSelect

###防sql注入
如果自己写sql时,所有参数都需要用mysql.escape(param)过滤下,防止sql注入