Skip to content

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

Merged
merged 3 commits into from
May 25, 2025

Conversation

vkarpov15
Copy link
Collaborator

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 converts doc 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 handle null if using cursors. Which is why the test added in this PR has doc?.name.toUpperCase() ?? null.

Examples

Copy link
Contributor

@Copilot Copilot AI left a 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
Copy link
Contributor

@Copilot Copilot AI left a 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.

@vkarpov15
Copy link
Collaborator Author

@hasezoey @AbdelrahmanHafez I changed my implementation to not rely on transforms, please take another look 👍

Copy link
Collaborator

@hasezoey hasezoey left a 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)

@vkarpov15 vkarpov15 merged commit 49bad32 into master May 25, 2025
71 checks passed
@hasezoey hasezoey deleted the vkarpov15/mongoose-lean-getters-44 branch May 26, 2025 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

lean getters does not work on generative functions
3 participants