Code Monkey home page Code Monkey logo

node-db-connector's Introduction

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:[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: 'b2b_data',
      connectionString: 'postgresql://user:[email protected]:1024/b2bdata'
    },
    {
      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

    var db = require('node-db-connector'),
        configs = [{
          name: ['wtb', 'catalog'],
          connectionString: 'mongodb://user:[email protected]: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.

Development setup

To run a development version of an npm module, you need to link it:

cd ../node-db-connector # go to the repository directory
npm link

cd .../dev/wtb-backoffice # go to the repository that needs the development version
npm link node-db-connector

See https://docs.npmjs.com/cli/link for more details.

To remove the development-version link:

# in project file
npm unlink node-db-connector

# in node-db-connector folder
npm unlink

Publishing

  • update the package version number in package.json. It uses semantic versionning. Semantic versionning uses 3 numbers to represent a version, e.g. 2.0.1:
    • Major: increment when the new version contains breaking changes.
    • Minor: increment when the new version adds backward-compatible changes.
    • Patch: increment when the new version makes backward-compatible bugfixes.

Incrementing a number means resetting subsequent numbers to 0, e.g. 2.1.7 -> 3.0.0, or 2.0.5 -> 2.1.0. More details at https://semver.org/

  • Make sure you are authenticated:
npm login

Then enter swaven login & credentials.

  • Publish the package:
npm publish

node-db-connector's People

Contributors

antoinegoutagny avatar clementmouret avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.