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 🙏

© 2025 – Pkg Stats / Ryan Hefner

sqlexec

v1.0.8

Published

Simplifies SQL command with MySQL

Readme

sqlexec

Sqlexec is library for running query MySQL Database with nodejs in the easy way.

The library will simplifies your coding syntax when write SQL command. You can write single query or transaction queries with a few code and readable syntax. Connection default is using pooling.

Install

npm install sqlexec

Dependencies

Setup Environment Variable

You can use enviroment variable (dot env file) to set database configuration with following parameters :

  1. MYSQL_HOST : host database (default : 127.0.0.1)
  2. MYSQL_USER : user database (default : root)
  3. MYSQL_PASSWORD : password database (default : empty)
  4. MYSQL_PORT: port database (default : 3306)
  5. MYSQL_DATABASE : database name (default : test)
  6. MYSQL_CONNECTIONLIMIT : connection limit for pooling (default : 10)
  7. MYSQL_TIMEZONE : time zone (default : 'Asia/Jakarta')

If you add variable node env (dot env file) with 'dev' mode, you can see log sql statement in terminal during development

NODE_ENV = dev

so you can trace your sql staement is right or show error if there is any wrong syntax.

Setup Connection

If you are not using dot env file you can set in your config.js file and call it in index.js/app.js :

const dbsql = require("sqlexec");

let dbconfig = {
    user: "root",
    password: "psw123",
    database: "dbtest",
    connectionLimit : 10,
    host: "localhost",
    port:  "3306",
    timezone:'Asia/Jakarta'
};
//create connection
let con=dbsql.connect(dbconfig);
//you can check connection object if needed
console.log(con)

If you are using dot env file :

const dbsql = require("sqlexec");

//empty object
let dbconfig = {};
//create connection
let con=dbsql.connect(dbconfig);
//you can check connection object if needed
console.log(con)

Singe SQL Command

This is sample single sql statement

const dbsql = require("sqlexec");

let dbconfig = {
    user: "root",
    password: "psw123",
    database: "dbtest",
    connectionLimit : 10,
    host: "localhost",
    port:  "3306",
    timezone:'Asia/Jakarta'
};
//create connection
dbsql.connect(dbconfig);


try {
    //select
    let result=await dbsql.sqlExec("SELECT name,city FROM customer WHERE name=?", ['David']);
    console.log(result)

 } catch(err){
    console.log(err)
 }

 try {
    //insert
    let sql="INSERT INTO customer(name,city) VALUES(?,?)";
    let param=['Andy','Jakarta'];
    await dbsql.sqlExec(sql,param);

 } catch(err) {
    console.log(err)
 }

Transaction SQL Command First Methode

This methode you will use session variable to link all sql statement, also you can retrieve some data that can be used to next query

const dbsql = require("sqlexec");

try {
    //create session to begin transaction
    let session = await dbsql.beginTrans();

    //statement one to retrieve data
    let sql1 = "SELECT name FROM customer WHERE id=?";
    let sqlResult= await dbsql.execTrans(session, sql1, [59]);
    console.log(sqlResult)

    //statement two, data retrieve will be inserted into other other table
    let sql2 = "INSERT INTO purchase(namecustomer) VALUES(?)";
    await dbsql.execTrans(session, sql2, [sqlResult[0].name]);

    //commit transaction
    await dbsql.commitTrans(session);
} catch(err){
    console.log(err)
}

if you will rollback transaction you can use

 await dbsql.rollbackTrans(session);

Transaction SQL Command Second Methode

This methode you are not using session, all sql statement will be set into array and execute all statement. This methode suitable for insert table with large data or delete data from many tables. For your information you cannot get return result using this methode.

const dbsql = require("sqlexec");

//insert table with many records
try{
    let sqls = [];

    sqls.push({query: "INSERT INTO customer(name,address) VALUES(?,?)",parameters:['Andy','New York']})
    sqls.push({query: "INSERT INTO customer(name,address) VALUES(?,?)",parameters:['Andrew','California']});

    await dbsql.sqlExecTrans(sqls);

} catch(err){
    console.log(err)
}

You can stick with methode one for using insert large data set and not using methode two, this is up to you.

Methods

  • connect(< object > configuration, return is connection object).

  • sqlExec(< string > sql statemtent, < array > value param), return is query result.

  • beginTrans() begin transaction and the return is session.

  • execTrans(session session from methode beginTrans, < string > sql statemnent, < array > value param), return is query result.

  • commitTrans() commit transaction and close session.

  • rollbackTrans() rollback transaction and close session.

  • sqlExecTrans(< object > {query:< string > sql statement, parameters:< array > value param }).