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

mysql-composer

v2.0.3

Published

A simple nodejs mysql syntax composer for insert and update syntax.

Downloads

448

Readme

Table of Contents

Introduction

One of the duty of my job is to write web scraper -- scrape data and store to mysql tables. And most of the time, the origin tables are not created by myself. It's inefficient to model these tables like sequelize do. All I want is having direct access to the tables and being efficient. That's why I develop mysql-composer.

If you are looking for a more powerful tool, sequelize may be a better choice.

Installation

npm i -S mysql-composer

Apis

mysql-composer is a simple tool for you to execute sql on mysql database.

const Composer = require('mysql-composer')
const mysql = require('mysql')
const connection = mysql.createConnection({
      host     : 'localhost',
      user     : 'me',
      password : 'secret',
      database : 'my_db'
    });
const composer = new Composer(connection)

insert

insert(config,callback,inspect)

  • config:
{
   table:'tableName', //require
   data:{ //require
         field1:'value1',
         field2:'value2'
       },
   ignore:true,//optional,defaults to true
   onDuplicateKeyUpdate:{field1:'value1',
   field2:function(){
        return 'field1+1'
    }},//optional 
}
  • callback: will be called when sql is executed: callback(err,result)
  • inspect: will be called with generated sql : inspect(sql)

example:

//insert ignore into user (`name`,`age`) values ('Tom','18')
composer.insert({
 table:'user',
 data:{
  name:'Tom',
  age:'18'
  }
},function(err,result){
  //your code after sql is executed
},function(sql){
    console.log(sql)
})

update

update(config,callback,inspect)

  • config:

     {
        table:'tableName', //require
        data:{ //require
              field1:'value1',
              field2:function(){
                   return 'field2+1'
               }
            },
        where:'id=1' //require,if you need to update all, you should explicitly set 'where' to 1.
     }
  • callback: will be called when sql is executed: callback(err,result)

  • inspect: will be called with generated sql : inspect(sql)

example:

//update `user` set age='18' where id=1
composer.update({
 table:'user',
 data:{
  age:'18'
  },
 where:"id=1"
},function(err,result){
  //your code after sql is executed
},function(sql){
    console.log(sql)
})

query

query(sql,callback,inspect)

  • sql: a valid sql syntax
  • callback: will be called when sql is executed: callback(err,result)
  • inspect: will be called with generated sql : inspect(sql)

example:

//update `user` set age='18' where id=1
composer.query('select * from user where id=1',function(err,result){
  //your code after sql is executed
},function(sql){
    console.log(sql)
})

beginTransaction

beginTransaction(callback)

  • callback: will be called when beginTransaction is executed: callback(err)

example:

composer.beginTransaction(function(err){
    if(err)throw err
    composer.insert({
     table:'user',
     data:{
      name:'Tom',
      age:'18'
      }
    },function(err,result){
      if(err)return composer.rollback(function(){
        throw err
      })

      composer.commit(function(err){
         if(err)return composer.rollback(function(){
            throw err
         })

         console.log('success')
      })
    })
})

rollback

rollback(callback)

  • callback: will be called when rollback is executed: callback(err)

commit

commit(callback)

  • callback: will be called when commit is executed: callback(err)

Promise support

mysql-composer supports promise, it will be convenient if you like to use mysql-composer with co or async/await. if you do not provide a callback in the apis above, they will return a promise:

var promise = composer.update({
            table:'name',
            where:'id=2',
            data:{
                field1:'value1',
                field2:'value2'
            }
        })
promise.then(function(result){

}).catch(function(err){

})