Skip to content

Commit

Permalink
BREAKING CHANGE: rename driver getConnection -> Connection, avoid…
Browse files Browse the repository at this point in the history
… relying on driver to import Binary, ObjectId, Decimal128

Re: #12638
  • Loading branch information
vkarpov15 committed Dec 27, 2022
1 parent 1f7b6fe commit a7c695f
Show file tree
Hide file tree
Showing 32 changed files with 268 additions and 233 deletions.
3 changes: 2 additions & 1 deletion lib/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
const AggregationCursor = require('./cursor/AggregationCursor');
const Query = require('./query');
const { applyGlobalMaxTimeMS, applyGlobalDiskUse } = require('./helpers/query/applyGlobalOption');
const clone = require('./helpers/clone');
const getConstructorName = require('./helpers/getConstructorName');
const prepareDiscriminatorPipeline = require('./helpers/aggregate/prepareDiscriminatorPipeline');
const promiseOrCallback = require('./helpers/promiseOrCallback');
Expand Down Expand Up @@ -1041,7 +1042,7 @@ Aggregate.prototype.exec = function(callback) {
return cb(new Error('Aggregate has empty pipeline'));
}

const options = utils.clone(this.options || {});
const options = clone(this.options || {});

collection.aggregate(this._pipeline, options, (err, cursor) => {
if (err != null) {
Expand Down
2 changes: 1 addition & 1 deletion lib/cast/objectid.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const isBsonType = require('../helpers/isBsonType');
const ObjectId = require('../driver').get().ObjectId;
const ObjectId = require('../types/objectid');

module.exports = function castObjectId(value) {
if (value == null) {
Expand Down
5 changes: 3 additions & 2 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const SyncIndexesError = require('./error/syncIndexes');
const PromiseProvider = require('./promise_provider');
const ServerSelectionError = require('./error/serverSelection');
const applyPlugins = require('./helpers/schema/applyPlugins');
const clone = require('./helpers/clone');
const driver = require('./driver');
const promiseOrCallback = require('./helpers/promiseOrCallback');
const get = require('./helpers/get');
Expand Down Expand Up @@ -741,7 +742,7 @@ Connection.prototype.openUri = function(uri, options, callback) {
options = processConnectionOptions(uri, options);

if (options) {
options = utils.clone(options);
options = clone(options);

const autoIndex = options.config && options.config.autoIndex != null ?
options.config.autoIndex :
Expand Down Expand Up @@ -1093,7 +1094,7 @@ Connection.prototype.collection = function(name, options) {
autoIndex: this.config.autoIndex != null ? this.config.autoIndex : this.base.options.autoIndex,
autoCreate: this.config.autoCreate != null ? this.config.autoCreate : this.base.options.autoCreate
};
options = Object.assign({}, defaultOptions, options ? utils.clone(options) : {});
options = Object.assign({}, defaultOptions, options ? clone(options) : {});
options.$wasForceClosed = this.$wasForceClosed;
const Collection = this.base && this.base.__driver && this.base.__driver.Collection || driver.get().Collection;
if (!(name in this.collections)) {
Expand Down
6 changes: 3 additions & 3 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const promiseOrCallback = require('./helpers/promiseOrCallback');
const castNumber = require('./cast/number');
const applyDefaults = require('./helpers/document/applyDefaults');
const cleanModifiedSubpaths = require('./helpers/document/cleanModifiedSubpaths');
const clone = require('./helpers/clone');
const compile = require('./helpers/document/compile').compile;
const defineKey = require('./helpers/document/compile').defineKey;
const flatten = require('./helpers/common').flatten;
Expand All @@ -40,7 +41,6 @@ const queryhelpers = require('./queryhelpers');
const utils = require('./utils');
const isPromise = require('./helpers/isPromise');

const clone = utils.clone;
const deepEqual = utils.deepEqual;
const isMongooseObject = utils.isMongooseObject;

Expand Down Expand Up @@ -1884,7 +1884,7 @@ Document.prototype.get = function(path, type, options) {
obj = schema.applyGetters(obj, this);
} else if (this.$__schema.nested[path] && options.virtuals) {
// Might need to apply virtuals if this is a nested path
return applyVirtuals(this, utils.clone(obj) || {}, { path: path });
return applyVirtuals(this, clone(obj) || {}, { path: path });
}

return obj;
Expand Down Expand Up @@ -1955,7 +1955,7 @@ Document.prototype.$__saveInitialState = function $__saveInitialState(path) {
const firstDot = savedStatePath.indexOf('.');
const topLevelPath = firstDot === -1 ? savedStatePath : savedStatePath.slice(0, firstDot);
if (!savedState.hasOwnProperty(topLevelPath)) {
savedState[topLevelPath] = utils.clone(this.$__getValue(topLevelPath));
savedState[topLevelPath] = clone(this.$__getValue(topLevelPath));
}
}
};
Expand Down
5 changes: 1 addition & 4 deletions lib/drivers/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

'use strict';

exports.Binary = require('./binary');
exports.Collection = function() {
throw new Error('Cannot create a collection from browser library');
};
exports.getConnection = () => function() {
exports.Connection = function() {
throw new Error('Cannot create a connection from browser library');
};
exports.Decimal128 = require('./decimal128');
exports.ObjectId = require('./objectid');
10 changes: 0 additions & 10 deletions lib/drivers/node-mongodb-native/binary.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/drivers/node-mongodb-native/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const MongooseCollection = require('../../collection');
const MongooseError = require('../../error/mongooseError');
const Collection = require('mongodb').Collection;
const ObjectId = require('./objectid');
const ObjectId = require('../../types/objectid');
const getConstructorName = require('../../helpers/getConstructorName');
const stream = require('stream');
const util = require('util');
Expand Down
7 changes: 0 additions & 7 deletions lib/drivers/node-mongodb-native/decimal128.js

This file was deleted.

5 changes: 1 addition & 4 deletions lib/drivers/node-mongodb-native/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@

'use strict';

exports.Binary = require('./binary');
exports.Collection = require('./collection');
exports.Decimal128 = require('./decimal128');
exports.ObjectId = require('./objectid');
exports.getConnection = () => require('./connection');
exports.Connection = require('./connection');
16 changes: 0 additions & 16 deletions lib/drivers/node-mongodb-native/objectid.js

This file was deleted.

8 changes: 4 additions & 4 deletions lib/helpers/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ const specialProperties = require('./specialProperties');
const isMongooseObject = require('./isMongooseObject');
const getFunctionName = require('./getFunctionName');
const isBsonType = require('./isBsonType');
const isMongooseArray = require('../types/array/isMongooseArray').isMongooseArray;
const isObject = require('./isObject');
const isPOJO = require('./isPOJO');
const symbols = require('./symbols');
const trustedSymbol = require('./query/trusted').trustedSymbol;
const utils = require('../utils');


/**
* Object clone with Mongoose natives support.
Expand All @@ -32,7 +32,7 @@ function clone(obj, options, isArrayChild) {
}

if (Array.isArray(obj)) {
return cloneArray(utils.isMongooseArray(obj) ? obj.__array : obj, options);
return cloneArray(isMongooseArray(obj) ? obj.__array : obj, options);
}

if (isMongooseObject(obj)) {
Expand All @@ -43,7 +43,7 @@ function clone(obj, options, isArrayChild) {
}
const isSingleNested = obj.$isSingleNested;

if (utils.isPOJO(obj) && obj.$__ != null && obj._doc != null) {
if (isPOJO(obj) && obj.$__ != null && obj._doc != null) {
return obj._doc;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Module dependencies.
*/

const Binary = require('../driver').get().Binary;
const Binary = require('bson').Binary;
const isBsonType = require('./isBsonType');
const isMongooseObject = require('./isMongooseObject');
const MongooseError = require('../error');
Expand Down
3 changes: 2 additions & 1 deletion lib/helpers/document/compile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const clone = require('../../helpers/clone');
const documentSchemaSymbol = require('../../helpers/symbols').documentSchemaSymbol;
const internalToObjectOptions = require('../../options').internalToObjectOptions;
const utils = require('../../utils');
Expand Down Expand Up @@ -110,7 +111,7 @@ function defineKey({ prop, subprops, prototype, prefix, options }) {
configurable: true,
writable: false,
value: function() {
return utils.clone(_this.get(path, null, {
return clone(_this.get(path, null, {
virtuals: this &&
this.schema &&
this.schema.options &&
Expand Down
12 changes: 12 additions & 0 deletions lib/helpers/isPOJO.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

module.exports = function isPOJO(arg) {
if (arg == null || typeof arg !== 'object') {
return false;
}
const proto = Object.getPrototypeOf(arg);
// Prototype may be null if you used `Object.create(null)`
// Checking `proto`'s constructor is safe because `getPrototypeOf()`
// explicitly crosses the boundary from object data to object metadata
return !proto || proto.constructor.name === 'Object';
};
3 changes: 2 additions & 1 deletion lib/helpers/model/discriminator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const Mixed = require('../../schema/mixed');
const clone = require('../clone');
const defineKey = require('../document/compile').defineKey;
const get = require('../get');
const utils = require('../../utils');
Expand Down Expand Up @@ -174,7 +175,7 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
}
}
}
schema.options = utils.clone(baseSchema.options);
schema.options = clone(baseSchema.options);
if (toJSON) schema.options.toJSON = toJSON;
if (toObject) schema.options.toObject = toObject;
if (typeof _id !== 'undefined') {
Expand Down
3 changes: 2 additions & 1 deletion lib/helpers/populate/assignRawDocsToIdStructure.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const clone = require('../../helpers/clone');
const leanPopulateMap = require('./leanPopulateMap');
const modelSymbol = require('../symbols').modelSymbol;
const utils = require('../../utils');
Expand Down Expand Up @@ -68,7 +69,7 @@ function assignRawDocsToIdStructure(rawIds, resultDocs, resultOrder, options, re
if (options.clone && doc != null) {
if (options.lean) {
const _model = leanPopulateMap.get(doc);
doc = utils.clone(doc);
doc = clone(doc);
leanPopulateMap.set(doc, _model);
} else {
doc = doc.constructor.hydrate(doc._doc);
Expand Down
3 changes: 2 additions & 1 deletion lib/helpers/populate/getModelsMapForPopulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const MongooseError = require('../../error/index');
const SkipPopulateValue = require('./SkipPopulateValue');
const clone = require('../clone');
const get = require('../get');
const getDiscriminatorByValue = require('../discriminator/getDiscriminatorByValue');
const getConstructorName = require('../getConstructorName');
Expand Down Expand Up @@ -531,7 +532,7 @@ function addModelNamesToMap(model, map, available, modelNames, options, data, re
model: Model
};
if (data.isVirtual && get(data.virtual, 'options.options')) {
currentOptions.options = utils.clone(data.virtual.options.options);
currentOptions.options = clone(data.virtual.options.options);
} else if (schemaOptions != null) {
currentOptions.options = Object.assign({}, schemaOptions);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/query/handleReadPreferenceAliases.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ module.exports = function handleReadPreferenceAliases(pref) {
}

return pref;
};
};
12 changes: 6 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Mongoose.prototype.setDriver = function setDriver(driver) {
}
_mongoose.__driver = driver;

const Connection = driver.getConnection();
const Connection = driver.Connection;
_mongoose.connections = [new Connection(_mongoose)];
_mongoose.connections[0].models = _mongoose.models;

Expand Down Expand Up @@ -348,7 +348,7 @@ Mongoose.prototype.get = Mongoose.prototype.set;
Mongoose.prototype.createConnection = function(uri, options, callback) {
const _mongoose = this instanceof Mongoose ? this : mongoose;

const Connection = _mongoose.__driver.getConnection();
const Connection = _mongoose.__driver.Connection;
const conn = new Connection(_mongoose);
if (typeof options === 'function') {
callback = options;
Expand Down Expand Up @@ -770,7 +770,7 @@ Mongoose.prototype.__defineGetter__('connection', function() {
});

Mongoose.prototype.__defineSetter__('connection', function(v) {
if (v instanceof this.__driver.getConnection()) {
if (v instanceof this.__driver.Connection) {
this.connections[0] = v;
this.models = v.models;
}
Expand Down Expand Up @@ -837,14 +837,14 @@ Object.defineProperty(Mongoose.prototype, 'Collection', {

Object.defineProperty(Mongoose.prototype, 'Connection', {
get: function() {
return this.__driver.getConnection();
return this.__driver.Connection;
},
set: function(Connection) {
if (Connection === this.__driver.getConnection()) {
if (Connection === this.__driver.Connection) {
return;
}

this.__driver.getConnection = () => Connection;
this.__driver.Connection = Connection;
}
});

Expand Down
Loading

0 comments on commit a7c695f

Please sign in to comment.