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 to register custom clients/ fallback to bundled clients #98

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

exports.connect = require('./lib/db').connect;
exports.Clients = require('./lib/clients').Clients;
exports.getClient = require('./lib/clients').getClient;

exports.Document = require('./lib/document');
Expand Down
42 changes: 36 additions & 6 deletions lib/clients/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
'use strict';

const assertConnected = function(db) {
if (db === null || db === undefined) {
throw new Error('You must first call \'connect\' before loading/saving documents.');
const DatabaseClient = require('./client');

var clients = {};

class Clients {
static register(type, client) {
if (!DatabaseClient.prototype.isPrototypeOf(client.prototype))
throw new TypeError(`Client must derive from 'DatabaseClient'`);
clients[type] = client;
}
};

static unregister(type) {
clients[type] = undefined;
}

static get(type) {
if (!clients.hasOwnProperty(type))
this.registerDefaultClients(type);
return clients[type];
}

static registerDefaultClients(type) {
switch (type) {
case 'nedb':
this.register(type, require('./nedbclient'));
break;
case 'mongodb':
this.register(type, require('./mongoclient'));
break;
}
}
}

exports.Clients = Clients;

exports.getClient = function() {
const client = global.CLIENT;
assertConnected(client);
if (client === null || client === undefined)
throw new Error('You must first call \'connect\' before loading/saving documents.');
return client;
};
};
29 changes: 13 additions & 16 deletions lib/db.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

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

/**
* Connect to current database
Expand All @@ -11,19 +10,17 @@ const MongoClient = require('./clients/mongoclient');
* @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 {
let index = url.indexOf('://');
if (index <= 0)
return Promise.reject(new Error('Unrecognized DB connection url.'));
}

let type = url.slice(0, index);
let client = Clients.get(type);
if (client === null || client === undefined)
return Promise.reject(new Error(`No Client registered for '${type}'`));

return client.connect(url, options).then(function(db) {
global.CLIENT = db;
return db;
});
};