Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.'));
}
};
};