diff --git a/lib/pocket/collection.js b/lib/pocket/collection.js index 7153d33..73d356e 100644 --- a/lib/pocket/collection.js +++ b/lib/pocket/collection.js @@ -11,13 +11,15 @@ function Collection(name, dbPath) { Collection.prototype.put = function(id, obj, callback) { var self = this; - path.exists(self._dbPath + '/data/' + self._name, function(exists) { + 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(self._dbPath + '/data/' + self._name, 0755, function(err) { + mkdirp(dir, 0755, function(err) { if (err) { return callback(err); } writeObject(); }); @@ -26,7 +28,30 @@ Collection.prototype.put = function(id, obj, callback) { function writeObject() { // TODO: Implement option to control pretty printing (disable by default). - fs.writeFile(self._dbPath + '/data/' + self._name + '/' + id + '.json', JSON.stringify(obj, null, 4), callback); + 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); } }