Closed titanism closed 1 year ago
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;
There might also be another bug here we discovered, whereas id
is not unique.
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.
cc @vkarpov15
Confirmed this is a bug!
const mongoose = require('mongoose');
mongoose.createConnection()
mongoose.createConnection()
mongoose.connections[0].destroy()
mongoose.createConnection()
mongoose.createConnection()
mongoose.createConnection()
mongoose.connections.map(c => c.id) // outputs: [ 1, 2, 3, 3, 4 ]
You can see 3
appears twice.
Prerequisites
🚀 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 ofmongoose.connections
, so you can iterate overmongoose.connections
to see properties such asreadyState
.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 benew Mongoose({ createInitialConnection: false });
or something. Adding this feature would require some refactoring though.