Skip to content

Commit

Permalink
Switched to Bluebird for promises (performance reasons)
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Feb 14, 2015
1 parent a706e45 commit 84898de
Show file tree
Hide file tree
Showing 10 changed files with 372 additions and 498 deletions.
69 changes: 33 additions & 36 deletions lib/Database.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
var MongoClient = require('mongodb').MongoClient,
_ = require('lodash'),
var _ = require('lodash'),
fn = require('functionality'),
Q = require('q');
Promise = require('bluebird'),
MongoClient = Promise.promisifyAll(require('mongodb').MongoClient);

var Model = require('./Model.js'),
Instance = require('./Instance.js');
var IridiumModel = require('./Model.js'),
IridiumInstance = require('./Instance.js');

(require.modules || {}).Database = module.exports = Database;
(require.modules || {}).IridiumDatabase = module.exports = IridiumDatabase;

function Database(uri, config) {
function IridiumDatabase(uri, config) {
/// <summary>Creates a new Iridium instance</summary>
/// <param name="uri" type="String" optional="true">A MongoDB URI which can be used to connect to the database</param>
/// <param name="config" type="Object" optional="true">A configuration object describing the details of the database to connect to and which becomes available as db.settings</param>

"use strict";
if(!(this instanceof Database)) return new Database(config);
if(!(this instanceof IridiumDatabase)) return new IridiumDatabase(config);

this.connection = null;
this.models = {};
Expand Down Expand Up @@ -49,10 +49,10 @@ function Database(uri, config) {
}
}

Database.Model = Model;
Database.Instance = Instance;
IridiumDatabase.Model = IridiumModel;
IridiumDatabase.Instance = IridiumInstance;

Database.prototype = {
IridiumDatabase.prototype = {
get uri() {
/// <summary>Gets a URL which can be used to connect to a MongoDB instance based on the configuration</summary>
/// <returns type="String" />
Expand All @@ -77,38 +77,34 @@ Database.prototype = {
}
};

Database.prototype.connect = function connect(cb) {
IridiumDatabase.prototype.connect = function connect(callback) {
/// <summary>Connects to the database server specified in the provided configuration</summary>
/// <param name="cb" type="Function" optional="true">A function to be called when the connection is completed, called immediately if one is already open</param>
/// <param name="callback" type="Function" optional="true">A function to be called when the connection is completed, called immediately if one is already open</param>

"use strict";
var deferred = Q.defer();

if(cb) deferred.promise.then(function(result) { cb(null, result); }, function(err) { cb(err); });

if (this.connection) deferred.resolve(this);
else Q.nbind(MongoClient.connect, MongoClient)(this.uri).then((function(db) {
return Promise.bind(this).then(function() {
if(this.connection) return this.connection;
return MongoClient.connectAsync(this.uri);
}).then(function(db) {
this.connection = db;
return Q(this);
}).bind(this)).then(function(result) {
deferred.resolve(result);
}, function(err) {
deferred.reject(err);
});

return deferred.promise;
return this;
}).nodeify(callback);
};

Database.prototype.close = Database.prototype.disconnect = function disconnect() {
IridiumDatabase.prototype.close = IridiumDatabase.prototype.disconnect = function disconnect() {
/// <summary>Closes the active database connection</summary>

"use strict";
if(!this.connection) return;
this.connection.close();
this.connection = null;
return Promise.bind(this).then(function() {
if(!this.connection) return this;
var conn = this.connection;
this.connection = conn;
conn.close();
return this;
});
};

Database.prototype.express = function express() {
IridiumDatabase.prototype.express = function express() {
/// <summary>Creates an Express Middleware which will make this database wrapper available through the req.db property</summary>
/// <returns type="Function"/>

Expand All @@ -120,15 +116,16 @@ Database.prototype.express = function express() {
/// <param name="res" type="Object">Node Response object</param>
/// <param name="next" type="Function">Callback used to continue to the next step in the Express request pipeline</param>

this.connect(function (err, _db) {
if (err) return next(err);
req.db = _db;
this.connect().then(function(db) {
req.db = db;
return next();
}).error(function(err) {
return next(err);
});
}).bind(this);
};

Database.prototype.register = function register() {
IridiumDatabase.prototype.register = function register() {
/// <signature>
/// <summary>Registers a plugin with the ORM, allowing extended functionality</summary>
/// <param name="plugin" type="Object">The plugin</param>
Expand Down
Loading

0 comments on commit 84898de

Please sign in to comment.