Created
December 6, 2016 20:00
-
-
Save mateodelnorte/daafa687ed6d57a1d4e7d101f8fb4037 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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