Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for custom adapter registration #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
49 changes: 33 additions & 16 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const NeDbClient = require('./clients/nedbclient');
const MongoClient = require('./clients/mongoclient');
let _adapter;

/**
* Connect to current database
Expand All @@ -10,20 +11,36 @@ const MongoClient = require('./clients/mongoclient');
* @param {Object} options
* @returns {Promise}
*/
exports.connect = function(url, options) {
if (url.indexOf('nedb://') > -1) {
// 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) {
// url example: 'mongodb://localhost:27017/myproject'
return MongoClient.connect(url, options).then(function(db) {
global.CLIENT = db;
return db;
});
} else {
return Promise.reject(new Error('Unrecognized DB connection url.'));
exports.connect = function (url, options) {
function handleConnectionSuccessfully(db) {
global.CLIENT = db;
return db;
}
};

let connect;
// We have custom adapter
if (_adapter) {
connect = NeDbClient.connect(url, options);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably mean _adapter.connect?

}
else
if (url.indexOf('nedb://') > -1) {
// url example: nedb://path/to/file/folder
connect = NeDbClient.connect(url, options);
} else if (url.indexOf('mongodb://') > -1) {
// url example: 'mongodb://localhost:27017/myproject'
connect = MongoClient.connect(url, options);
} else {
return Promise.reject(new Error('Unrecognized DB connection url.'));
}

return connect.then(handleConnectionSuccessfully);
};

/**
* Register new adapter instead of using MongoDB and NeDB
* We assume that the adapter is valid and inherit DatabaseClient
* @param {DatabaseClient} adapter
*/
exports.registerAdapter = function handleAdapterRegister(adapter) {
_adapter = adapter;
}