Skip to content

Commit

Permalink
Implement functionality to establish relations among collections.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Dec 4, 2011
1 parent 60eff43 commit 2addafe
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions lib/pocket/collection.js
Expand Up @@ -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();
});
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 2addafe

Please sign in to comment.