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-utils-js

v1.0.5

Published

A promise-based implemenation, designed for utilizing mysql easly

Downloads

24

Readme

mysql-utils-js

MySQL utility functions which will make your code easier to fetch and write data to database. This supports MySQL transaction in a cleaner way.

NPM JavaScript Style Guide

Install

npm i --save mysql-utils-js

Usage

Sample code for performing single query

const sqlutils = require('mysql-utils-js');

const config = {
    host: 'localhost',
    user: 'username',
    password: 'pwd',
    database: 'db1',
    connectionLimit: 5
};

// A generic callback which contains three parameters [err, data, msg]
const functioncallback = (err, data, msg) {
    console.log({err, data, msg})
}
const query = "SELECT * FROM table1 WHERE id = ? and name = ?" // sample query
const params = [1, 'name_1']; // sample parameter for above query

sqlutils.createConnectionPool(config);
sqlutils.connectAndRun(functioncallback, 
    sqlutils.queryErrSucc(query, params, 
        (err) => {
            functioncallback(err, null, 'error happened'); 
        }, (data) => {
            functioncallback(null, data, 'success');
        }
    )
);

Sample code for performing query transaction.

const sqlutils = require('mysql-utils-js');

const config = {
    host: 'localhost',
    user: 'username',
    password: 'pwd',
    database: 'db1',
    connectionLimit: 5
};

sqlutils.createConnectionPool(config);

let queryList = [
    ['queryid1',`SELECT id FROM users WHERE name = ?`,
    _ => ['name1'],                                         // passing param value for query number 1
        err => [true, err, 'Some error message'],           // this will be called if any error happened while performing query
        (_resAll, res) => (res && res.affectedRows > 0) ?   // _ressAll contains result of all query and res contains current query result
            [false, res, 'Some success message']:           // returning success
            [true, res, 'Some failure message']             // returning failure
    ],
    ['updateinfoquery',`DELETE FROM users WHERE id = ?`,
        (results) => {
            let useid = results['queryid1'][1].insertId;    // taking userid from previous query result (from queryid1 result)
            return [userid]                                 // passing param value for query number 2
        },
        err => [true, err, 'Some error message'],           // this will be called if any error happened while performing query
        (_resAll, res) => (res && res.affectedRows > 0) ?   // _ressAll contains result of all query and res contains current query result
            [false, res, 'Some success message']:           // returning sucess
            [true, res, 'Some error message']               // returning failure
    ]
];

// This is a function which will run the list of queries in TRANSACTION
// THIS IS A TRANSACTION, Queries can fail independently, so even if one query fails the whole sequence of queries will be rolled back
// Input: callbackinp, listofqueries
// This function calls connection.query for all the queries in the listofqueries in sequence and returns the results through the callback
// list of queries format => [[queryid, querysql, queryarg, failfunc, succfunc],...] queryid => the results of this particular query will be under queryid key
// failfunc => will be called to check if we get error object in the query callback, and then we check if it actually an error
// succfunc => will be called when mysql error obj is NULL, this can also fail the transaction if it returns [true,. ,.]
// The above function should return [shouldifailtransaction, result if succ else errobj, succmsg if succ else errmsg]
// If ALL SUCCESS, callback will be called with (null, resultobj, succmsg); resultobj => {'queryid1':[resultforthisquery', 'queryid2':[]}
// Even if one FAILS, callback will be called with ({'errs':errobj}, null, failmsg) and the transaction would have been rolled back
sqlutils.getMysqlConnAndRunTran((err, data, msg) => {
    // handle all query result here
}, queryList);

License

ISC © hanjas