Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 4 additions & 3 deletions lib/cursor/queryCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ function QueryCursor(query) {
if (this.options.transform) {
this._transforms.push(this.options.transform);
}
if (this._mongooseOptions._transformForAsyncIterator) {
this._transforms.push(_transformForAsyncIterator);
}
// Re: gh-8039, you need to set the `cursor.batchSize` option, top-level
// `batchSize` option doesn't work.
if (this.options.batchSize) {
Expand Down Expand Up @@ -396,9 +399,7 @@ QueryCursor.prototype.transformNull = function(val) {
*/

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

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() ?? null).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