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'
}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.
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)
})