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

multysql

v1.0.2

Published

Wrapper for lazy handling multiple mysql dbs

Downloads

9

Readme

MultySQL

Build Status npm version MIT license

A lazy loader for managing connection pools to multiple MySQL databases

This module is based on node-promise-mysql module by lukeb-uk which itself is a wrapper for MySQL client that uses Bluebird promises. In summary, you can use this module to handle connections with multiple MySQL databases. This module is intended for the situation where you migth need to create connections to new databases in runtime and can't be sure to connect everywhere when starting the app. Every new database will be lazily connected, that means, when requested for the first use. And it will always be connected using the generic connection configuration passed to DbHandler at it's initialization, the only change is the database

Installation

To install multysql, just use npm or any other similar package manager for javascript:

$ npm install multysql

Usage

In TypeScript

import { DbHandler, MySQL } from 'multysql';

const genericPoolConfig: MySQL.PoolConfig = {
    host: process.env.DB_HOST,
    user: process.env.DB_USER,
    password: process.env.DB_PWD
    // see node-promise-mysql module for more options
};

let dbs = new DbHandler(genericPoolConfig);

async function requestStuff() {
    // lazy connect the first database, whose name is passed as a string
    const firstPool: MySQL.Pool = dbs.get('firstDatabase');

    // it won't generate a new pool if you try to get it again
    const firstPoolDuplicate: MySQL.Pool = dbs.get('firstDatabase'); // firstPool === firstPoolDuplicate

    let firstDbUserRow = await firstPool.query('SELECT username FROM users ORDER BY username LIMIT 1;');
    let firstDbUsername = firstDbUserRow[0].username; // Annie123

    // now we connect to the second database
    const secondPool: MySQL.Pool = dbs.get('secondDatabase');

    let secondDbUserRow = await secondPool.query('SELECT username FROM users ORDER BY username LIMIT 1;');
    let secondDbUsername = firstDbUserRow[0].username; // Archie456

    // do more stuff with both dbs connected...
}

requestStuff().then(() => {
    // finally we close every pool in our application
    dbs.all().forEach((db) => {
        // this will iterate over firstPool and secondPool ending them
        db.end();
    })
});

In JavaScript

const multySQL = require('multysql');

const genericPoolConfig = {
    host: process.env.DB_HOST,
    user: process.env.DB_USER,
    password: process.env.DB_PWD
    // see node-promise-mysql module for more options
};

let dbs = new multySQL.DbHandler(genericPoolConfig);

async function requestStuff() {
    // lazy connect the first database, whose name is passed as a string
    const firstPool = dbs.get('firstDatabase');

    // it won't generate a new pool if you try to get it again
    const firstPoolDuplicate = dbs.get('firstDatabase'); // firstPool === firstPoolDuplicate

    let firstDbUserRow = await firstPool.query('SELECT username FROM users ORDER BY username LIMIT 1;');
    let firstDbUsername = firstDbUserRow[0].username; // Annie123

    // now we connect to the second database
    const secondPool = dbs.get('secondDatabase');

    let secondDbUserRow = await secondPool.query('SELECT username FROM users ORDER BY username LIMIT 1;');
    let secondDbUsername = firstDbUserRow[0].username; // Archie456

    // do more stuff with both or more dbs connected...
}

requestStuff().then(() => {
    // finally we close every pool in our application
    dbs.all().forEach((db) => {
        // this will iterate over firstPool and secondPool ending them
        db.end();
    })
});

Development

Build

To build from TypeScript code (located at ./lib directory) type:

 $ npm run build

And built javascript code will be inside ./dist directory

Tests

In order to run test, you must clone this repository, install dependencies, set the following environment variables and have, at least, two mysql databases set up, with similar user and password.

 $ git clone https://github.com/FedericoAmura/multysql.git
 $ cd multysql
 $ export DB_HOST=<mysqlHost> # default 'localhost'
 $ export DB_USER=<mysqlUser>
 $ export DB_PWD=<mysqlPassword>
 $ export DB_FIRST_DATABASE=<yourFirstDatabase> # default 'first'
 $ export DB_SECOND_DATABASE=<yourSecondDatabase> # default 'second'
 $ npm install
 $ npm test # run tests in TypeScript
 $ npm jstest # run tests in JavaScript

Contributing

In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.