Skip to content

Commit

Permalink
Merge pull request #13021 from lpizzinidev/gh-12965
Browse files Browse the repository at this point in the history
feat(index): added createInitialConnection option to Mongoose constructor
  • Loading branch information
vkarpov15 committed Feb 13, 2023
2 parents 9a89506 + 92f5924 commit 4b71e8e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,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 @@ -1525,4 +1525,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

0 comments on commit 4b71e8e

Please sign in to comment.