File tree 2 files changed +29
-4
lines changed
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() {
1103
1103
return results ;
1104
1104
} ;
1105
1105
const _createCollection = async ( ) => {
1106
- await conn . _waitForConnect ( ) ;
1107
- const autoCreate = utils . getOption (
1106
+ let autoCreate = utils . getOption (
1108
1107
'autoCreate' ,
1109
1108
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
1112
1112
) ;
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
+
1113
1124
if ( ! autoCreate ) {
1114
1125
return ;
1115
1126
}
1127
+
1116
1128
return await this . createCollection ( ) ;
1117
1129
} ;
1118
1130
Original file line number Diff line number Diff line change @@ -1637,6 +1637,19 @@ describe('connections:', function() {
1637
1637
assert . ok ( ! res . map ( c => c . name ) . includes ( 'gh12940_Conn' ) ) ;
1638
1638
} ) ;
1639
1639
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
+
1640
1653
it ( 'should not create default connection with createInitialConnection = false (gh-12965)' , function ( ) {
1641
1654
const m = new mongoose . Mongoose ( {
1642
1655
createInitialConnection : false
You can’t perform that action at this time.
0 commit comments