Permalink
Browse files

refactor schema.prototype.indexes

  • Loading branch information...
aheckmann committed May 17, 2011
1 parent fd9b333 commit 10676bae311a9851e99dae0d5da0a57cbe47997a
Showing with 34 additions and 18 deletions.
  1. +34 −18 lib/mongoose/schema.js
View
@@ -361,14 +361,18 @@ Schema.prototype.set = function (key, value) {
*/
Schema.prototype.__defineGetter__('indexes', function () {
- var index
- , indexes = []
+ var indexes = []
, seenSchemas = [];
+ collectIndexes(this);
+
+ return indexes;
+
function collectIndexes (schema, prefix) {
if (~seenSchemas.indexOf(schema)) return;
seenSchemas.push(schema);
+ var index;
var paths = schema.paths;
prefix = prefix || '';
@@ -388,35 +392,47 @@ Schema.prototype.__defineGetter__('indexes', function () {
}
}
- if (!prefix) {
- return indexes = indexes.concat(schema._indexes);
+ if (prefix) {
+ fixSubIndexPaths(schema, prefix);
+ } else {
+ indexes = indexes.concat(schema._indexes);
}
+ }
- // Check for indexes added to subdocs using Schema.index().
- // These indexes need their paths prefixed properly.
- // schema._indexes = [ [indexObj, options], [indexObj, options] ..]
- var subindexes = schema._indexes;
- var len = subindexes.length;
+ /**
+ * Checks for indexes added to subdocs using Schema.index().
+ * These indexes need their paths prefixed properly.
+ *
+ * schema._indexes = [ [indexObj, options], [indexObj, options] ..]
+ */
+
+ function fixSubIndexPaths (schema, prefix) {
+ var subindexes = schema._indexes
+ , len = subindexes.length
+ , indexObj
+ , newindex
+ , klen
+ , keys
+ , key
+ , i = 0
+ , j
for (i = 0; i < len; ++i) {
- var indexObj = subindexes[i][0]
- , keys = Object.keys(indexObj)
- , klen = keys.length
- , newindex = {}
+ indexObj = subindexes[i][0];
+ keys = Object.keys(indexObj);
+ klen = keys.length;
+ newindex = {};
// use forward iteration, order matters
- for (var j = 0; j < klen; ++j) {
- var key = keys[j];
+ for (j = 0; j < klen; ++j) {
+ key = keys[j];
newindex[prefix + key] = indexObj[key];
}
indexes.push([newindex, subindexes[i][1]]);
}
}
- collectIndexes(this);
-
- return indexes;
});
/**

0 comments on commit 10676ba

Please sign in to comment.