Skip to content

Instantly share code, notes, and snippets.

@mateodelnorte
Created December 6, 2016 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mateodelnorte/daafa687ed6d57a1d4e7d101f8fb4037 to your computer and use it in GitHub Desktop.
Save mateodelnorte/daafa687ed6d57a1d4e7d101f8fb4037 to your computer and use it in GitHub Desktop.
var EventEmitter = require('events').EventEmitter;
var log = require('debug')('mongo');
var client = require('mongodb').MongoClient;
var util = require('util');
function Mongo () {
EventEmitter.call(this);
}
util.inherits(Mongo, EventEmitter);
Mongo.prototype.connect = function connect (mongoUrl) {
var self = this;
client.connect(mongoUrl, function (err, db) {
if (err) {
log('✗ MongoDB Connection Error. Please make sure MongoDB is running: ', err);
self.emit('error', err);
}
self.db = db;
self.collectionOne = db.collection('collectionOne');
self.collectionTwo = db.collection('collectionTwo');
self.collectionThree = db.collection('collectionThree');
log('initialized connection to mongo at %s', mongoUrl);
self.emit('connected', db);
});
};
module.exports = new Mongo();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment