Skip to content

Commit

Permalink
1) adding 'sort' to mongoose-subpopulate chaining 2) getBare method
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaGross committed Oct 17, 2012
1 parent a7502d6 commit ed234c6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
46 changes: 30 additions & 16 deletions lib/mongoose-subpopulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,25 +161,24 @@ var wrapSchema = exports.wrapSchema = function wrapSchema (schema) {
schema.populate = function populateWrapped (path, fields, conditions, options) {
schema.populateStack.push(path);
verifyPath(path);
try {
return wrapSchema(populateOrig.call(schema, path, fields, conditions, options));
} catch (mongooseError) {
wrapCallback(mongooseError);
}
return wrapSchema(populateOrig.call(schema, path, fields, conditions, options));
};
schema.populate.mSubpopulateWrapped = true;
}
if (schema.sort && !schema.sort.mSubpopulateWrapped) {
var sortOrig = schema.sort;
schema.sort = function sortWrapped (sort) {
try {
return wrapSchema(sortOrig.call(schema, sort));
} catch (mongooseError) {
wrapCallback(mongooseError);
}
return wrapSchema(sortOrig.call(schema, sort));
};
schema.populate.mSubpopulateWrapped = true;
}
if (schema.select && !schema.select.mSubpopulateWrapped) {
var selectOrig = schema.select;
schema.select = function selectWrapped (selectString) {
return wrapSchema(selectOrig.call(schema, selectString));
};
schema.select.mSubpopulateWrapped = true;
}
if (schema.exec && !schema.exec.mSubpopulateWrapped) {
var execOrig = schema.exec;
schema.exec = function execWrapped (callback) {
Expand Down Expand Up @@ -283,6 +282,7 @@ function getCallerDetails () {
// Here we wrap individual data objects to abstract them in a few nice ways:
// 1) Directly access the _id attribute as a string
// 2) Get and set attributes of the object; all changes are synced immediately with the mongoose link
// 3) Get a bare dictionary of the model and any populated descendants
function wrapModelObject (object) {
if (object === null || object === undefined) {
return object;
Expand Down Expand Up @@ -315,10 +315,6 @@ function wrapModelObject (object) {
} else {
(function (attr) {
wrappedObject.__defineGetter__(attr, function () {
// For debugging purposes: see which attributes are attempting to be accessed
if (!/(\/mongoose\/|util\.js|\/mongoose-subpopulate\/)/.test(getCallerDetails().file)) {
console.log('MONGOOSE-SUBPOPULATE WARNING: attempting to access ', attr, getCallerDetails());
}
return object[attr];
});
})(i);
Expand All @@ -328,10 +324,9 @@ function wrapModelObject (object) {
for (var i in schema) {
(function (attr) {
wrappedObject.__defineGetter__(attr, function () {

// If our caller is in a Mongoose library, then just return the object attribute directly -
// we don't want to screw with Mongoose internals too much
if (getCallerDetails().file.match(/\/mongoose\//)) {
if (getCallerDetails().file.indexOf('/mongoose/') !== -1) {
return object[attr];
}

Expand Down Expand Up @@ -369,6 +364,25 @@ function wrapModelObject (object) {
})(i);
}

wrappedObject.getBare = function (callback) {
var 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;
iter(null);
});
} else {
bareDictionary[i] = wrappedObject[i];
iter(null);
}
}, function () {
callback(null, bareDictionary);
});
};

return wrappedObject;
}
exports.extendMongoose = function (mongooseIn, defineModels) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongoose-subpopulate",
"version": "0.1.3",
"version": "0.1.4",
"main": "./lib/mongoose-subpopulate.js",
"scripts": {},
"repository": {
Expand Down

0 comments on commit ed234c6

Please sign in to comment.