You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature: Option to disable creation of initial connection #12965
I have searched existing issues to ensure the feature has not already been requested
🚀 Feature Proposal
If you manage multiple custom connections, a typical pattern is to simply create new connections with createConnection(uri, options). Doing so, it adds by default to the mongoose singleton of mongoose.connections, so you can iterate over mongoose.connections to see properties such as readyState.
The default connection however is always present. The only way to delete it (regardless of custom Mongoose instances) is to manually call mongoose.connection.destroy();. This isn't such a good pattern, as a better option might be new Mongoose({ createInitialConnection: false }); or something. Adding this feature would require some refactoring though.
If you create multiple connections, then call .destroy() on them, and then create more connections, multiple of the connections will have the same id property because .length does not check for uniqueness.
Activity
titanism commentedon Jan 31, 2023
This might not actually be too hard, but we could be wrong:
// default global options this.options = Object.assign({ pluralization: true, autoIndex: true, - autoCreate: true + autoCreate: true, + createInitialConnection: false }, options); + + if (this.options.createInitialConnection) { + const conn = this.createConnection(); // default connection + conn.models = this.models; + } - const conn = this.createConnection(); // default connection - conn.models = this.models;titanism commentedon Jan 31, 2023
There might also be another bug here we discovered, whereas
idis not unique.mongoose/lib/connection.js
Lines 72 to 76 in 9d7bf10
If you create multiple connections, then call
.destroy()on them, and then create more connections, multiple of the connections will have the sameidproperty because.lengthdoes not check for uniqueness.cc @vkarpov15
titanism commentedon Jan 31, 2023
Confirmed this is a bug!
You can see
3appears twice.feat(index): added createInitialConnection option to Mongoose constru…
Merge pull request #13021 from lpizzinidev/gh-12965
feat(index): added createInitialConnection option to Mongoose constru…
[Snyk] Upgrade mongoose from 6.8.0 to 6.10.2 (#6)