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 🙏

© 2026 – Pkg Stats / Ryan Hefner

dbconnectortoolkit

v0.2.0

Published

This module provides an integrated tool to connect database under different settings

Downloads

23

Readme

DBConnectorToolkit

This module provides an integrated tool to connect database under different settings, single db, master with read replica, with redis for caching, and allow using message queue to centralize db writes requests. The main purpose is to reduce code changes on application with growing traffic and upgrading in infra.

Documentation is available at https://dbconnectortoolkit.readthedocs.io.

We have created a demo project (DBConnectorSampleWeb) and a demo site (dbconnectorapi.authpaper.com) to illustrate how this library works.

Installation

# Include installation commands here
npm install dbconnectortoolkit

Sample use case

Suppose you have a postgres database (web) and would like to select data from it.

const dbConnector = require('dbconnectortoolkit').default;

const masterDBConfig = {
  client: 'pg',
  endpoint: 'localhost',
  port: 5432,
  username: 'your_username',
  password: 'your_password',
  database: 'web',
}
const dbConnector = dbConnector(masterDBConfig)
try {
    await dbConnector.connect()
    // Select all data from the users table
    const result = await dbConnector.select(
        [{ table: 'users' }],
        ['*']
    )
    console.log('Data from users table:', result)
    // also have insert, update, upsert, delete methods and query method for raw queries
} catch (err) {
    // throw when database cannot login or something wrong with the query
    console.error('Error:', err)
} finally {
    await dbConnector.disconnect()
}

Now suppose your application should only have 1-10 concurrent connections to database (so it will not blow up) and they are managed by a pool

const masterDBConfig2 = {
    ...masterDBConfig,
    minConnection: 1,
    maxConnection: 10
}

Done.

Now your application is getting great, your lead asks you to use replica database for read queries. Instead of rewriting lots of SQL query code to use slave DB client, all you need is

const replicaDBConfig = [
    {
    ...
    },...
]
db = dbConnector(masterDBConfig, replicaDBConfig);

All select queries will go to replica and write queries will go to master. No other code change is needed.

Now the traffic is even higher, redis is introduced as cache layer. How many code changes?

const cacheConfig = {
    client: 'ioredis',
    url: 'localhost@6379',
    dbIndex: 1, // database index, default is 0
    cacheHeader: 'dbCache:' // optional, default is dbCache:
}
db = dbConnector(masterDBConfig, replicaDBConfig, cacheConfig);

Done, no other code change is needed. All read query results will be cached in redis. The key in redis will be dbCache:${sha256 hash of the raw query}

If data should come from database instead of cache, set _getLatest to true.

Supporting databases, caches and message queues

Database clients supported: pg (PostgreSQL, using mainly pg pool), mariadb (MariaDB or MySQL, using mariadb pool), sqlite (SQLite, using sqlite3) Cache clients supported: ioredis (Redis), redis (Redis), nodecache (node-cache), memcached (memcached) Message queue clients supported: kafka (Kafka, using kafkajs)

TODO

Unit test cases Support of db transaction when message queue is used to centralize db writes.

Databases pending to support: MSSQL, Aurora DSQL, Oracle

Cache pending to support: Valkey

Message queue pending to support: Amazon MSK, rabbitMQ, Amazon Simple Queue Service SQS

Contributing

If you want to contribute to this project, please submit a pull request or create an issue for discussion.

Working on README / documentations are also welcomed.

FAQ / Troubleshooting / Support

Please open an issue on github.

Funding

You may buy me a coffee.

BTC: bc1qgl8g2xu3f60lkxgzg80jvykkmf3gywaky3c2tt

ETH / BNB / POL (pologon): 0xA5BC03ddc951966B0Df385653fA5b7CAdF1fc3DA