var Q = require('q'); var format = require('util').format; var low = require('lowdb'); low.mixin(low.mixin(require('underscore-db'))); /** * OKDB! * Minimal interface over a database of named collections of documents. */ function OKDB(options) { if (!(this instanceof OKDB)) return new OKDB(options); options = options || {}; this._db = options.db || JSONDown('db'); } 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 */ function JSONDown(name, options) { if (!(this instanceof JSONDown)) return new JSONDown(name, options); options = options || {}; var filename = name + '.json'; this._db = low(filename); } JSONDown.prototype._resolve = function(data) { return Q.Promise(function resolvePromise(resolve, reject) { resolve(data); }); }; JSONDown.prototype.get = function(collection, id) { var data = this._db(collection).get(id); return this._resolve(data || {}); }; JSONDown.prototype.getMeta = function() { var data = this._db('meta').first(); return this._resolve(data || {}); }; JSONDown.prototype.getAll = function(collection) { var data = this._db(collection).toArray(); return this._resolve(data || []); }; module.exports = OKDB; module.exports.JSONDown = JSONDown;