Skip to content

Commit

Permalink
refactor: use driver.js singleton rather than global.$MongooseDriver
Browse files Browse the repository at this point in the history
Re: #6705
  • Loading branch information
vkarpov15 committed Jul 28, 2018
1 parent 451c50e commit 836eb53
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ test/model.discriminator.test.js
tools/
test/es6/
test/files/
dist/
2 changes: 1 addition & 1 deletion lib/browser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env browser */

global.$MongooseDriver = require('./drivers/browser');
require('./driver').set(require('./drivers/browser'));

var DocumentProvider = require('./document_provider.js');
var PromiseProvider = require('./promise_provider');
Expand Down
2 changes: 1 addition & 1 deletion lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

const EventEmitter = require('events').EventEmitter;
const Schema = require('./schema');
const Collection = global.$MongooseDriver.Collection;
const Collection = require('./driver').get().Collection;
const STATES = require('./connectionstate');
const MongooseError = require('./error');
const PromiseProvider = require('./promise_provider');
Expand Down
15 changes: 15 additions & 0 deletions lib/driver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

/*!
* ignore
*/

let driver = null;

module.exports.get = function() {
return driver;
};

module.exports.set = function(v) {
driver = v;
};
4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
* Module dependencies.
*/

global.$MongooseDriver = require(global.MONGOOSE_DRIVER_PATH || './drivers/node-mongodb-native');
const driverPath = global.MONGOOSE_DRIVER_PATH ||
'./drivers/node-mongodb-native';
require('./driver').set(require(driverPath));

const Schema = require('./schema');
const SchemaType = require('./schematype');
Expand Down
2 changes: 1 addition & 1 deletion lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const CastError = require('./error/cast');
const ObjectParameterError = require('./error/objectParameter');
const QueryCursor = require('./cursor/QueryCursor');
const ReadPreference = global.$MongooseDriver.ReadPreference;
const ReadPreference = require('./driver').get().ReadPreference;
const applyWriteConcern = require('./helpers/schema/applyWriteConcern');
const cast = require('./cast');
const castUpdate = require('./helpers/query/castUpdate');
Expand Down
2 changes: 1 addition & 1 deletion lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const VirtualType = require('./virtualtype');
const get = require('lodash.get');
const getIndexes = require('./helpers/schema/getIndexes');
const mpath = require('mpath');
const readPref = global.$MongooseDriver.ReadPreference;
const readPref = require('./driver').get().ReadPreference;
const utils = require('./utils');

let MongooseTypes;
Expand Down
2 changes: 1 addition & 1 deletion lib/types/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Module dependencies.
*/

var Binary = global.$MongooseDriver.Binary;
var Binary = require('../driver').get().Binary;
var utils = require('../utils');

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/types/decimal128.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
* @constructor ObjectId
*/

module.exports = global.$MongooseDriver.Decimal128;
module.exports = require('../driver').get().Decimal128;
2 changes: 1 addition & 1 deletion lib/types/objectid.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @constructor ObjectId
*/

var ObjectId = global.$MongooseDriver.ObjectId;
var ObjectId = require('../driver').get().ObjectId;

/*!
* Getter for convenience with populate, see gh-6115
Expand Down

0 comments on commit 836eb53

Please sign in to comment.