Skip to content

Commit

Permalink
feat(index): added createInitialConnection option to Mongoose constru…
Browse files Browse the repository at this point in the history
…ctor

closes #12965
  • Loading branch information
lpizzinidev authored and vkarpov15 committed Feb 22, 2023
1 parent 2dbd4aa commit c3c133b
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 @@ -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) ?? true;
if (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 @@ -1545,4 +1545,11 @@ describe('connections:', function() {
const res = await m.connection.db.listCollections().toArray();
assert.ok(!res.map(c => c.name).includes('gh12940_Conn'));
});

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 c3c133b

Please sign in to comment.