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

db-util-redoleus

v1.0.53

Published

This is a node module that provides database connectivity tools to be used with Typescript. Currently it provides an abstraction layer over the **node-mysql** module and allows for (yet) very basic queries. <br> All of the public methods that are asynchro

Downloads

26

Readme

General

This is a node module that provides database connectivity tools to be used with Typescript. Currently it provides an abstraction layer over the node-mysql module and allows for (yet) very basic queries. All of the public methods that are asynchronous, return Promises so that they can also used with the async/await functionality - thus avoiding the infamous 'callback hell' of nested callbacks. It is published on npm where it can be installed from. It is currently a public package accessible for everyone.

Usage

Setup a .env file

Firstly you will need to setup a .env file in your projects root directory containing the following properties:

ACTIVE_DB=DEV_LOCAL_DB_HOST

DEV_LOCAL_DB_HOST=[db-host]
DEV_LOCAL_DATABASE=[db-name]
DEV_LOCAL_DB_USER=[db-user]
DEV_LOCAL_DB_PASSWORD=[db-password]

DEV_AWS_DB_HOST=[db-host]
DEV_AWS_DATABASE=[db-name]
DEV_AWS_DB_USER=[db-user]
DEV_AWS_DB_PASSWORD=[db-password]

PROD_AWS_DB_HOST=[db-host]
PROD_AWS_DATABASE=[db-name]
PROD_AWS_DB_USER=[db-user]
PROD_AWS_DB_PASSWORD=[db-password]

The idea is to have multiple database credentials, one for each evironment that your code runs on. E.g. a localhost dev environment & a cloud production/dev environment. Simply change the ACTIVE_DB property to the DB_HOST you want to use - see example above.

Usage

First of all import the module via the command:

import { MySqlUtils } from 'db-util-redoleus';

The module contains the following public methods:

  • public static getInstance(): Returns the singleton instance of the MySqlUtils class. E.g.: const sql = MySqlUtils.getInstance();
  • public async testConnection(): Promise<boolean> : Returns a boolean value indicating whether a successful connection was made to the database server. Can be used for server health-checks from load-balancers, etc, E.g.:
const healthStatus = await sql.testConnection();
if (healthStatus) {
    // return a status 200 
} else {
    // throw an error or return a status 500 
}
  • public executeQuery(queryString: string, queryParams?: any): Promise<any>: Used for simple queries such as simple SELECT queries where opening a transaction for multiple table inserts/updates is not required. E.g.:
const sqlString = 'SELECT * FROM users_table where user_id = ?';
const dbResults = sql.executeQuery(sqlString, [userId]);
  • public getConnectionFromPool(): Promise<mysql.Connection>: Used for more complicated queries where opening a transaction is required in order to be able to rollback in case of a multi-part query failing halfway through. For more information see the mysql node module documentation.
const connection = sql.getConnectionFromPool();
connection.beginTransaction(function(err) {
  if (err) { throw err; }
  connection.query('INSERT INTO user_table SET name=?', "mrfksiv", function(err, result) {
    if (err) { 
      connection.rollback(function() {
        throw err;
      });
    }
    const log = result.insertId;
     
    connection.query('INSERT INTO log SET logid=?', log, function(err, result) {
      if (err) { 
        connection.rollback(function() {
          throw err;
        });
      }  
      connection.commit(function(err) {
        if (err) { 
          connection.rollback(function() {
            throw err;
          });
        }
        console.log('Transaction Completed successfully.');
        connection.end();
      });
    });
  });
});