@@ -35,6 +35,7 @@ const shardingPlugin = require('./plugins/sharding');
3535const trusted = require ( './helpers/query/trusted' ) . trusted ;
3636const sanitizeFilter = require ( './helpers/query/sanitizeFilter' ) ;
3737const isBsonType = require ( './helpers/isBsonType' ) ;
38+ const MongooseError = require ( './error/mongooseError' ) ;
3839
3940const defaultMongooseSymbol = Symbol . for ( 'mongoose:default' ) ;
4041
@@ -62,6 +63,7 @@ function Mongoose(options) {
6263 this . connections = [ ] ;
6364 this . models = { } ;
6465 this . events = new EventEmitter ( ) ;
66+ this . __driver = driver . get ( ) ;
6567 // default global options
6668 this . options = Object . assign ( {
6769 pluralization : true ,
@@ -135,13 +137,44 @@ Mongoose.prototype.ConnectionStates = STATES;
135137 * uses to communicate with the database. A driver is a Mongoose-specific interface that defines functions
136138 * like `find()`.
137139 *
140+ * @deprecated
138141 * @memberOf Mongoose
139142 * @property driver
140143 * @api public
141144 */
142145
143146Mongoose . prototype . driver = driver ;
144147
148+ /**
149+ * Overwrites the current driver used by this Mongoose instance. A driver is a
150+ * Mongoose-specific interface that defines functions like `find()`.
151+ *
152+ * @memberOf Mongoose
153+ * @method setDriver
154+ * @api public
155+ */
156+
157+ Mongoose . prototype . setDriver = function setDriver ( driver ) {
158+ const _mongoose = this instanceof Mongoose ? this : mongoose ;
159+
160+ if ( _mongoose . __driver === driver ) {
161+ return _mongoose ;
162+ }
163+
164+ const openConnection = _mongoose . connections && _mongoose . connections . find ( conn => conn . readyState !== STATES . disconnected ) ;
165+ if ( openConnection ) {
166+ const msg = 'Cannot modify Mongoose driver if a connection is already open. ' +
167+ 'Call `mongoose.disconnect()` before modifying the driver' ;
168+ throw new MongooseError ( msg ) ;
169+ }
170+ _mongoose . __driver = driver ;
171+
172+ const Connection = driver . getConnection ( ) ;
173+ _mongoose . connections = [ new Connection ( _mongoose ) ] ;
174+
175+ return _mongoose ;
176+ } ;
177+
145178/**
146179 * Sets mongoose options
147180 *
@@ -278,7 +311,7 @@ Mongoose.prototype.get = Mongoose.prototype.set;
278311Mongoose . prototype . createConnection = function ( uri , options , callback ) {
279312 const _mongoose = this instanceof Mongoose ? this : mongoose ;
280313
281- const Connection = driver . get ( ) . getConnection ( ) ;
314+ const Connection = _mongoose . __driver . getConnection ( ) ;
282315 const conn = new Connection ( _mongoose ) ;
283316 if ( typeof options === 'function' ) {
284317 callback = options ;
@@ -474,7 +507,6 @@ Mongoose.prototype.pluralize = function(fn) {
474507 */
475508
476509Mongoose . prototype . model = function ( name , schema , collection , options ) {
477-
478510 const _mongoose = this instanceof Mongoose ? this : mongoose ;
479511
480512 if ( typeof schema === 'string' ) {
@@ -691,7 +723,7 @@ Mongoose.prototype.__defineGetter__('connection', function() {
691723} ) ;
692724
693725Mongoose . prototype . __defineSetter__ ( 'connection' , function ( v ) {
694- if ( v instanceof Connection ) {
726+ if ( v instanceof this . __driver . getConnection ( ) ) {
695727 this . connections [ 0 ] = v ;
696728 this . models = v . models ;
697729 }
@@ -720,18 +752,6 @@ Mongoose.prototype.__defineSetter__('connection', function(v) {
720752
721753Mongoose . prototype . connections ;
722754
723- /*!
724- * Connection
725- */
726-
727- const Connection = driver . get ( ) . getConnection ( ) ;
728-
729- /*!
730- * Collection
731- */
732-
733- const Collection = driver . get ( ) . Collection ;
734-
735755/**
736756 * The Mongoose Aggregate constructor
737757 *
@@ -748,7 +768,14 @@ Mongoose.prototype.Aggregate = Aggregate;
748768 * @api public
749769 */
750770
751- Mongoose . prototype . Collection = Collection ;
771+ Object . defineProperty ( Mongoose . prototype , 'Collection' , {
772+ get : function ( ) {
773+ return this . __driver . Collection ;
774+ } ,
775+ set : function ( Collection ) {
776+ this . __driver . Collection = Collection ;
777+ }
778+ } ) ;
752779
753780/**
754781 * The Mongoose [Connection](#connection_Connection) constructor
@@ -759,7 +786,18 @@ Mongoose.prototype.Collection = Collection;
759786 * @api public
760787 */
761788
762- Mongoose . prototype . Connection = Connection ;
789+ Object . defineProperty ( Mongoose . prototype , 'Connection' , {
790+ get : function ( ) {
791+ return this . __driver . getConnection ( ) ;
792+ } ,
793+ set : function ( Connection ) {
794+ if ( Connection === this . __driver . getConnection ( ) ) {
795+ return ;
796+ }
797+
798+ this . __driver . getConnection = ( ) => Connection ;
799+ }
800+ } ) ;
763801
764802/**
765803 * The Mongoose version
0 commit comments