Created
January 18, 2012 07:10
-
-
Save jimmyjacobson/1631704 to your computer and use it in GitHub Desktop.
Redis Queries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=== database object file | |
var errors = require('./errors'); | |
var db; | |
exports.setDatabase = function(database) { | |
db = database; | |
} | |
exports.getDatabase = function() { | |
return db; | |
} | |
exports.getByKey = function(key, type, callback) { | |
if (Object.keys(arguments).length == 2) { | |
callback = type; | |
db.type(key, function(err, type) { | |
if (err) { return callback(err);} | |
getByType(key, type, function(err, value) { | |
callback(err, value); | |
}); | |
}); | |
} | |
else { | |
getByType(key, type, function(err, value) { | |
callback(err, value); | |
}); | |
} | |
} | |
function getByType(key, type, callback) { | |
if (type == 'none') { | |
return callback(new errors.DatabaseError([type + ' not supported Redis type'])); | |
} | |
else if (type == 'string') { | |
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); | |
}); | |
} | |
else if (type == 'hash') { | |
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