Nightmarish Pawnshop Mystic
Unleash awesomeness. Private packages, team management tools, and powerful integrations. Get started with npm Orgs »

mysql-abstraction

4.2.0 • Public • Published

Build Status Coverage Status Dependencies

This is an abstraction layer built on https://github.com/felixge/node-mysql it add's various helper methods and helps deal with transactions.

Example usage:

var Connection, mysql, q;
 
mysql = require('mysql-abstraction')({
  user: process.env.MYSQL_USER,
  host: process.env.MYSQL_HOST,
  password: process.env.MYSQL_PASSWORD,
  connectionLimit: 100
});
 
Connection = mysql.connection;
 
//select then update using a transaction
= new Connection(true);
q.q({ q:'SELECT something FROM table WHERE id=?',params:[1],lock:1,cb: function(err,data){
    //do something with data
    q.q( { q:'UPDATE table SET something=? WHERE id=?',params:['something else',1],function(){
        q.end()
    }})
} })
 
//count the number of rows
= new Connection();
q.count({ q:'SELECT count(*) FROM table',cb: function(err,data){
    //do something with data
    console.log(data);
} })
 
//fetch the first row
= new Connection();
q.row({ q:'SELECT something FROM table WHERE id=1',cb: function(err,data){
    //do something with data
    console.log(data);
} })
 

See tests/query.coffee for more usage examples

Note when deadlocks are automatically rolled back and queries are reissued any autoincrement columns aren't reverted, see http://stackoverflow.com/questions/14758625/mysql-auto-increment-columns-on-transaction-commit-and-rollback for more details

There is a crude stats collection via the connection.stats prameter which counts the number of select/update/delete/insert queries per connection, you can disable stats collection by setting connection.gatherStats to false

Keywords

install

npm i mysql-abstraction

Downloadsweekly downloads

35

version

4.2.0

license

LGPL-3.0

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability