forked from jaredhanson/pocket
/
collection.js
59 lines (49 loc) · 1.57 KB
/
collection.js
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
var path = require('path')
, fs = require('fs')
, mkdirp = require('mkdirp')
, util = require('util');
function Collection(name, dbPath) {
this._name = name;
this._dbPath = dbPath;
}
Collection.prototype.put = function(id, obj, callback) {
var self = this;
var dir = self._dbPath + '/data/' + self._name;
path.exists(dir, function(exists) {
if (exists) {
writeObject();
} else {
// No directory exists in which to store objects in this collection.
// Create a directory and write the object.
mkdirp(dir, 0755, function(err) {
if (err) { return callback(err); }
writeObject();
});
}
});
function writeObject() {
// TODO: Implement option to control pretty printing (disable by default).
fs.writeFile(dir + '/' + id + '.json', JSON.stringify(obj, null, 4), callback);
}
}
Collection.prototype.relate = function(id, rel, refs, callback) {
var self = this;
var dir = self._dbPath + '/rel/' + self._name + '/' + id;
path.exists(dir, function(exists) {
if (exists) {
writeReferences();
} else {
// No directory exists in which to store the relations of this object.
// Create a directory and write the references.
mkdirp(dir, 0755, function(err) {
if (err) { return callback(err); }
writeReferences();
});
}
});
function writeReferences() {
// TODO: Implement option to control pretty printing (disable by default).
fs.writeFile(dir + '/' + rel + '.json', JSON.stringify(refs, null, 4), callback);
}
}
module.exports = Collection;