File tree Expand file tree Collapse file tree 2 files changed +17
-3
lines changed Expand file tree Collapse file tree 2 files changed +17
-3
lines changed Original file line number Diff line number Diff line change @@ -77,6 +77,9 @@ function QueryCursor(query) {
77
77
if ( this . options . transform ) {
78
78
this . _transforms . push ( this . options . transform ) ;
79
79
}
80
+ if ( this . _mongooseOptions . _transformForAsyncIterator ) {
81
+ this . _transforms . push ( _transformForAsyncIterator ) ;
82
+ }
80
83
// Re: gh-8039, you need to set the `cursor.batchSize` option, top-level
81
84
// `batchSize` option doesn't work.
82
85
if ( this . options . batchSize ) {
@@ -396,9 +399,7 @@ QueryCursor.prototype.transformNull = function(val) {
396
399
*/
397
400
398
401
QueryCursor . prototype . _transformForAsyncIterator = function ( ) {
399
- if ( this . _transforms . indexOf ( _transformForAsyncIterator ) === - 1 ) {
400
- this . map ( _transformForAsyncIterator ) ;
401
- }
402
+ this . _mongooseOptions . _transformForAsyncIterator = true ;
402
403
return this ;
403
404
} ;
404
405
Original file line number Diff line number Diff line change @@ -2354,6 +2354,19 @@ describe('Query', function() {
2354
2354
assert . strictEqual ( called , 1 ) ;
2355
2355
} ) ;
2356
2356
2357
+ it ( 'transform with for/await and cursor' , async function ( ) {
2358
+ const Model = db . model ( 'Test' , new Schema ( { name : String } ) ) ;
2359
+
2360
+ await Model . create ( { name : 'test' } ) ;
2361
+ const cursor = Model . find ( ) . transform ( doc => doc ?. name . toUpperCase ( ) ?? null ) . cursor ( ) ;
2362
+ const names = [ ] ;
2363
+ for await ( const name of cursor ) {
2364
+ names . push ( name ) ;
2365
+ }
2366
+
2367
+ assert . deepStrictEqual ( names , [ 'TEST' ] ) ;
2368
+ } ) ;
2369
+
2357
2370
describe ( 'orFail (gh-6841)' , function ( ) {
2358
2371
let Model ;
2359
2372
You can’t perform that action at this time.
0 commit comments