Created
November 20, 2012 13:55
-
-
Save spolu/4118083 to your computer and use it in GitHub Desktop.
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
var fwk = require('fwk'); | |
/** | |
* Object generic | |
* | |
* Implements simple methods to modifiy | |
* objects in database | |
* @inherits events.EventEmitter | |
* | |
* @param {mongo, cfg, collection, uid, [obj]} | |
*/ | |
var object = function(spec, my) { | |
my = my || {}; | |
var _super = {}; | |
my.cfg = spec.cfg; | |
my.mongo = spec.mongo; | |
my.collection = spec.collection; | |
my.uid = spec.uid; | |
if(spec.obj) | |
my.cache = spec.obj; | |
// public | |
var get; | |
var exist; | |
var remove; | |
var save; | |
var db_find; | |
var db_post; | |
// private | |
var invalidate; | |
var that = {}; | |
/** | |
* Retrieve object from database or cache if | |
* already in | |
* @param cb callback function(err, objs) | |
*/ | |
get = function(cb) { | |
var get_object = function(key, cb) { | |
var c = my.mongo.collection(my.collection); | |
c.find({uid: key}, function(err, cursor) { | |
if(err) cb(err); | |
else { | |
cursor.toArray(function(err, items) { | |
if(err) cb(err); | |
else { | |
if(items && items.length > 0) { | |
cb(null, items[0]); | |
} | |
else { | |
cb(null, null); | |
} | |
} | |
}); | |
} | |
}); | |
}; | |
if(my.cache) { | |
cb(null, my.cache); | |
} | |
else { | |
get_object(my.uid, function(err, obj) { | |
if(err) cb(err); | |
else { | |
my.cache = obj; | |
cb(null, obj); | |
} | |
}); | |
} | |
}; | |
/** | |
* Check if an object exists for uid | |
* @param cb callback function(err, bool) | |
*/ | |
exist = function(cb) { | |
get(function(err, item) { | |
if(err) cb(err); | |
else if(item) { | |
cb(null, true); | |
} | |
else { | |
cb(null, false); | |
} | |
}); | |
}; | |
/** | |
* Remove object from database | |
* @param query the request query | |
* @param cb callback function(err) | |
*/ | |
remove = function(cb) { | |
var c = my.mongo.collection(my.collection); | |
c.remove({uid: my.uid}, {safe:true}, function(err) { | |
if(err) cb(err); | |
else { | |
delete my.cache; | |
cb(); | |
} | |
}); | |
}; | |
/** | |
* Update object in database | |
* @param obj the obj to update | |
* @param cb callback function(err) | |
*/ | |
save = function(obj, cb) { | |
obj.uid = my.uid; | |
my.cache = obj; | |
var c = my.mongo.collection(my.collection); | |
c.update({uid: my.uid}, obj, | |
{upsert: true, multi: false, safe: true}, function(err) { | |
if(err) cb(err); | |
else { | |
cb(); | |
} | |
}); | |
}; | |
/** | |
* db helper to find elements | |
* @param collection the collection to use | |
* @param query a query | |
*/ | |
db_find = function(collection, query, cb) { | |
var c = my.mongo.collection(collection); | |
c.find(query, function(err, cursor) { | |
if(err) cb(err); | |
else { | |
cursor.toArray(cb); | |
} | |
}); | |
}; | |
/** | |
* db helper function to post element | |
* @param collection | |
*/ | |
db_post = function(collection, obj, cb){ | |
var c = my.mongo.collection(collection); | |
c.insert(obj, cb); | |
}; | |
fwk.method(that, 'get', get, _super); | |
fwk.method(that, 'exist', exist, _super); | |
fwk.method(that, 'remove', remove, _super); | |
fwk.method(that, 'save', save, _super); | |
fwk.method(that, 'db_find', db_find, _super); | |
fwk.method(that, 'db_post', db_post, _super); | |
return that; | |
}; | |
exports.object = object; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment