Promisified (Bluebird) connection utils for Mongoose
mongootils
requires Mongoose as a peer dependency, so make sure you have it installed in your project.
npm i --save mongootils
var Mongootils = require('mongootils');
var db = new Mongootils('mongodb://localhost/database');
db.connect()
.then(function(connection){
console.log('Connected to ' + db.getConnectionURI());
return db.disconnect();
})
.then(function(uri){
console.log('Disconnected from ' + uri);
});
var Mongootils = require('mongootils');
var app = require('express')();
var db = new Mongootils('mongodb://localhost/database');
db.connect()
.then(function(connection){
console.log('Connected to ' + db.getConnectionURI());
app.set('db', db);
});
// In another place...
app.get('/api/users', function(req, res){
var db = req.app.get('db');
var User = db.model('User');
// ...
});
var db = new Mongootils(mongoose.connections[3]);
The same arguments as mongoose.connect
or mongoose.createConnection
).
var db = new Mongootils('mongodb://localhost/mongootils_test', { /* mongoose options object */ });
Returns a Bluebird promise that is resolved when the connection has been effectively opened, or rejected otherwise.
Returns a Bluebird promise that is resolved when the connection has been effectively closed, or rejected otherwise.
Checks for the underlying connection state.
if (db.is('connecting') || db.is('connected')) {
// Listen to Thin Lizzy
} else {
// ... or listen to Thin Lizzy
}
Retrieves the underlying NativeConnection
object.
Returns the mongoose
-valid connection URI for the underlying connection.
var db = new Mongootils('mongodb://localhost:27000,127.0.0.1:27001/my_db');
console.log(db.getConnectionURI()); // prints 'mongodb://localhost:27000/my_db,mongodb://127.0.0.1:27001/my_db'
- Test multiple connections to the same database with two different Mongootils object
- Test multiple database URI
- Code feature
- Write tests
- Pull Request