mongoosejs / mongoose-lean-virtuals

Attach virtuals to the results of mongoose lean() queries
Apache License 2.0
45 stars 24 forks source link

fix: allow calling `parent()` on parent, allow accessing parent's virtual from child subdoc #73

Closed vkarpov15 closed 3 months ago

vkarpov15 commented 5 months ago

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 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.

Examples