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 the parent() 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.
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