Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
Added the ability to accept a mongodb database from MongoClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Nov 28, 2013
1 parent b65fff8 commit 0b1f2c9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/mongo_ascoltatore.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ var async = require("async");
var MongoAscoltatore = function(opts) {
AbstractAscoltatore.call(this);

if (opts.db || opts.uri) {
if (typeof opts.db === 'string' || opts.uri) {
// old options
opts.uri = opts.uri || 'mongodb://127.0.0.1/';
opts.db = opts.db || 'ascoltatori';
opts.url = opts.uri + opts.db + '?auto_reconnect';
delete opts.db;
}

this._opts = opts || {};
Expand All @@ -53,8 +54,7 @@ var MongoAscoltatore = function(opts) {

var that = this;

// this operation is SYNC, so emit after this will work
MongoClient.connect(this.url, this.mongoOpts, function(err, db) {
var setupDB = function(err, db) {
if (err) {
that.emit('error', err);
return;
Expand Down Expand Up @@ -83,7 +83,13 @@ var MongoAscoltatore = function(opts) {
});
}
});
});
};

if (opts.db) {
setupDB(null, opts.db);
} else {
MongoClient.connect(this.url, this.mongoOpts, setupDB);
}
};

/**
Expand Down
12 changes: 12 additions & 0 deletions test/mongo_ascoltatore_spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

var MongoClient = require('mongodb').MongoClient;

describe("ascoltatori.MongoAscoltatore", function() {

behaveLikeAnAscoltatore(ascoltatori.MongoAscoltatore, "mongo", mongoSettings);
Expand Down Expand Up @@ -30,4 +33,13 @@ describe("ascoltatori.MongoAscoltatore", function() {
}.bind(this));
}.bind(this));
});

it("should reuse another mongo connection", function(done) {
this.instance.close(function() {
MongoClient.connect('mongodb://127.0.0.1/ascoltatoriTest3', {}, function(err, db) {
this.instance = new ascoltatori.MongoAscoltatore({ db: db });
this.instance.on('ready', done);
}.bind(this));
}.bind(this));
});
});

0 comments on commit 0b1f2c9

Please sign in to comment.