Skip to content
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

Fix dynamic virtual populate on sub-documents #12440

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Hybrid-Force
Copy link

Fix #12363

Summary
Apply dynamic ref getter of virtual population on sub-documents instead of the parent document.

Mongoose supports dynamic virtual populate via ref and refPath options. However, the ref function is called on the parent document, and refPath is relative to the parent document. It does not make much sense for dynamic virtual populates defined on nested schemas, because the child schema has no knowledge of the parent schema.

After this patch, ref is called on sub-document, and refPath is relative to sub-document instead of the parent document.

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a closer look and the distinction that refPath is relative to the top-level document rather than the subdocument is intentional. Paraphrasing from this comment: if refPath is relative to the subdocument, it is impossible to make refPath reference a field outside of the subdocument (like a top-level field) without introducing new syntax. Whereas with ref(), you can always call this.ownerDocument() to get the top-level document.

I do like the fix that ref() on virtuals is now called on the subdocument rather than the top-level document. But refPath should still be relative to top-level document. Is that possible?

Also, it looks like these changes only affect virtual populate. So it looks like refPath is still relative to top-level document for conventional populate.

@Hybrid-Force
Copy link
Author

I do like the fix that ref() on virtuals is now called on the subdocument rather than the top-level document. But refPath should still be relative to top-level document. Is that possible?

Sure, I will update the patch.

@hasezoey
Copy link
Collaborator

seeing as this PR has been open for quite some time with no activity and changes were requested, should this PR be closed?

(from what it looks like this PR was targeting ~6.7, and now mongoose is at 8)

@hasezoey hasezoey added the Stale label Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dynamic virtual populate not working on subdocument with separate schema
3 participants