Skip to content

Instantly share code, notes, and snippets.

@raycmorgan
Forked from jimmyjacobson/gist:1631704
Created January 18, 2012 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raycmorgan/1636039 to your computer and use it in GitHub Desktop.
Save raycmorgan/1636039 to your computer and use it in GitHub Desktop.
Redis Queries
=== database object file
var errors = require('./errors');
var db;
exports.setDatabase = function(database) {
db = database;
}
exports.getDatabase = function() {
return db;
}
exports.getString = function (key, callback) {
exports.getByKey(key, 'string', callback);
}
exports.getHash = function (key, callback) {
exports.getByKey(key, 'hash', callback);
}
exports.getByKey = function(key, type, callback) {
if (arguments.length == 2) {
callback = type;
db.type(key, function(err, type) {
if (err) { return callback(err); }
getByType(key, type, callback);
});
}
else {
getByType(key, type, callback);
}
}
// Private
function getByType(key, type, callback) {
if (typeof getByTypeFns[type] !== 'function') {
return callback(new errors.DatabaseError([type + ' not supported Redis type']));
}
return getByTypeFns[type](key, callback);
}
// export this for unit test ability
var getByTypeFns = exports._getByTypeFns = {
string: getByTypeString,
hash: getByTypeHash
};
function getByTypeString(key, callback) {
db.get(key, function(err, string) {
if (err) { return callback(err); }
if (!string) {
var error = new errors.NotFound([key + ' not found']);
return callback(error);
}
return callback(null, string);
});
};
function getByTypeHash(key, callback) {
db.hgetall(key, function(err, hash) {
if (err) { return callback(err); }
if (Object.keys(hash).length == 0) {
var error = new errors.NotFound([key + ' not found']);
return callback(error);
}
return callback(null, hash);
});
};
== unit tests ==
var redis = require("redis")
, db = redis.createClient();
var dao = require('./databaseObject');
dao.setDatabase(db);
describe('Object', function(){
before(function(done){
db.set("key:should:exist", 1);
db.hmset("hash:should:exist", {foo: '1', bar: '2'}, function(err) {
done();
});
});
describe('get Key by Not Found ID', function() {
it('should return Not Found', function(done) {
dao.getByKey('this:key:does:not:exist', 'string', function(err, string) {
err.should.exist;
done();
});
});
});
describe('Get Hash by Not Found ID', function() {
it('should return Not Found', function(done) {
dao.getByKey('key:not:found', 'hash', function(err) {
err.should.exist;
done();
});
});
});
describe('Get Value by ID', function() {
it('should not fail', function(done) {
dao.getByKey('key:should:exist', 'string', function(err, value) {
value.should.exist;
done();
});
});
});
describe('Get Hash by ID', function() {
it('should return a hash', function(done) {
dao.getByKey('hash:should:exist', function(err, hash) {
hash.should.exist;
done();
});
});
});
describe('Get Hash by ID and Type', function() {
it('should return a hash', function(done) {
dao.getByKey('hash:should:exist', 'hash', function(err, hash) {
hash.should.exist;
done();
});
});
});
after(function() {
db.flushall();
})
});
== errors ==
exports.NotFound = NotFound;
exports.ValidationError = ValidationError;
exports.DatabaseError = DatabaseError;
function NotFound(msg) {
this.name = 'NotFound';
this.statusCode = 404;
this.textMessage = msg;
this.json = {
type: this.name,
msg: this.textMessage
};
Error.call(this, msg);
Error.captureStackTrace(this, arguments.callee);
}
NotFound.prototype.__proto__ = Error.prototype;
function ValidationError (errors) {
this.name = 'ValidationError';
this.statusCode = 403;
this.textMessage = "Validation failed.";
this.errors = errors;
this.json = {
type: this.name,
msg: this.textMessage,
errors: this.errors
};
Error.call(this, this.textMessage);
Error.captureStackTrace(this, arguments.callee);
}
ValidationError.prototype.__proto__ = Error.prototype;
function DatabaseError (errors) {
this.name = 'DatabaseError';
this.statusCode = 500;
this.textMessage = "Database Error.";
this.errors = errors;
this.json = {
type: this.name,
msg: this.textMessage,
errors: this.errors
};
Error.call(this, this.textMessage);
Error.captureStackTrace(this, arguments.callee);
}
DatabaseError.prototype.__proto__ = Error.prototype;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment