Permalink
Browse files

Streaming: make sure all streamed objects are wrapped.

  • Loading branch information...
1 parent 2078fe1 commit 04220eef78bf695d78d3faef1d3c2dd01680d554 @JoshuaGross committed Jan 29, 2013
Showing with 26 additions and 2 deletions.
  1. +24 −1 lib/mongoose-subpopulate.js
  2. +1 −1 package.json
  3. +1 −0 test/streams.js
@@ -170,7 +170,6 @@ var wrapSchema = exports.wrapSchema = function wrapSchema (schema) {
schema.save.mSubpopulateWrapped = true;
}
- // Chaining support
if (schema.populate && !schema.populate.mSubpopulateWrapped) {
var populateOrig = schema.populate;
schema.populate = function populateWrapped (path, fields, conditions, options) {
@@ -268,6 +267,30 @@ var wrapSchema = exports.wrapSchema = function wrapSchema (schema) {
schema.exec.mSubpopulateWrapped = true;
}
+ if (schema.stream && !schema.stream.mSubpopulateWrapped) {
+ var streamOrig = schema.stream;
+ schema.stream = function streamWrapped () {
+ var wrappedRes = wrapSchema(streamOrig.apply(schema, arguments));
+ var dataListeners = [];
+ var wrapDataEvents = function (data) {
+ var wrappedData = wrapModelObject(data);
+ dataListeners.map(function (listener) {
+ listener(wrappedData);
+ });
+ };
+ wrappedRes.on('data', wrapDataEvents);
+ var onOrig = wrappedRes.on;
+ wrappedRes.on = function (type, fn) {
+ if (type === 'data') {
+ dataListeners.push(fn);
+ } else {
+ onOrig.call(wrappedRes, type, fn);
+ }
+ };
+ return wrappedRes;
+ };
+ }
+
schema.mSubpopulateWrapped = true;
// Wrap constructor
View
@@ -1,6 +1,6 @@
{
"name": "mongoose-subpopulate",
- "version": "0.1.18",
+ "version": "0.1.19",
"main": "./lib/mongoose-subpopulate.js",
"scripts": {},
"repository": {
View
@@ -32,6 +32,7 @@ describe('mongoose streams', function () {
var usersFound = 0;
stream.on('data', function (user) {
+ expect(typeof user.getBare).to.be('function');
usersFound++;
});
stream.on('close', function () {

0 comments on commit 04220ee

Please sign in to comment.