Skip to content

Commit

Permalink
Merge pull request #6771 from Automattic/gh6750
Browse files Browse the repository at this point in the history
refactor: use `global.$MongooseDriver` to store current driver to remove webpack warnings
  • Loading branch information
vkarpov15 committed Jul 28, 2018
2 parents 12e0d09 + 836eb53 commit 4071de4
Show file tree
Hide file tree
Showing 28 changed files with 99 additions and 79 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Expand Up @@ -5,3 +5,4 @@ test/model.discriminator.test.js
tools/
test/es6/
test/files/
dist/
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -38,5 +38,6 @@ docs/*.html

# Webpack output
dist
test/files/main.js

package-lock.json
2 changes: 2 additions & 0 deletions lib/browser.js
@@ -1,5 +1,7 @@
/* eslint-env browser */

require('./driver').set(require('./drivers/browser'));

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

Expand Down
7 changes: 2 additions & 5 deletions lib/collection.js
Expand Up @@ -6,7 +6,7 @@

const EventEmitter = require('events').EventEmitter;
const STATES = require('./connectionstate');
const utils = require('./utils');
const immediate = require('./helpers/immediate');

/**
* Abstract Collection constructor
Expand Down Expand Up @@ -83,10 +83,7 @@ Collection.prototype.conn;

Collection.prototype.onOpen = function() {
this.buffer = false;
var _this = this;
utils.immediate(function() {
_this.doQueue();
});
immediate(() => this.doQueue());
};

/**
Expand Down
3 changes: 1 addition & 2 deletions lib/connection.js
Expand Up @@ -5,9 +5,8 @@
*/

