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

@doctor_internet/interdb

v3.0.0

Published

Handler for multiple named SQL connection pools.

Downloads

25

Readme

InterDB

Handler for multiple named SQL connection pools.

Handling multiple MySQL connection pools to different servers can be a hassle, depending on the implementation. InterDB aims to reduce the workload required to implement that.

Usage

let Handler = require('@doctor_internet/interdb').ListDBHandler
let db = new Handler({
    live: {
        host: "db.example.com",
        user: "node-service-worker",
        password: "amazingpassword",
        database: "jobs"
    },
    backup: {
        host: "backup.example.com",
        user: "node-backup-service-worker",
        password: "evenbetterpassword",
        database: "backups"
    }
})
db.get("live").query("SELECT * FROM complete").then(console.log)

with this example fetching completed jobs from the live db. Unfortunately, InterDB only supports MySQL2 promise based connection pools, as opposed to callback based pools or other connection drivers.

There's also a class which automatically pulls connection details from the environment files, assuming interenv is installed.

DB_AUTO=API,LOGS
DB_HOST=db.example.com
DB_USER=exampleuser
DB_PASS=goodpassword
DB_API_DB=api
DB_LOGS_DB=prod_logs
DB_LOGS_PASS=anothergreatpassword
let Handler = require('@doctor_internet/interdb').EnvDBHandler
let db = new Handler(process.env)
console.log(db.configs)

outputs

{
  default: {
    host: 'db.example.com',
    user: 'exampleuser',
    port: 3306,
    password: 'goodpassword'
  },
  api: {
    host: 'db.example.com',
    user: 'exampleuser',
    port: 3306,
    password: 'goodpassword',
    database: 'api'
  },
  logs: {
    host: 'db.example.com',
    user: 'exampleuser',
    port: 3306,
    password: 'anothergreatpassword',
    database: 'prod_logs'
  }
}

Default Values

No default values are provided for the ListDBHandler, each config is passed to the connection pool as-is.

For the EnvDBHandler, a default host, port and user are provided (localhost, 3306 and root respectively). Un-namespaced variables are used first, and placed in the "default" key. (ex: DB_HOST -> default.host)

Names in the DB_AUTO list then expanded (comma separated). Each name is lowercase'd, and used as the config key. (API -> api) Namespaced DB vars are then found and added (DB_API_DB -> api.database) Then any vars in the default host which are not in the database are found and merged (DB_HOST -> default.host -> api.host) Finally, any null or empty string values are unset.

Value Mappings

DB_HOST -> host DB_USER -> user DB_PORT -> port DB_PASS -> password DB_DB -> database

API

const InterDB = require('@doctor_internet/interdb')
const Handler = InterDB.EnvDBHandler

let DB = new Handler()
console.log(DB.has("default")) // boolean: true
console.log(DB.has("some_key_that_doesnt_exist")) // boolean: false
console.log(DB.get("default")) // MySQL2 Promise Connection Pool.

Install

With npm installed, run

$ npm install @doctor_internet/interdb

See Also

License

MIT