mongopooler
v1.0.1
Published
MongoDB middleware to manage connections via a cleaner setup
Downloads
3
Readme
MongoPooler
NodeJS package to manage MongoDB connections
Installation
To install this package, navigate to your project root and run:
npm install --save mongopooler
Usage
Import the pooler wherever it needs to be used
let MongoPool = require('mongopooler');
Setup the pool in your main server file
var options = { numberOfRetries : 5, auto_reconnect: true, poolSize : 40, connectTimeoutMS: 500 };
var uri = "mongoURI"
MongoPool.createPool(uri, options);
Access the pool from wherever required, to make calls to your MongoDB
MongoPool.getInstance(function(client){
// Perform DB operations here
})
Notes:
- The getInstance function returns a MongoDB client object, you still have to call client.db on it to pick a database
- getInstance will fail if it's called before createPool() is
Misc
This package is inspired by Yaki Klein's answer on this StackOverflow post.