From 4cb67532e389efc6de3261d4b739bdddf0f20e63 Mon Sep 17 00:00:00 2001 From: Ryan Stowell Date: Sat, 18 Feb 2017 18:14:49 -0600 Subject: [PATCH 1/2] Add support to register custom clients/ fallback to bundled clients --- index.js | 5 ++++- lib/clients/index.js | 42 ++++++++++++++++++++++++++++++++++++------ lib/db.js | 29 +++++++++++++---------------- 3 files changed, 53 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index 6ebc3d2..9d2ca7f 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,10 @@ 'use strict'; +const { Clients, getClient } = require('./lib/clients'); + +exports.Clients = Clients; +exports.getClient = getClient; exports.connect = require('./lib/db').connect; -exports.getClient = require('./lib/clients').getClient; exports.Document = require('./lib/document'); exports.EmbeddedDocument = require('./lib/embedded-document'); diff --git a/lib/clients/index.js b/lib/clients/index.js index f9b0bab..40241cb 100644 --- a/lib/clients/index.js +++ b/lib/clients/index.js @@ -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; -}; \ No newline at end of file +}; diff --git a/lib/db.js b/lib/db.js index fa1cb9a..4bc15e4 100644 --- a/lib/db.js +++ b/lib/db.js @@ -1,7 +1,6 @@ 'use strict'; -const NeDbClient = require('./clients/nedbclient'); -const MongoClient = require('./clients/mongoclient'); +const { Clients } = require('./clients'); /** * Connect to current database @@ -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; + }); }; \ No newline at end of file From f3c5b24f377068a5b9d04402f5452f238dc48cc4 Mon Sep 17 00:00:00 2001 From: Ryan Stowell Date: Sat, 18 Feb 2017 20:21:40 -0600 Subject: [PATCH 2/2] Remove unsupported syntax --- index.js | 6 ++---- lib/db.js | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 9d2ca7f..b68ca93 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,8 @@ 'use strict'; -const { Clients, getClient } = require('./lib/clients'); - -exports.Clients = Clients; -exports.getClient = getClient; exports.connect = require('./lib/db').connect; +exports.Clients = require('./lib/clients').Clients; +exports.getClient = require('./lib/clients').getClient; exports.Document = require('./lib/document'); exports.EmbeddedDocument = require('./lib/embedded-document'); diff --git a/lib/db.js b/lib/db.js index 4bc15e4..b445202 100644 --- a/lib/db.js +++ b/lib/db.js @@ -1,6 +1,6 @@ 'use strict'; -const { Clients } = require('./clients'); +const Clients = require('./clients').Clients; /** * Connect to current database