express-mongoose-db v0.1.0
express-mongoose-db
Connect once and memorize connection for next usages
Usage
First - install middleware in project:
npm i express-mongoose-db --save
Second - add middleware to express application:
var app = require('express')();
app.use(require('express-mongoose-db')());Now you can access mongoose.connection object of mongoose module in any following middleware or route handler in express.
API
express-mongoose-db(options)
Options
You can pass options to constructor of middleware function like this: require('express-mongoose-db')(options) where options is an object with fields described below.
Also you can modify defaults value in config property of middleware contructor like this:
var mongoosedb = require('express-mongoose-db');
mongoosedb.config.readPreference = 'secondary';
var app = require('express')();
app.use(mongoosedb());
app.get('/', function(req, res) {
var connection = require('mongoose').connection;
});host- server or replica string (default:localhost, but can beserver.one.com:123,server.two.com:456)db- name of database (default:test)options- object, that passed to mongoose.connect.- And all options from connect-once, such as
reconnectWaitandheartbeatfunction.
Events
To know what's up in your life, we provide event-emitter to listen to. For example - this is how you know, that reconnecton happening:
var mongoosedb = require('express-mongoose-db')(options);
mongoosedb.connection.on('reconnect', function(err) {
console.log("Reconnecting to mongo (" + this.retries + " retries left). " + (err.stack ? err.stack : err));
});Also you can subscribe on connection event:
var mongoosedb = require('express-mongoose-db')(options);
mongoosedb.connection.when('available', function(err, db) {
});express-mongoose-db will start attempts to connect straight after require.


