File tree Expand file tree Collapse file tree 2 files changed +29
-4
lines changed Expand file tree Collapse file tree 2 files changed +29
-4
lines changed Original file line number Diff line number Diff line change @@ -1103,16 +1103,28 @@ Model.init = function init() {
11031103 return results ;
11041104 } ;
11051105 const _createCollection = async ( ) => {
1106- await conn . _waitForConnect ( ) ;
1107- const autoCreate = utils . getOption (
1106+ let autoCreate = utils . getOption (
11081107 'autoCreate' ,
11091108 this . schema . options ,
1110- conn . config ,
1111- conn . base . options
1109+ conn . config
1110+ // No base.options here because we don't want to take the base value if the connection hasn't
1111+ // set it yet
11121112 ) ;
1113+ if ( autoCreate == null ) {
1114+ // `autoCreate` may later be set when the connection is opened, so wait for connect before checking
1115+ await conn . _waitForConnect ( ) ;
1116+ autoCreate = utils . getOption (
1117+ 'autoCreate' ,
1118+ this . schema . options ,
1119+ conn . config ,
1120+ conn . base . options
1121+ ) ;
1122+ }
1123+
11131124 if ( ! autoCreate ) {
11141125 return ;
11151126 }
1127+
11161128 return await this . createCollection ( ) ;
11171129 } ;
11181130
Original file line number Diff line number Diff line change @@ -1637,6 +1637,19 @@ describe('connections:', function() {
16371637 assert . ok ( ! res . map ( c => c . name ) . includes ( 'gh12940_Conn' ) ) ;
16381638 } ) ;
16391639
1640+ it ( 'does not wait for buffering if autoCreate: false (gh-15241)' , async function ( ) {
1641+ const m = new mongoose . Mongoose ( ) ;
1642+ m . set ( 'bufferTimeoutMS' , 100 ) ;
1643+
1644+ const schema = new Schema ( { name : String } , {
1645+ autoCreate : false
1646+ } ) ;
1647+ const Model = m . model ( 'gh15241_Conn' , schema ) ;
1648+
1649+ // Without gh-15241 changes, this would buffer and fail even though `autoCreate: false`
1650+ await Model . init ( ) ;
1651+ } ) ;
1652+
16401653 it ( 'should not create default connection with createInitialConnection = false (gh-12965)' , function ( ) {
16411654 const m = new mongoose . Mongoose ( {
16421655 createInitialConnection : false
You can’t perform that action at this time.
0 commit comments