Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 31 additions & 40 deletions lib/cursor/queryCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,29 +379,6 @@ QueryCursor.prototype.addCursorFlag = function(flag, value) {
return this;
};

/*!
* ignore
*/

QueryCursor.prototype.transformNull = function(val) {
if (arguments.length === 0) {
val = true;
}
this._mongooseOptions.transformNull = val;
return this;
};

/*!
* ignore
*/

QueryCursor.prototype._transformForAsyncIterator = function() {
if (this._transforms.indexOf(_transformForAsyncIterator) === -1) {
this.map(_transformForAsyncIterator);
}
return this;
};

/**
* Returns an asyncIterator for use with [`for/await/of` loops](https://thecodebarbarian.com/getting-started-with-async-iterators-in-node-js).
* You do not need to call this function explicitly, the JavaScript runtime
Expand Down Expand Up @@ -434,18 +411,11 @@ QueryCursor.prototype._transformForAsyncIterator = function() {

if (Symbol.asyncIterator != null) {
QueryCursor.prototype[Symbol.asyncIterator] = function() {
return this.transformNull()._transformForAsyncIterator();
this._mongooseOptions._asyncIterator = true;
return this;
};
}

/*!
* ignore
*/

function _transformForAsyncIterator(doc) {
return doc == null ? { done: true } : { value: doc, done: false };
}

/**
* Get the next doc from the underlying cursor and mongooseify it
* (populate, etc.)
Expand All @@ -456,16 +426,37 @@ function _transformForAsyncIterator(doc) {

function _next(ctx, cb) {
let callback = cb;
if (ctx._transforms.length) {
callback = function(err, doc) {
if (err || (doc === null && !ctx._mongooseOptions.transformNull)) {
return cb(err, doc);

// Create a custom callback to handle transforms, async iterator, and transformNull
callback = function(err, doc) {
if (err) {
return cb(err);
}

// Handle null documents - if asyncIterator, we need `done: true`, otherwise just
// skip. In either case, avoid transforms.
if (doc === null) {
if (ctx._mongooseOptions._asyncIterator) {
return cb(null, { done: true });
} else {
return cb(null, null);
}
cb(err, ctx._transforms.reduce(function(doc, fn) {
}

// Apply transforms
if (ctx._transforms.length && doc !== null) {
doc = ctx._transforms.reduce(function(doc, fn) {
return fn.call(ctx, doc);
}, doc));
};
}
}, doc);
}

// For async iterator, convert to {value, done} format
if (ctx._mongooseOptions._asyncIterator) {
return cb(null, { value: doc, done: false });
}

return cb(null, doc);
};

if (ctx._error) {
return immediate(function() {
Expand Down
13 changes: 13 additions & 0 deletions test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2354,6 +2354,19 @@ describe('Query', function() {
assert.strictEqual(called, 1);
});

it('transform with for/await and cursor', async function() {
const Model = db.model('Test', new Schema({ name: String }));

await Model.create({ name: 'test' });
const cursor = Model.find().transform(doc => doc.name.toUpperCase()).cursor();
const names = [];
for await (const name of cursor) {
names.push(name);
}

assert.deepStrictEqual(names, ['TEST']);
});

describe('orFail (gh-6841)', function() {
let Model;

Expand Down
Loading