Skip to content

Commit

Permalink
Add GET and PUT handlers, salting of data, and improve datastore.
Browse files Browse the repository at this point in the history
  • Loading branch information
ammmir committed Mar 7, 2011
1 parent e3f62de commit 0dc9f37
Show file tree
Hide file tree
Showing 5 changed files with 411 additions and 14 deletions.
134 changes: 134 additions & 0 deletions datastore.js
Expand Up @@ -12,6 +12,8 @@ var Db = mongodb.Db,
BSON = mongodb.BSONPure;
//BSON = mongodb.BSONNative;

var SaltService = require('./security').SaltService;

function DataStore() {
this.connection = null;
this.db = null;
Expand Down Expand Up @@ -48,4 +50,136 @@ DataStore.prototype.connect = function connect(cb) {
});
};

DataStore.prototype.clearCollection = function clearCollection(collection, cb) {
this.db.collection(collection, function(err, c) {
c.remove(function(err, c) {
cb(err);
});
});
};

DataStore.prototype.query = function query(collection, where, cb) {
this.db.collection(collection, function(err, c) {
if(err) {
console.log('error opening collection "' + collection + '": ' + err);
cb(err);
return;
}

where = SaltService.encrypt(collection, where);

if('_id' in where)
where['_id'] = new BSON.ObjectID(where['_id']);

c.find(where, function(err, cursor) {
if(err) {
console.log('cursor error: ' + err);
cb(err);
return;
}

cursor.nextObject(function(err, doc) {
if(err) {
console.log('cursor navigation error: ' + err);
cb(err);
return;
}

doc = SaltService.decrypt(collection, doc);

cb(null, doc);
});
});
});
};

DataStore.prototype.queryAll = function queryAll(collection, where, cb) {
this.db.collection(collection, function(err, c) {
if(err) {
console.log('error performing queryAll on ' + collection + ': ' + err);
cb(err);
return;
}

where = SaltService.encrypt(collection, where);

if('_id' in where)
where['_id'] = new BSON.ObjectID(where['_id']);

console.log("where claused: " + JSON.stringify(where));

c.find(where, function(err, cursor) {
if(err) {
console.log('error on find: ' + err);
cb(err);
return;
}

cursor.toArray(function(err, results) {
if(err) {
console.log('error calling cursor.toArray: ' + err);
cb(null);
return;
}

for(var i = 0; i < results.length; i++) {
results[i] = SaltService.decrypt(collection, results[i]);
}

console.log('found ' + results.length + ' results');
cb(null, results);
});
});
});
};

DataStore.prototype.insert = function insert(collection, doc, cb) {
this.db.collection(collection, function(err, c) {
if(err) {
console.log('error opening collection "' + collection + '": ' + err);
return cb(err);
}

doc = SaltService.encrypt(collection, doc);

c.insert(doc, function(docs) {
cb(null, doc);
});
});
};

DataStore.prototype.update = function update(collection, where, update, cb) {
this.db.collection(collection, function(err, c) {
if(err) {
console.log('error opening collection "' + collection + '": ' + err);
return cb(null);
}

where = SaltService.encrypt(collection, where);
update = SaltService.encrypt(collection, update);

c.update(where, {'$set': update}, function(err, result) {
cb(err);
});
});
};

DataStore.prototype.remove = function remove(collection, where, cb) {
this.db.collection(collection, function(err, c) {
if(err) {
console.log('error opening collection "' + collection + '": ' + err);
return cb(err);
}

c.remove(where, {}, function(err) {
if(err) {
console.log('error removing from ' + collection + ' where ' + JSON.stringify(where));
return cb(err);
}

cb();
});
});
};

exports.DataStore = global['DataStore'] || (global['DataStore'] = new DataStore());
2 changes: 2 additions & 0 deletions index.js
@@ -0,0 +1,2 @@
exports.DataStore = require('./datastore').DataStore;
exports.REST = require('./rest').REST;
94 changes: 94 additions & 0 deletions rest.js
Expand Up @@ -5,9 +5,103 @@
* @author Amir Malik
*/

var url = require('url');

var connect = require('../../vendor/connect');

var DataStore = require('./datastore').DataStore;

function REST() {
}

REST.prototype.get = function(req, res, next) {
var collection = req.params.collection,
id = req.params.id,
query = url.parse(req.url, true).query;

var where = query || {}
var count = 0;

if(id) {
where['_id'] = id;
}

console.log(JSON.stringify(where));

DataStore.queryAll(collection, where, function(err, results) {
if(err) {
console.log("REST.get error: " + err);
throw err;
}

req.nixus.data = [];

if(id) {
if(results.length == 0)
req.nixus.data = null;
else
req.nixus.data = results[0];
} else {
req.nixus.data = [];
for(var i = 0; i < results.length; i++) {
req.nixus.data.push(req.nixus.process(results[i], req, res));
}
}
next();
});
};

REST.prototype.put = function(req, res, next) {
var collection = req.params.collection,
id = req.params.id;

var doc = req.body;
var where = {
_id: id,
};

DataStore.update(collection, where, doc, function(err) {
if(err) {
console.log("REST.put error: " + err);
throw err;
}

next();
});
};

REST.prototype.post = function(req, res, next) {
var collection = req.params.collection,
id = req.params.id;

var doc = req.body;
doc = req.nixus.process(doc, req, res);

DataStore.insert(collection, doc, function(err, doc) {
if(err)
throw err;

console.log("REST.post inserted doc with _id: " + doc['_id']);

req.nixus.data = doc;
next();
});
};

REST.prototype.del = function(req, res, next) {
var collection = req.params.collection,
id = req.params.id;

var where = {
_id: id,
};

DataStore.remove(collection, where, function(err) {
if(err)
throw err;

next();
});
};

exports.REST = global['REST'] || (global['REST'] = new REST());
3 changes: 3 additions & 0 deletions security.js
Expand Up @@ -28,6 +28,9 @@ SaltService.prototype._docrypt = function(collection, doc, encrypt) {
keys.forEach(function(key) {
var c;

if(!(key in doc) || !doc[key])
return;

if(encrypt) {
c = crypto.createCipher(algo, key);
} else {
Expand Down

0 comments on commit 0dc9f37

Please sign in to comment.