-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
fix(queryCursor): add _transformForAsyncIterator transform after user-defined transforms #15430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes an issue with async iterator transforms by ensuring user-defined transforms are applied before the internal _transformForAsyncIterator.
- Reorders the addition of transforms in QueryCursor so that user transforms are pushed before the async iterator transform.
- Adds a new test case in query.test.js to verify correct transform handling with for/await loops.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
File | Description |
---|---|
test/query.test.js | Adds a test to check that user-specified transforms work correctly in async iteration |
lib/cursor/queryCursor.js | Reorders and flags the internal async iterator transform to run after user-specified transforms |
…rt and avoid calling transforms on null with async iterator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes an issue with user-defined transforms in async iterators by applying them before the internal async iterator transform.
- Updates the async iterator implementation in queryCursor.js by removing transformNull and adjusting transform order.
- Adds a test case to ensure that transforms work correctly with async iteration.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
test/query.test.js | New test added for async iterator transform with user-defined transform. |
lib/cursor/queryCursor.js | Removed transformNull and updated async iterator implementation to flag async iteration. |
@hasezoey @AbdelrahmanHafez I changed my implementation to not rely on transforms, please take another look 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, though some other parts need to be updated to fix the tests.
Also somewhat related, i noticed that asyncIterator
is conditionally assigned because of old nodejs versions (<10.0), should we maybe remove that condition for 9.x to reduce the conditions?
(If the comments are anything to go by, then it could be done for 8.x, though just for accidental breakage i would recommend for 9.x)
Fix mongoosejs/mongoose-lean-getters#44
Summary
mongoosejs/mongoose-lean-getters#44 (comment) pointed out an issue which is caused by how we handle async iterators in queryCursor.js. Query cursors use a
_transformForAsyncIterator
transform which convertsdoc
to{ value: doc, done: false }
or{ done: true }
depending on whether the cursor returned null. Because transforms pass the transformed value to the next transform,_transformForAsyncIterator
breaks user-specified transforms if it gets added first.This PR makes it so that user-specified transforms are applied before
_transformForAsyncIterator
, which means user-specified transforms won't see the{ value: doc, done: false }
doc.However, I want to do some further work to check if we can make
_transformForAsyncIterator
not rely on transforms.transformNull()
also adds a weird edge case for user-specified transforms where user-specified transforms need to handlenull
if using cursors. Which is why the test added in this PR hasdoc?.name.toUpperCase() ?? null
.Examples