-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
feat: re-export default Mongoose instance properties for ESM named imports support #12256
Conversation
…ports support Fix #12148
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One minor comment, otherwise LGTM. Thanks!
index.js
Outdated
module.exports.deleteModel = mongoose.deleteModel; | ||
module.exports.modelNames = mongoose.modelNames; | ||
module.exports.plugin = mongoose.plugin; | ||
// module.exports.connection = mongoose.connection; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not export the default connection, is it also because of "custom driver may overwrite this"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes exactly. setDriver()
can recreate the default connection.
…t re-exported for Node ESM re: #12148
Fix #12148
Summary
I did some reading in https://github.com/nodejs/node/pull/35249/files and https://simonplend.com/node-js-now-supports-named-imports-from-commonjs-modules-but-what-does-that-mean/, it looks like all we need to do to support ESM named imports in Node is to explicitly re-export all the properties on the Mongoose global.
This does come with some caveats. Most notably that
this
will no longer be an instance ofMongoose
in all exported functions by default. This shouldn't break anything - we added support for destructured imports in a73c2ab. But it is still a risk.I'm also not exporting
Connection
andCollection
, because those are custom getters and overwriting those can break support for custom drivers.Examples