Browse files

skip/limit support. Significant optimizations of getBare

  • Loading branch information...
1 parent 7d27764 commit e6a000cec80daf20fa5c6ea8fd0683db009f8a9a @JoshuaGross committed Nov 27, 2012
Showing with 44 additions and 8 deletions.
  1. +44 −8 lib/mongoose-subpopulate.js
View
52 lib/mongoose-subpopulate.js
@@ -175,7 +175,21 @@ var wrapSchema = exports.wrapSchema = function wrapSchema (schema) {
schema.sort = function sortWrapped (sort) {
return wrapSchema(sortOrig.call(schema, sort));
};
- schema.populate.mSubpopulateWrapped = true;
+ schema.sort.mSubpopulateWrapped = true;
+ }
+ if (schema.skip && !schema.sort.mSubpopulateWrapped) {
+ var skipOrig = schema.skip;
+ schema.skip = function skipWrapped (skip) {
+ return wrapSchema(skipOrig.call(schema, skip));
+ };
+ schema.skip.mSubpopulateWrapped = true;
+ }
+ if (schema.limit && !schema.limit.mSubpopulateWrapped) {
+ var limitOrig = schema.limit;
+ schema.limit = function limitWrapped (limit) {
+ return wrapSchema(limitOrig.call(schema, limit));
+ };
+ schema.limit.mSubpopulateWrapped = true;
}
if (schema.select && !schema.select.mSubpopulateWrapped) {
var selectOrig = schema.select;
@@ -417,18 +431,40 @@ function wrapModelObject (object, cacheData, callback) {
})(i);
}
- wrappedObject.__proto__.getBare = function (callback) {
- var keys = Object.keys(wrappedObject.schema.paths);
+ wrappedObject.__proto__.getBare = function (keys, callback) {
+ if ('function' === typeof keys) {
+ callback = keys;
+ keys = undefined;
+ }
+
+ var keys = keys || Object.keys(wrappedObject.schema.paths);
var bareDictionary = {};
async.map(keys, function (i, iter) {
- if (wrappedObject[i] && wrappedObject[i].getBare) {
- wrappedObject[i].getBare(function (err, dict) {
- bareDictionary[i] = dict;
+
+ // Instead of passing a string, you can pass a dictionary in the form:
+ // { 'populated_collection_ref': ['list', 'of', 'subfields'] }
+ var subfields = undefined;
+ if ('string' !== typeof i) {
+ for (var collection in i) {
+ subfields = i[collection];
+ i = collection;
+ break;
+ }
+ }
+
+ var referencedObject = wrappedObject[i];
+ if ('undefined' !== typeof referencedObject) {
+ if (referencedObject.getBare) {
+ referencedObject.getBare(subfields, function (err, dict) {
+ bareDictionary[i] = dict;
+ iter(null);
+ });
+ } else {
+ bareDictionary[i] = referencedObject;
iter(null);
- });
+ }
} else {
- bareDictionary[i] = wrappedObject[i];
iter(null);
}
}, function () {

0 comments on commit e6a000c

Please sign in to comment.