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-async-wrapper

v2.0.1

Published

This is a Wrapper class, which helps to get rid of callbacks of mysql package functions and provides a way to use them in async await (es7) syntax, Below Examples uses express framework in both (import/export syntax and commonJs syntax)

Downloads

79

Readme

Async Mysql Wrapper

This is a Wrapper class, which helps to get rid of callbacks of mysql package functions and provides a way to use them in async await (es7) syntax, Below Examples uses express framework in both (import/export syntax and commonJs syntax)

It also supports retry query execution for provided error codes

Import BaseDatabase class from mysql-async-wrapper, create a db instance with pool and export it

database.js (import/export syntax)

import mysql from "mysql"
import BaseDatabase from "mysql-async-wrapper"

const pool = mysql.createPool({
    //pool configuration
})

const db = new BaseDatabase(pool);
export default db;

database.js (commonJs Syntax)

const mysql = require("mysql");
const BaseDatabase = require("mysql-async-wrapper").default; // you need to add default (it's a typescript compiler issue)

const pool = mysql.createPool({
    //pool configuration
})

const db = new BaseDatabase(pool);
module.exports = db;

to retry query execution in case of error pass configuration object while creating db instance like below


const maxRetryCount = 3; // Number of Times To Retry
const retryErrorCodes = ["ER_LOCK_DEADLOCK", "ERR_LOCK_WAIT_TIMEOUT"] // Retry On which Error Codes 

const db = new BaseDatabase(pool, {
    maxRetrCount,
    retryErrorCodes
})

now in api controllers ( route handlers )

import db from "database.js"; // const db = require("database.js") in case of commonJS

async function controller(req, res, next){
    try{

        const connetion = await db.getConnection();
        
        const empQuery = `Select * from Employees`;
        const empResult = await connection.executeQuery(empQuery, []);

        const deptQuery = `Select * from Departments`;
        const deptResult = await connection.executeQuery(deptQuery, []);

    }catch(err){
        next(err); 
    }finally{
        db.close(); // To Release Connection
    }
}
  • To begin transaction pass transaction true in options while calling getConnection
  • Incase of error during query executing and connection is in transaction then it will automatically get rollback
  • But To rollback transaction in case of errors other than query errors please use rollback in catch block
import db from "database.js";

async function controller(req, res, next){
    try{

        const connection = await db.getConnection({ transaction: true }); // Will Begin Transaction
        
        const empQuery = `Insert into Employees (EmpID, Name) values (?,?)`;
        const empResult = await connection.executeQuery(empQuery, ["E02", "Abhay"]); // Incase of error auto rollback of transaction will be done

        const deptQuery = `Insert into Departments (DeptID, EmpID) values (?,?)`;
        const deptResult = await connection.executeQuery(deptQuery, ["D01", "E02"]); 

        await db.commit();
    }catch(err){
        db.rollback(); // to rollback in case of errors other than query error
        next(err); 
    }finally{
        db.close(); 
    }
}

If Required Transaction can be begin using beginTransaction

async function controller(req, res, next){
    try{

        const connection = await db.getConnection(); 

        const getEmpQuery = `Select EmpID, Name from Employees where EmpID = ?`;
        const getEmpResult = await connection.executeQuery(empQuery, ["E01"]); 


        await db.beginTransaction(); // Will Begin Transaction
        
        const empQuery = `Insert into Employees (EmpID, Name) values (?,?)`;
        const empResult = await connection.executeQuery(empQuery, ["E02", "Abhay"]); 

        const deptQuery = `Insert into Departments (DeptID, EmpID) values (?,?)`;
        const deptResult = await connection.executeQuery(deptQuery, ["D01", "E02"]); 

        await db.commit();
    }catch(err){
        db.rollback();
        next(err); 
    }finally{
        db.close(); 
    }
}

you can also use retry for specific error codes for particular query only


const empQuery = `Insert into Employees (EmpID, Name) values (?,?)`;
const empResult = await connection.executeQuery(empQuery, ["E02", "Abhay"], ["ER_LOCK_DEADLOCK"]); // by simply passing array of error codes as 3 parameter of execute query