const EventEmitter = require('events').EventEmitter;
const driver = global.MONGOOSE_DRIVER_PATH || './drivers/node-mongodb-native';
const Schema = require('./schema');
const Collection = require(driver + '/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
@@ -0,0 +1,15 @@
'use strict';

/*!
* ignore
*/

let driver = null;

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

module.exports.set = function(v) {
driver = v;
};
3 changes: 3 additions & 0 deletions lib/drivers/browser/index.js
Expand Up @@ -3,6 +3,9 @@
*/

exports.Binary = require('./binary');
exports.Collection = function() {
throw new Error('Cannot create a collection from browser library');
};
exports.Decimal128 = require('./decimal128');
exports.ObjectId = require('./objectid');
exports.ReadPreference = require('./ReadPreference');
20 changes: 0 additions & 20 deletions lib/drivers/index.js

This file was deleted.

5 changes: 0 additions & 5 deletions lib/drivers/index.web.js

This file was deleted.

16 changes: 8 additions & 8 deletions lib/drivers/node-mongodb-native/collection.js
Expand Up @@ -7,7 +7,7 @@
const MongooseCollection = require('../../collection');
const Collection = require('mongodb').Collection;
const get = require('lodash.get');
const utils = require('../../utils');
const sliced = require('sliced');

/**
* A [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) collection implementation.
Expand Down Expand Up @@ -72,7 +72,7 @@ NativeCollection.prototype.onOpen = function() {
}
} else {
// create
var opts = utils.clone(_this.opts.capped);
const opts = Object.assign({}, _this.opts.capped);
opts.capped = true;
_this.conn.db.createCollection(_this.name, opts, callback);
}
Expand Down Expand Up @@ -132,7 +132,7 @@ function iter(i) {
if (debug) {
if (typeof debug === 'function') {
debug.apply(_this,
[_this.name, i].concat(utils.args(args, 0, args.length - 1)));
[_this.name, i].concat(sliced(args, 0, args.length - 1)));
} else {
this.$print(_this.name, i, args);
}
Expand Down Expand Up @@ -224,7 +224,7 @@ function format(obj, sub) {
return obj;
}

var x = utils.clone(obj, {transform: false});
var x = require('../../utils').clone(obj, {transform: false});
var representation;

if (x.constructor.name === 'Binary') {
Expand Down Expand Up @@ -282,10 +282,10 @@ function format(obj, sub) {
return x;
}

return require('util')
.inspect(x, false, 10, true)
.replace(/\n/g, '')
.replace(/\s{2,}/g, ' ');
return require('util').
inspect(x, false, 10, true).
replace(/\n/g, '').
replace(/\s{2,}/g, ' ');
}

/**
Expand Down
1 change: 1 addition & 0 deletions lib/drivers/node-mongodb-native/index.js
Expand Up @@ -3,6 +3,7 @@
*/

exports.Binary = require('./binary');
exports.Collection = require('./collection');
exports.Decimal128 = require('./decimal128');
exports.ObjectId = require('./objectid');
exports.ReadPreference = require('./ReadPreference');
10 changes: 10 additions & 0 deletions lib/helpers/immediate.js
@@ -0,0 +1,10 @@
/*!
* Centralize this so we can more easily work around issues with people
* stubbing out `process.nextTick()` in tests using sinon:
* https://github.com/sinonjs/lolex#automatically-incrementing-mocked-time
* See gh-6074
*/

module.exports = function immediate(cb) {
return process.nextTick(cb);
};
4 changes: 4 additions & 0 deletions lib/index.js
Expand Up @@ -4,6 +4,10 @@
* Module dependencies.
*/

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

const Schema = require('./schema');
const SchemaType = require('./schematype');
const VirtualType = require('./virtualtype');
Expand Down
9 changes: 5 additions & 4 deletions lib/model.js
Expand Up @@ -27,6 +27,7 @@ const cast = require('./cast');
const castUpdate = require('./helpers/query/castUpdate');
const discriminator = require('./helpers/model/discriminator');
const getDiscriminatorByValue = require('./queryhelpers').getDiscriminatorByValue;
const immediate = require('./helpers/immediate');
const internalToObjectOptions = require('./options').internalToObjectOptions;
const isPathSelectedInclusive = require('./helpers/projection/isPathSelectedInclusive');
const get = require('lodash.get');
Expand Down Expand Up @@ -824,7 +825,7 @@ Model.prototype.remove = function remove(options, fn) {

Model.prototype.$__remove = function $__remove(options, cb) {
if (this.$__.isDeleted) {
return utils.immediate(() => cb(null, this));
return immediate(() => cb(null, this));
}

var where = this.$__where();
Expand Down Expand Up @@ -1225,7 +1226,7 @@ function _ensureIndexes(model, options, callback) {
}

if (!indexes.length) {
utils.immediate(function() {
immediate(function() {
done();
});
return;
Expand Down Expand Up @@ -1271,7 +1272,7 @@ function _ensureIndexes(model, options, callback) {
}));
};

utils.immediate(function() {
immediate(function() {
// If buffering is off, do this manually.
if (options._automatic && !model.collection.collection) {
model.collection.addQueue(create, []);
Expand Down Expand Up @@ -3487,7 +3488,7 @@ function populate(model, docs, options, callback) {
const modelsMap = getModelsMapForPopulate(model, docs, options);

if (modelsMap instanceof Error) {
return utils.immediate(function() {
return immediate(function() {
callback(modelsMap);
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/query.js
Expand Up @@ -7,7 +7,7 @@
const CastError = require('./error/cast');
const ObjectParameterError = require('./error/objectParameter');
const QueryCursor = require('./cursor/QueryCursor');
const ReadPreference = require('./drivers').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
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 = require('./drivers').ReadPreference;
const readPref = require('./driver').get().ReadPreference;
const utils = require('./utils');

let MongooseTypes;
Expand Down
8 changes: 5 additions & 3 deletions lib/schematype.js
Expand Up @@ -4,10 +4,12 @@
* Module dependencies.
*/

var MongooseError = require('./error');
var $exists = require('./schema/operators/exists');
var $type = require('./schema/operators/type');
var immediate = require('./helpers/immediate');
var utils = require('./utils');
var MongooseError = require('./error');

var CastError = MongooseError.CastError;
var ValidatorError = MongooseError.ValidatorError;

Expand Down Expand Up @@ -796,15 +798,15 @@ SchemaType.prototype.doValidate = function(value, fn, scope) {
}
if (ok === undefined || ok) {
if (--count <= 0) {
utils.immediate(function() {
immediate(function() {
fn(null);
});
}
} else {
var ErrorConstructor = validatorProperties.ErrorConstructor || ValidatorError;
err = new ErrorConstructor(validatorProperties);
err.$isValidatorError = true;
utils.immediate(function() {
immediate(function() {
fn(err);
});
}
Expand Down
4 changes: 2 additions & 2 deletions lib/types/buffer.js
Expand Up @@ -2,8 +2,8 @@
* Module dependencies.
*/

var Binary = require('../drivers').Binary,
utils = require('../utils');
var Binary = require('../driver').get().Binary;
var utils = require('../utils');

/**
* Mongoose Buffer constructor.
Expand Down
2 changes: 1 addition & 1 deletion lib/types/decimal128.js
Expand Up @@ -8,4 +8,4 @@
* @constructor ObjectId
*/

module.exports = require('../drivers').Decimal128;
module.exports = require('../driver').get().Decimal128;
3 changes: 2 additions & 1 deletion lib/types/embedded.js
Expand Up @@ -6,6 +6,7 @@

const Document = require('../document_provider')();
const EventEmitter = require('events').EventEmitter;
const immediate = require('../helpers/immediate');
const internalToObjectOptions = require('../options').internalToObjectOptions;
const utils = require('../utils');

Expand Down Expand Up @@ -139,7 +140,7 @@ EmbeddedDocument.prototype.save = function(options, fn) {
*/

EmbeddedDocument.prototype.$__save = function(fn) {
return utils.immediate(() => fn(null, this));
return immediate(() => fn(null, this));
};

/*!
Expand Down
2 changes: 1 addition & 1 deletion lib/types/objectid.js
Expand Up @@ -8,7 +8,7 @@
* @constructor ObjectId
*/

var ObjectId = require('../drivers').ObjectId;
var ObjectId = require('../driver').get().ObjectId;

/*!
* Getter for convenience with populate, see gh-6115
Expand Down
3 changes: 2 additions & 1 deletion lib/types/subdocument.js
@@ -1,6 +1,7 @@
'use strict';

var Document = require('../document');
var immediate = require('../helpers/immediate');
var internalToObjectOptions = require('../options').internalToObjectOptions;
var utils = require('../utils');

Expand Down Expand Up @@ -68,7 +69,7 @@ Subdocument.prototype.save = function(options, fn) {
*/

Subdocument.prototype.$__save = function(fn) {
return utils.immediate(() => fn(null, this));
return immediate(() => fn(null, this));
};

Subdocument.prototype.$isValid = function(path) {
Expand Down
25 changes: 11 additions & 14 deletions lib/utils.js
Expand Up @@ -60,8 +60,8 @@ exports.deepEqual = function deepEqual(a, b) {
return a.getTime() === b.getTime();
}

if ((a instanceof ObjectId && b instanceof ObjectId) ||
(a instanceof Decimal && b instanceof Decimal)) {
if ((isBsonType(a, 'ObjectID') && isBsonType(b, 'ObjectID')) ||
(isBsonType(a, 'Decimal128') && isBsonType(b, 'Decimal128'))) {
return a.toString() === b.toString();
}

Expand Down Expand Up @@ -138,6 +138,14 @@ exports.deepEqual = function deepEqual(a, b) {
return true;
};

/*!
* Get the bson type, if it exists
*/

function isBsonType(obj, typename) {
return get(obj, '_bsontype', void 0) === typename;
}

/*!
* Get the last element of an array
*/
Expand Down Expand Up @@ -195,7 +203,7 @@ exports.clone = function clone(obj, options) {
if (obj instanceof ObjectId) {
return new ObjectId(obj.id);
}
if (obj instanceof Decimal) {
if (isBsonType(obj, 'Decimal128')) {
if (options && options.flattenDecimals) {
return obj.toJSON();
}
Expand Down Expand Up @@ -882,17 +890,6 @@ exports.each = function(arr, fn) {
}
};

/*!
* Centralize this so we can more easily work around issues with people
* stubbing out `process.nextTick()` in tests using sinon:
* https://github.com/sinonjs/lolex#automatically-incrementing-mocked-time
* See gh-6074
*/

exports.immediate = function immediate(cb) {
return process.nextTick(cb);
};

/*!
* ignore
*/
Expand Down

0 comments on commit 4071de4

Please sign in to comment.