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

node-db-connector

v6.1.1

Published

Unified db management

Downloads

3,301

Readme

node-db-connector

Unified db connection mgmt: provides a simple way to connect to MongoDB, Mysql and Redis databases.

It uses the following drivers:
Mongo: Mongoose or native driver
Mysql: promise-mysql
Redis: promise-redis-legacy
PostgreSQL support was removed in version 3.0.0 as we do not use it anymore.

This package only handles connection & disconnect. Please refer to each driver's own documentation for how to query the DBs.

Usage

    var db = require('node-db-connector')

    try{      
      await db.init(configs, {})
      console.log('DBs connections OK')

      await db.close()
      console.log('All DBs closed')

    }
    catch(err){
      console.error('Something horrible happened: ' + err)
    }

API

init(configs, [options])

Connects to the DBs defined in configs. Returns a promise.
Each database is accessible on the node-db-connector object.

Parameters

configs
Type: Array of objects. Mandatory, no default.

Lists the databases to connect to. Each element is an object with the following properties:

name | type | mandatory | description -|-|-|- connectionString | string | ✓ | the connection string to connect to the DB.
name | string or string[] | | Name the database will be referenced after. If not provided for Mongo, the database is referenced after the db name provided in the connection string.For Mongo DBs, the property can be an array of strings. The first value will reference the main db (the one in the connection string). The other values must be the names of other databases the connection string gives access to.
secret | string | | Id of the AWS secret that stores credentials.

Secrets are expected to be valid JSON with username, password and (optionally) authdb. If a secret is used and provides all 3 fields, the connection string only needs to include the protocol, host and options.

Examples:

    {
       connectionString: 'mongodb://internal.prod-critical-db.com?readPreference=primary',
       secret: 'db-admin-sensitive-credentials'
    }
    {
       connectionString: 'mongodb://192.168.6.9:27017/wtb',
       secret: 'my-secret-with-credentials'
    },
    {
       connectionString: 'mongodb://user:[email protected]:27017/dashboard',
       mongoose: true
    },
    {
      name: 'wtb-dev',
      connectionString: 'mongodb://user:pwd@localhost:27017/wtb'
    },
    {
      name: ['wtb', 'catalog'],
      connectionString: 'mongodb://user:[email protected]:27017/wtb'
    },
    {
      name: 'cms',
      connectionString: 'mysql://user:[email protected]:3306/prd_cms'
    },
    {
      name: 'redis',
      connectionString: 'redis://192.168.6.9:6379'
    }

If you need to alias a DB, use the dbName:alias syntax, where dbName is the real name of the database, and alias is the name you want to use. This is useful to avoid conflicts when you need to connect to 2 different databases with the same name.

    {name: 'wtb:source', connectionString: 'mongodb://...'}

Allows you to use db.source to query the wtb database.

options
Type: object

Available options:

  • mongoose object: the mongoose instance. Mandatory if Mongoose is used for a connection.
  • logger object: a logger object. Must provide info and error methods. Default to console.
  • separator string: separator to specify an alias for db name. Default ':'

close()

Closes all connections.

Example

    const db = require('node-db-connector'),
          configs = [{
            name: ['wtb', 'catalog'],
            connectionString: 'mongodb://user:[email protected]:27017/wtb'
          }]

    try{      
      await db.init(configs, {})    
      console.log('DBs connections OK')

      const users = await db.wtb.collection('users').find().toArray()    
      const products = await db.catalog.collection('products').findOne({_id: 42})    

      await db.close()
      console.log('All DBs closed')
    }
    catch(err){
      console.error('Aargh :(\n' + err)
    }

Tests

npm test or mocha tests to run all tests. Connection strings must be in a file in tests/connectionStrings.json. The JSON object must be of the following form:

{
  "stg_admin": "mongodb://user:pwd@host:27017",
  "stg_admin_wrong_db": "mongodb://user:pwd@host:27017/wtb",
  "stg_local": "mongodb://user:pwd@host:27017/wtb",
  "stg_local_wrong_db": "mongodb://user:pwd@host:27017"
}