summaryrefslogtreecommitdiff
path: root/app/node_modules/okdb/index.js
blob: 79ce1ebdb118fe61026e61e2f540a37fa9d96033 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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 || {};
  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');
  }
}

/**
 * DB implementation backed by a JSON file.
 * TODO Incomplete
 */
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);
}

FSDB.prototype._resolve = function(data, error) {
  return Q.Promise(function resolvePromise(resolve, reject) {
    if (error) {
      reject(error);
    } else {
      resolve(data);
    }
  });
};

FSDB.prototype.get = function(collection, query) {
  if (!query) {
    return this._resolve(null, new Error('No query given'));
  } else {
    var data = this._db(collection).find(query);
    return this._resolve(data);
  }
};

FSDB.prototype.put = function(collection, query, data) {
  data = data || {};
  if (!query) {
    return this._resolve(null, new Error('No query given'));
  } else if (this._db(collection).find(query)) {
    var updated = this._db(collection)
        .chain()
        .find(query)
        .assign(data)
        .value();
    return this._resolve(updated);
  } else {
    return this._resolve(null, new Error('Cannot update nonexistent entry'));
  }
};

FSDB.prototype.create = function(collection, data) {
  var created = this._db(collection)
    .chain()
    .push(data)
    .last()
    .value();
  return this._resolve(created);
};

FSDB.prototype.remove = function(collection, id, data) {
  throw new Error('Not implemented!');
};

FSDB.prototype.find = function(collection, query) {
  if (!collection || !query) {
    return this._resolve(null, new Error('Bad input'));
  } else {
    var data = this._db(collection).find(query);
    return this._resolve(data);
  }
};

FSDB.prototype.getMeta = function() {
  var data = this._db('meta').first();
  return this._resolve(data || {});
};

FSDB.prototype.getAll = function(collection) {
  var data = this._db(collection).toArray();
  return this._resolve(data || []);
};

module.exports = OKDB;