From 0c4f6ad96c2ee1b1c948dd227e291ff73023b95b Mon Sep 17 00:00:00 2001 From: Sean Fridman Date: Fri, 3 Apr 2015 12:35:32 -0400 Subject: Directly expose DB impl rather than compose dawg --- app/index.js | 4 +-- app/node_modules/okdb/index.js | 56 +++++++++++++++++------------------------- 2 files changed, 24 insertions(+), 36 deletions(-) (limited to 'app') diff --git a/app/index.js b/app/index.js index f4e9593..29dcfd0 100644 --- a/app/index.js +++ b/app/index.js @@ -14,6 +14,7 @@ var OKSchema = require('okschema'); * Basically takes configuration and gives you a server. */ function OKCMS(options) { + if (!(this instanceof OKCMS)) return new OKCMS(options); options = options || {}; var root = this._root = options.root || 'www'; var schemaConfig = options.schemas || {}; @@ -21,8 +22,7 @@ function OKCMS(options) { var viewConfig = options.views || { '/': { template: 'index' } }; - // TODO Make configurable - var db = this._db = new OKDB(); + var db = new OKDB(options.db || 'fs'); var templateCache = this._templateCache = new OKTemplate({root: root}); var server = this._server = new OKServer({root: root}); // Special query for getting meta info diff --git a/app/node_modules/okdb/index.js b/app/node_modules/okdb/index.js index f32627c..cf670c1 100644 --- a/app/node_modules/okdb/index.js +++ b/app/node_modules/okdb/index.js @@ -10,64 +10,52 @@ low.mixin(low.mixin(require('underscore-db'))); function OKDB(options) { if (!(this instanceof OKDB)) return new OKDB(options); options = options || {}; - this._db = options.db || JSONDown('db'); + var type; + if (typeof options === 'string') + type = options; + else + type = options.type; + if (!type) + throw new Error('No DB type provided'); + switch (type) { + case 'fs': + return FSDB(options); + default: + throw new Error('Invalid DB type'); + } } -OKDB.prototype.getMeta = function() { - return this._db.getMeta(); -}; - -OKDB.prototype.putMeta = function(meta) { - return this._db.putMeta(meta); -} - -OKDB.prototype.get = function(collection, id) { - return this._db.get(collection, id); -}; - -OKDB.prototype.getAll = function(collection) { - return this._db.getAll(collection); -}; - -OKDB.prototype.put = function(collection, id) { - return this._db.put(collection, id); -}; - -OKDB.prototype.putBatch = function(collection, data) { - return this._db.putBatch(collection, data); -}; - /** * DB implementation backed by a JSON file. - * TODO Unfinished + * TODO Incomplete */ -function JSONDown(name, options) { - if (!(this instanceof JSONDown)) return new JSONDown(name, options); +function FSDB(options) { + if (!(this instanceof FSDB)) return new FSDB(options); options = options || {}; + var name = options.name || 'db'; var filename = name + '.json'; this._db = low(filename); } -JSONDown.prototype._resolve = function(data) { +FSDB.prototype._resolve = function(data) { return Q.Promise(function resolvePromise(resolve, reject) { resolve(data); }); }; -JSONDown.prototype.get = function(collection, id) { +FSDB.prototype.get = function(collection, id) { var data = this._db(collection).get(id); - return this._resolve(data || {}); + return this._resolve(data); }; -JSONDown.prototype.getMeta = function() { +FSDB.prototype.getMeta = function() { var data = this._db('meta').first(); return this._resolve(data || {}); }; -JSONDown.prototype.getAll = function(collection) { +FSDB.prototype.getAll = function(collection) { var data = this._db(collection).toArray(); return this._resolve(data || []); }; module.exports = OKDB; -module.exports.JSONDown = JSONDown; -- cgit v1.2.3-70-g09d2