mysql-transactions

Use SQL transactions with node-mysql

npm install mysql

npm install mysql-transactions

var transactions = require('mysql-transactions')({
    user: 'hamburglar',
    password: 'omnomnomnomnomnom',
    database: 'playhouse'
});
 
 
 
transactions.begin();
 
 
transactions.query('INSERT INTO users SET ?', { name: 'Lord Voldemort' }, function(errresult) {
 
    if (err) {
        transactions.rollback();
        return console.log('Rolled back.');
    }
 
 
    transactions.commit(function(errresult) {
        console.log('Committed.');
    });
 
});

Note: After calling .begin(), .commit() or .rollback() must be called so that the connection is closed.

Each [fn] is a callback for a mysql.query.

  • begin([fn]): start the transaction process.
  • query(String [, Object/Array] [, fn]): traditional query.
  • rollback([fn]): rollback the transaction.
  • commit([fn]): commit the transaction.