DouglasGabr / mongoose-lean-defaults

Attach defaults to the results of mongoose queries when using .lean()
https://www.npmjs.com/package/mongoose-lean-defaults
ISC License
15 stars 12 forks source link

Breaking change makes v2.1.1 incompatible with Mongoose v5, but still claims compatibility #27

Closed carrolp closed 2 years ago

carrolp commented 2 years ago

Describe the bug

This change introduces a dependency on mongoose.Schema.Types.Subdocument, but Subdocument is not added to mongoose.Schema.Types until Mongoose v6. But mongoose-lean-defaults still states that its peerDependency is mongoose >=5.

As a result, when used with mongoose v5, an error like this will occur:

"err":{"message":"Right-hand side of 'instanceof' is not an object","name":"TypeError","stack":"TypeError: Right-hand side of 'instanceof' is not an object\n    at getDefault ([MYPROJECT]/node_modules/mongoose-lean-defaults/index.js:140:25)....

Mongoose 5.13.14: https://github.com/Automattic/mongoose/blob/5.13.14/lib/schema/index.js (no Subdocument) Mongoose 6.0.0: https://github.com/Automattic/mongoose/blob/6.0.0/lib/schema/index.js#L16 (has Subdocument)

If the dependency on Subdocument being defined is necessary, then I would think the peerDependency needs to be updated to mongoose >=6. As that would be a breaking change (no longer compatible with v5), the mongoose-leand-defaults version should be bumped to 3.0.0 instead of 2.1.x.

We are updating our project to explicitly depend on mongoose-lean-defaults at version 2.1.0 (not ^2.1.0) to avoid this breakage for now.

To Reproduce
Steps to reproduce the behavior:

  1. Use mongoose-lean-defaults with a project using Mongoose v5

Expected behavior
mongoose-lean-defaults 2.x should continue to support Mongoose v5

Additional context
Mongoose version: 5.13.14 Node version: 14, 16

DouglasGabr commented 2 years ago

@carrolp Thanks for reporting this issue. I'll mark v2.1.1 as deprecated because it introduces this unexpected breaking change. I'll double check if there is some way to keep the same logic without relying on mongoose.Schema.Types.Subdocument.

Any help is appreciated, if you want to contribute with a pull request.