🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

cluster-db

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cluster-db

A wrapper for switching the master/slave connection information.

0.1.0
latest
Source
npm
Version published
Weekly downloads
5
Maintainers
1
Weekly downloads
 
Created
Source

cluster-db

version download

A wrapper for switching the master/slave connection information.

js-standard-style

Usage

var async = require('async')
var cluster = require('../index')
var database = require('./database.json')

var simpleQuery = function (sql, value, cb) {
  cluster.get('slave*', function (err, conn) {
    if (err) {
      return console.error(err)
    }

    conn.query(sql, value, function (err, rows) {
      conn.release()

      if (err) {
        console.log(sql, value)
        throw err
      }

      cb(err, rows)
    })
  })
}

// set event
cluster.on('remove', function (param) {
  console.info(param)
})

// load database
cluster.load('mysql', function (err) {
  if (err) {
    return console.error(err)
  }

  var tasks = []
  for (var i = 0; i < database.length; i++) {
    tasks.push(function (config) {
      return function (cb) {
        cluster.add(config.id, config, function (err) {
          if (err) {
            return cb(err)
          }

          cb(null, config.id)
        })
      }
    }(database[i]))
  }

  async.parallel(tasks, function (err, result) {
    if (err) {
      return console.error(err)
    }

    console.log('load cluster complete.', result)

    simpleQuery('SELECT * FROM users LIMIT 1', null, function (err, rows) {
      if (err) {
        return console.error(err)
      }

      console.log(rows)
    })
  })
})
// database.json
[
  {
    "id": "master",
    "host": "host",
    "user": "username",
    "password": "password",
    "database": "database"
  },
  {
    "id": "slave1",
    "host": "host",
    "user": "username",
    "password": "password",
    "database": "database"
  },
  {
    "id": "slave2",
    "host": "host",
    "user": "username",
    "password": "password",
    "database": "database"
  },
  {
    "id": "stats1",
    "host": "host",
    "user": "username",
    "password": "password",
    "database": "database"
  },
  {
    "id": "stats2",
    "host": "host",
    "user": "username",
    "password": "password",
    "database": "database"
  }
]

Todo

  • Support multiple database type.

Documentation

See the documentation

Release History

See the changelog

LICENSE

cluster-db is licensed under the MIT license.

Keywords

cluster

FAQs

Package last updated on 27 Nov 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts