JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 454
  • Score
    100M100P100Q90132F
  • License MIT

Unified db management

Package Exports

  • node-db-connector

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (node-db-connector) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

node-db-connector

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

It uses the following drivers:
Mongo: Mongoose or native driver
Mysql: promise-mysql
Postgresql: pg-promise
Redis: promise-redis

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')
    db.init(configs, {})
    .then(() => {
      console.log('DBs connections OK')
      return db.close()
    })
    .then(() => {
      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:
connectionString string: the connection string to connect to the DB.
name string or Array of 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.

Examples:

    {
       connectionString: 'mongodb://user:pwd@192.168.6.9:27017/dashboard',
       mongoose: true
    },
    {
      name: 'wtb-dev',
      connectionString: 'mongodb://user:pwd@localhost:27017/wtb'
    },
    {
      name: ['wtb', 'catalog'],
      connectionString: 'mongodb://user:pwd@192.168.6.9:27017/wtb'
    },
    {
      name: 'b2b_data',
      connectionString: 'postgresql://user:pwd@192.168.6.9:1024/b2bdata'
    },
    {
      name: 'cms',
      connectionString: 'mysql://user:pwd@192.168.6.9: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

    var db = require('node-db-connector'),
        configs = [{
          name: ['wtb', 'catalog'],
          connectionString: 'mongodb://user:pwd@192.168.6.9:27017/wtb'
        }]
    db.init(configs, {})
    .then(() => {
      console.log('DBs connections OK')
      return db.wtb.collection('users').find().toArray()
    .then((users)=>{
      return db.catalog.collection('products').findOne({_id: 42})
    })
    .then((doc)=>{
      return db.close()
    })
    .then(() => {
      console.log('All DBs closed')
    })
    .catch((err) => {
      console.error('Something horrible happened: ' + err)
    })

Tests

npm test or mocha tests to run all tests. For them to run, you need a mongodb instance on localhost:27017 (no authentication). The instance must contain a DB named wtb which itself must have a non-empty collection coin.