Permalink
Browse files

added 'findAndLoad'

  • Loading branch information...
1 parent 2fd8bcd commit 32b10ff4d665329bb2ffc9e7d01e0e283d86d89a @mkuklis mkuklis committed Oct 8, 2012
Showing with 51 additions and 35 deletions.
  1. +22 −22 lib/nohm.js
  2. +29 −13 lib/retrieve.js
View
@@ -54,7 +54,7 @@ Nohm.model = function (name, options, temp) {
if ( ! name ) {
this.logError('When creating a new model you have to provide a name!');
}
-
+
var obj = function (id, cb) {
this.init(options);
// if this is changed, check if the factory needs to be changed as well!
@@ -63,21 +63,21 @@ Nohm.model = function (name, options, temp) {
}
};
obj.prototype = new Nohm();
-
+
obj.prototype.modelName = name;
obj.prototype.idGenerator = options.idGenerator || 'default';
-
+
var meta = {
inDb: false
};
meta.properties = options.properties;
meta.version = _meta_version(meta.properties, obj);
-
+
obj.prototype.meta = meta;
-
+
// this creates a few functions for short-form like: SomeModel.load(1, function (err, props) { /* `this` is someModelInstance here */ });
- var shortFormFuncs = ['load', 'find', 'save', 'sort', 'subscribe', 'subscribeOnce', 'unsubscribe'];
+ var shortFormFuncs = ['load', 'find', 'findAndLoad', 'save', 'sort', 'subscribe', 'subscribeOnce', 'unsubscribe'];
shortFormFuncs.forEach(function (val) {
obj[val] = function () {
var instance = new obj();
@@ -101,17 +101,17 @@ Nohm.model = function (name, options, temp) {
var _meta_version = function (properties, obj) {
var hash = crypto.createHash('sha1');
-
+
hash.update(JSON.stringify(properties));
hash.update(JSON.stringify(obj.prototype.modelName));
hash.update(obj.prototype.idGenerator.toString());
-
+
return hash.digest('hex');
};
/**
* Factory to produce instances of models
- *
+ *
* @param {String} name Name of the model (as given to Nohm.model())
* @param {Number} [id] Id to be loaded. This requires the callback.
* @param {Function} [callback] Called when the user is loaded from the db.
@@ -133,7 +133,7 @@ Nohm.factory = function factory(name, id, callback) {
/**
* Gets all registered models.
- *
+ *
* @returns {Object} Object containing all registered models
* @static
*/
@@ -240,7 +240,7 @@ Nohm.prototype.init = function (options) {
Nohm.logError('Did not find a viable redis client in Nohm or the model: '+this.modelName);
return false;
}
-
+
if ( ! this.meta.inDb) {
__updateMeta.call(this);
}
@@ -254,7 +254,7 @@ Nohm.prototype.init = function (options) {
// initialize the properties
if (options.hasOwnProperty('properties')) {
-
+
for (var p in options.properties) {
if (options.properties.hasOwnProperty(p)) {
this.properties[p] = h.$extend(true, {}, options.properties[p]); // deep copy
@@ -273,7 +273,7 @@ Nohm.prototype.init = function (options) {
}
}
}
-
+
if (options.hasOwnProperty('methods')) {
addMethods.call(this, options.methods);
}
@@ -296,18 +296,18 @@ var __updateMeta = function () {
return false;
}
var self = this;
-
+
var version_key = Nohm.prefix.meta.version + this.modelName;
var idGenerator_key = Nohm.prefix.meta.idGenerator + this.modelName;
var properties_key = Nohm.prefix.meta.properties + this.modelName;
- var properties = traverse(self.meta.properties).map(function (x) {
+ var properties = traverse(self.meta.properties).map(function (x) {
if (typeof x === 'function') {
- return String(x)
+ return String(x)
} else {
return x;
}
});
-
+
this.getClient().get(version_key, function (err, db_version) {
if (err) {
Nohm.logError(err);
@@ -335,11 +335,11 @@ var __updateMeta = function () {
/**
* DO NOT USE THIS UNLESS YOU ARE ABSOLUTELY SURE ABOUT IT!
- *
+ *
* Deletes any keys from the db that start with nohm prefixes.
- *
+ *
* DO NOT USE THIS UNLESS YOU ARE ABSOLUTELY SURE ABOUT IT!
- *
+ *
* @param {Object} [redis] You can specify the redis client to use. Default: Nohm.client
* @param {Function} [callback] Called after all keys are deleted.
*/
@@ -357,11 +357,11 @@ Nohm.purgeDb = function (redis, callback) {
});
};
var deletes = [];
-
+
Object.keys(Nohm.prefix).forEach(function (key) {
deletes.push(async.apply(delKeys, Nohm.prefix[key]));
});
-
+
async.series(deletes, function (err) {
callback(err);
});
View
@@ -70,6 +70,22 @@ exports.load = function (id, callback) {
});
};
+/**
+ * Finds ids of objects and loads them.
+ */
+exports.findAndLoad = function findAndLoad(searches, callback) {
+ var self = this;
+ this.find(searches, function (err, ids) {
+ if (err) callback(err, ids);
+ if (ids.length > 0) {
+ async.map(ids, function (id, callback) {
+ self.load(id, function (err, props) {
+ callback(err, this.allProperties());
+ });
+ }, callback);
+ }
+ });
+};
/**
* Finds ids of objects by search arguments
@@ -127,8 +143,8 @@ exports.find = function find(searches, callback) {
callback(err, ids);
});
};
-
-
+
+
if (typeof searches === 'function') {
callback = searches;
searches = {};
@@ -186,18 +202,18 @@ exports.sort = function (options, ids) {
ids = false;
}
options = typeof(options) !== 'function' && typeof(options) === 'object' && Object.keys(options).length > 0 ? options : {};
-
+
if (ids.length > 0 && options === {}) {
return callback(ids.sort());
}
-
+
if ( ! options.field || ! this.properties.hasOwnProperty(options.field)) {
callback('invalid field in options', ids);
return Nohm.logError('Invalid field in sort() options: ' + options.field);
}
-
+
var field_type = this.properties[options.field].type;
-
+
var alpha = options.alpha || field_type === 'string' ? 'ALPHA' : '';
var direction = options.direction ? options.direction : 'ASC';
var scored = Nohm.indexNumberTypes.indexOf(field_type) !== -1;
@@ -218,13 +234,13 @@ exports.sort = function (options, ids) {
var zset_key = Nohm.prefix.scoredindex+this.modelName+':'+options.field;
var client = this.getClient();
var tmp_key;
-
+
if (ids) {
// to get the intersection of the given ids and all ids on the server we first
// temporarily store the given ids either in a set or sorted set and then return the intersection
-
- client = client.multi();
-
+
+ client = client.multi();
+
if (scored) {
tmp_key = zset_key+':tmp_sort:'+(+ new Date()) + Math.ceil(Math.random()*1000);
var tmp_zadd_args = [tmp_key];
@@ -249,14 +265,14 @@ exports.sort = function (options, ids) {
}
if (ids) {
client.del(tmp_key);
- client.exec(Nohm.logError);
+ client.exec(Nohm.logError);
}
};
var sortNormal = function (client, idset_key, field, alpha, direction, start, stop, callback) {
var hash_key = Nohm.prefix.hash+this.modelName;
- client.sort([idset_key,
- 'BY', hash_key+':*->'+field,
+ client.sort([idset_key,
+ 'BY', hash_key+':*->'+field,
'LIMIT', start, stop,
direction,
alpha],

0 comments on commit 32b10ff

Please sign in to comment.