Skip to content
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(index): added createInitialConnection option to Mongoose constructor #13021

Merged
merged 2 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ function Mongoose(options) {
autoIndex: true,
autoCreate: true
}, options);
const conn = this.createConnection(); // default connection
conn.models = this.models;
const createInitialConnection = utils.getOption('createInitialConnection', this.options);
if (createInitialConnection == null || createInitialConnection) {
const conn = this.createConnection(); // default connection
conn.models = this.models;
}

if (this.options.pluralization) {
this._pluralize = legacyPluralize;
Expand Down
7 changes: 7 additions & 0 deletions test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1526,4 +1526,11 @@ describe('connections:', function() {
const connectionIds = m.connections.map(c => c.id);
assert.deepEqual(connectionIds, [1, 2, 3, 4, 5]);
});

it('should not create default connection with createInitialConnection = false (gh-12965)', function() {
const m = new mongoose.Mongoose({
createInitialConnection: false
});
assert.deepEqual(m.connections.length, 0);
});
});
7 changes: 7 additions & 0 deletions types/mongooseoptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ declare module 'mongoose' {
*/
cloneSchemas?: boolean;

/**
* Set to `false` to disable the creation of the initial default connection.
*
* @default true
*/
createInitialConnection?: boolean;

/**
* If `true`, prints the operations mongoose sends to MongoDB to the console.
* If a writable stream is passed, it will log to that stream, without colorization.
Expand Down