@@ -36,6 +36,7 @@ const shardingPlugin = require('./plugins/sharding');
3636const trusted = require ( './helpers/query/trusted' ) . trusted ;
3737const sanitizeFilter = require ( './helpers/query/sanitizeFilter' ) ;
3838const isBsonType = require ( './helpers/isBsonType' ) ;
39+ const MongooseError = require ( './error/mongooseError' ) ;
3940
4041const defaultMongooseSymbol = Symbol . for ( 'mongoose:default' ) ;
4142
@@ -63,6 +64,7 @@ function Mongoose(options) {
6364 this . connections = [ ] ;
6465 this . models = { } ;
6566 this . events = new EventEmitter ( ) ;
67+ this . __driver = driver . get ( ) ;
6668 // default global options
6769 this . options = Object . assign ( {
6870 pluralization : true ,
@@ -136,13 +138,44 @@ Mongoose.prototype.ConnectionStates = STATES;
136138 * uses to communicate with the database. A driver is a Mongoose-specific interface that defines functions
137139 * like `find()`.
138140 *
141+ * @deprecated
139142 * @memberOf Mongoose
140143 * @property driver
141144 * @api public
142145 */
143146
144147Mongoose . prototype . driver = driver ;
145148
149+ /**
150+ * Overwrites the current driver used by this Mongoose instance. A driver is a
151+ * Mongoose-specific interface that defines functions like `find()`.
152+ *
153+ * @memberOf Mongoose
154+ * @method setDriver
155+ * @api public
156+ */
157+
158+ Mongoose . prototype . setDriver = function setDriver ( driver ) {
159+ const _mongoose = this instanceof Mongoose ? this : mongoose ;
160+
161+ if ( _mongoose . __driver === driver ) {
162+ return _mongoose ;
163+ }
164+
165+ const openConnection = _mongoose . connections && _mongoose . connections . find ( conn => conn . readyState !== STATES . disconnected ) ;
166+ if ( openConnection ) {
167+ const msg = 'Cannot modify Mongoose driver if a connection is already open. ' +
168+ 'Call `mongoose.disconnect()` before modifying the driver' ;
169+ throw new MongooseError ( msg ) ;
170+ }
171+ _mongoose . __driver = driver ;
172+
173+ const Connection = driver . getConnection ( ) ;
174+ _mongoose . connections = [ new Connection ( _mongoose ) ] ;
175+
176+ return _mongoose ;
177+ } ;
178+
146179/**
147180 * Sets mongoose options
148181 *
@@ -279,7 +312,7 @@ Mongoose.prototype.get = Mongoose.prototype.set;
279312Mongoose . prototype . createConnection = function ( uri , options , callback ) {
280313 const _mongoose = this instanceof Mongoose ? this : mongoose ;
281314
282- const Connection = driver . get ( ) . getConnection ( ) ;
315+ const Connection = _mongoose . __driver . getConnection ( ) ;
283316 const conn = new Connection ( _mongoose ) ;
284317 if ( typeof options === 'function' ) {
285318 callback = options ;
@@ -475,7 +508,6 @@ Mongoose.prototype.pluralize = function(fn) {
475508 */
476509
477510Mongoose . prototype . model = function ( name , schema , collection , options ) {
478-
479511 const _mongoose = this instanceof Mongoose ? this : mongoose ;
480512
481513 if ( typeof schema === 'string' ) {
@@ -692,7 +724,7 @@ Mongoose.prototype.__defineGetter__('connection', function() {
692724} ) ;
693725
694726Mongoose . prototype . __defineSetter__ ( 'connection' , function ( v ) {
695- if ( v instanceof Connection ) {
727+ if ( v instanceof this . __driver . getConnection ( ) ) {
696728 this . connections [ 0 ] = v ;
697729 this . models = v . models ;
698730 }
@@ -721,18 +753,6 @@ Mongoose.prototype.__defineSetter__('connection', function(v) {
721753
722754Mongoose . prototype . connections ;
723755
724- /*!
725- * Connection
726- */
727-
728- const Connection = driver . get ( ) . getConnection ( ) ;
729-
730- /*!
731- * Collection
732- */
733-
734- const Collection = driver . get ( ) . Collection ;
735-
736756/**
737757 * The Mongoose Aggregate constructor
738758 *
@@ -749,7 +769,14 @@ Mongoose.prototype.Aggregate = Aggregate;
749769 * @api public
750770 */
751771
752- Mongoose . prototype . Collection = Collection ;
772+ Object . defineProperty ( Mongoose . prototype , 'Collection' , {
773+ get : function ( ) {
774+ return this . __driver . Collection ;
775+ } ,
776+ set : function ( Collection ) {
777+ this . __driver . Collection = Collection ;
778+ }
779+ } ) ;
753780
754781/**
755782 * The Mongoose [Connection](#connection_Connection) constructor
@@ -760,7 +787,18 @@ Mongoose.prototype.Collection = Collection;
760787 * @api public
761788 */
762789
763- Mongoose . prototype . Connection = Connection ;
790+ Object . defineProperty ( Mongoose . prototype , 'Connection' , {
791+ get : function ( ) {
792+ return this . __driver . getConnection ( ) ;
793+ } ,
794+ set : function ( Connection ) {
795+ if ( Connection === this . __driver . getConnection ( ) ) {
796+ return ;
797+ }
798+
799+ this . __driver . getConnection = ( ) => Connection ;
800+ }
801+ } ) ;
764802
765803/**
766804 * The Mongoose version
0 commit comments