fix: allow calling parent()
on parent, allow accessing parent's virtual from child subdoc
#73
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix #65
Fix #64
Summary
#64 points out that, because mongoose-lean-virtual applies virtuals to child subdocs before their parents, that makes it tricky to use parent virtuals in child subdocs. This change was intentional with #28, and we can't change the subdoc traversal order without a backwards breaking change. But, given that
parent()
is the only way to get a subdocument's parent, mongoose-lean-virtuals can apply virtuals in theparent()
call to ensure the child subdoc gets the parent doc with virtuals applied.This can create issues if a subdocument calls
parent()
, and then the parent tries to access one of the subdocument's virtuals. The subdocument virtual won't exist yet. But this case is hopefully unlikely.I also bumped ESLint, fixed some lint warnings, added
parent()
to TypeScript type defs, and fixed #64 (also #66) because that's just a one-line fix.Examples