Skip to content

Commit

Permalink
Merge pull request #1 from daumiller/OptionalDependencies
Browse files Browse the repository at this point in the history
Merge PR58 from chaosfinity
scottwrobinson#58
  • Loading branch information
Dillon Aumiller committed Mar 17, 2016
2 parents efd7b6e + 60333ab commit 711f1ef
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions lib/db.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
var NeDbClient = require('./clients/nedbclient');
var MongoClient = require('./clients/mongoclient');
try {
var NeDbClient = require('./clients/nedbclient');
} catch (e) {
if(e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
var NeDbClient = null;
}

try {
var MongoClient = require('./clients/mongoclient');
} catch (e) {
if(e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
var MongoClient = null;
}

exports.connect = function(url, options) {
if (url.indexOf('nedb://') > -1) {
if(NeDbClient === null) {
return Promise.reject(new Error('The NeDB dependency has not been met'));
}
// url example: nedb://path/to/file/folder
return NeDbClient.connect(url, options).then(function(db) {
global.CLIENT = db;
return db;
});
} else if(url.indexOf('mongodb://') > -1) {
if(MongoClient === null) {
return Promise.reject(new Error('The MongoDB dependency has not been met'));
}
// url example: 'mongodb://localhost:27017/myproject'
return MongoClient.connect(url, options).then(function(db) {
global.CLIENT = db;
Expand All @@ -17,4 +38,4 @@ exports.connect = function(url, options) {
} else {
return Promise.reject(new Error('Unrecognized DB connection url.'));
}
};
};

0 comments on commit 711f1ef

Please sign in to comment.