Skip to content

Commit 836eb53

Browse files
committedJul 28, 2018
refactor: use driver.js singleton rather than global.$MongooseDriver
Re: #6705
1 parent 451c50e commit 836eb53

10 files changed

+26
-8
lines changed
 

‎.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ test/model.discriminator.test.js
55
tools/
66
test/es6/
77
test/files/
8+
dist/

‎lib/browser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-env browser */
22

3-
global.$MongooseDriver = require('./drivers/browser');
3+
require('./driver').set(require('./drivers/browser'));
44

55
var DocumentProvider = require('./document_provider.js');
66
var PromiseProvider = require('./promise_provider');

‎lib/connection.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
const EventEmitter = require('events').EventEmitter;
88
const Schema = require('./schema');
9-
const Collection = global.$MongooseDriver.Collection;
9+
const Collection = require('./driver').get().Collection;
1010
const STATES = require('./connectionstate');
1111
const MongooseError = require('./error');
1212
const PromiseProvider = require('./promise_provider');

‎lib/driver.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
/*!
4+
* ignore
5+
*/
6+
7+
let driver = null;
8+
9+
module.exports.get = function() {
10+
return driver;
11+
};
12+
13+
module.exports.set = function(v) {
14+
driver = v;
15+
};

‎lib/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
* Module dependencies.
55
*/
66

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

911
const Schema = require('./schema');
1012
const SchemaType = require('./schematype');

‎lib/query.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
const CastError = require('./error/cast');
88
const ObjectParameterError = require('./error/objectParameter');
99
const QueryCursor = require('./cursor/QueryCursor');
10-
const ReadPreference = global.$MongooseDriver.ReadPreference;
10+
const ReadPreference = require('./driver').get().ReadPreference;
1111
const applyWriteConcern = require('./helpers/schema/applyWriteConcern');
1212
const cast = require('./cast');
1313
const castUpdate = require('./helpers/query/castUpdate');

‎lib/schema.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const VirtualType = require('./virtualtype');
1111
const get = require('lodash.get');
1212
const getIndexes = require('./helpers/schema/getIndexes');
1313
const mpath = require('mpath');
14-
const readPref = global.$MongooseDriver.ReadPreference;
14+
const readPref = require('./driver').get().ReadPreference;
1515
const utils = require('./utils');
1616

1717
let MongooseTypes;

‎lib/types/buffer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Module dependencies.
33
*/
44

5-
var Binary = global.$MongooseDriver.Binary;
5+
var Binary = require('../driver').get().Binary;
66
var utils = require('../utils');
77

88
/**

‎lib/types/decimal128.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
* @constructor ObjectId
99
*/
1010

11-
module.exports = global.$MongooseDriver.Decimal128;
11+
module.exports = require('../driver').get().Decimal128;

‎lib/types/objectid.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @constructor ObjectId
99
*/
1010

11-
var ObjectId = global.$MongooseDriver.ObjectId;
11+
var ObjectId = require('../driver').get().ObjectId;
1212

1313
/*!
1414
* Getter for convenience with populate, see gh-6115

0 commit comments

Comments
 (0)
Please sign in to comment.