summaryrefslogtreecommitdiff
path: root/app/node_modules/okdb/index.js
blob: f32627cfe2ba50a5df7748d4f7e3553742b496f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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;