diff --git a/lib/operations/collection_ops.js b/lib/operations/collection_ops.js index e828584f80..5dd81dd33d 100644 --- a/lib/operations/collection_ops.js +++ b/lib/operations/collection_ops.js @@ -18,10 +18,14 @@ const handleCallback = require('../utils').handleCallback; const indexInformationDb = require('./db_ops').indexInformation; const isObject = require('../utils').isObject; const Long = require('mongodb-core').BSON.Long; +const makeLazyLoader = require('../utils').makeLazyLoader; const MongoError = require('mongodb-core').MongoError; const ReadPreference = require('mongodb-core').ReadPreference; const toError = require('../utils').toError; +const loadCollection = makeLazyLoader(`${__dirname}/../collection`); +const loadDb = makeLazyLoader(`${__dirname}/../db`); + /** * Group function helper * @ignore @@ -987,7 +991,7 @@ function mapReduce(coll, map, reduce, options, callback) { if (result.result != null && typeof result.result === 'object') { const doc = result.result; // Return a collection from another db - const Db = require('../db'); + let Db = loadDb(); collection = new Db(doc.db, coll.s.db.s.topology, coll.s.db.s.options).collection( doc.collection ); @@ -1204,7 +1208,7 @@ function removeDocuments(coll, selector, options, callback) { * @param {Collection~collectionResultCallback} [callback] The results callback */ function rename(coll, newName, options, callback) { - const Collection = require('../collection'); + let Collection = loadCollection(); // Check the collection name checkCollectionName(newName); // Build the command diff --git a/lib/operations/cursor_ops.js b/lib/operations/cursor_ops.js index 5c0c093a52..67f8db9f4e 100644 --- a/lib/operations/cursor_ops.js +++ b/lib/operations/cursor_ops.js @@ -3,9 +3,12 @@ const buildCountCommand = require('./collection_ops').buildCountCommand; const formattedOrderClause = require('../utils').formattedOrderClause; const handleCallback = require('../utils').handleCallback; +const makeLazyLoader = require('../utils').makeLazyLoader; const MongoError = require('mongodb-core').MongoError; const push = Array.prototype.push; +const loadCursor = makeLazyLoader(`${__dirname}/../cursor`); + /** * Get the count of documents for this cursor. * @@ -74,7 +77,7 @@ function count(cursor, applySkipLimit, opts, callback) { * @param {Cursor~resultCallback} callback The result callback. */ function each(cursor, callback) { - const Cursor = require('../cursor'); + let Cursor = loadCursor(); if (!callback) throw MongoError.create({ message: 'callback is mandatory', driver: true }); if (cursor.isNotified()) return; @@ -114,7 +117,7 @@ function each(cursor, callback) { * @param {Cursor~resultCallback} [callback] The result callback. */ function hasNext(cursor, callback) { - const Cursor = require('../cursor'); + let Cursor = loadCursor(); if (cursor.s.currentDoc) { return callback(null, true); @@ -165,7 +168,7 @@ function next(cursor, callback) { // Get the next available document from the cursor, returns null if no more documents are available. function nextObject(cursor, callback) { - const Cursor = require('../cursor'); + let Cursor = loadCursor(); if (cursor.s.state === Cursor.CLOSED || (cursor.isDead && cursor.isDead())) return handleCallback( @@ -196,7 +199,7 @@ function nextObject(cursor, callback) { * @param {Cursor~toArrayResultCallback} [callback] The result callback. */ function toArray(cursor, callback) { - const Cursor = require('../cursor'); + let Cursor = loadCursor(); const items = []; diff --git a/lib/operations/db_ops.js b/lib/operations/db_ops.js index 3795f8b05c..7986b09640 100644 --- a/lib/operations/db_ops.js +++ b/lib/operations/db_ops.js @@ -6,6 +6,7 @@ const resolveReadPreference = require('../utils').resolveReadPreference; const crypto = require('crypto'); const debugOptions = require('../utils').debugOptions; const handleCallback = require('../utils').handleCallback; +const makeLazyLoader = require('../utils').makeLazyLoader; const MongoError = require('mongodb-core').MongoError; const parseIndexOptions = require('../utils').parseIndexOptions; const ReadPreference = require('mongodb-core').ReadPreference; @@ -17,6 +18,9 @@ const findOne = require('./collection_ops').findOne; const remove = require('./collection_ops').remove; const updateOne = require('./collection_ops').updateOne; +const loadCollection = makeLazyLoader(`${__dirname}/../collection`); +const loadDb = makeLazyLoader(`${__dirname}/../db`); + const debugFields = [ 'authSource', 'w', @@ -64,7 +68,7 @@ const illegalCommandFields = [ * @param {Db~resultCallback} [callback] The command result callback */ function addUser(db, username, password, options, callback) { - const Db = require('../db'); + let Db = loadDb(); // Did the user destroy the topology if (db.serverConfig && db.serverConfig.isDestroyed()) @@ -132,7 +136,7 @@ function addUser(db, username, password, options, callback) { * @param {Db~collectionsResultCallback} [callback] The results callback */ function collections(db, options, callback) { - const Collection = require('../collection'); + let Collection = loadCollection(); options = Object.assign({}, options, { nameOnly: true }); // Let's get the collection names @@ -172,7 +176,7 @@ function collections(db, options, callback) { * @param {Db~collectionResultCallback} [callback] The results callback */ function createCollection(db, name, options, callback) { - const Collection = require('../collection'); + let Collection = loadCollection(); // Get the write concern options const finalOptions = applyWriteConcern(Object.assign({}, options), { db }, options); @@ -632,7 +636,7 @@ function profilingLevel(db, options, callback) { * @param {Db~resultCallback} [callback] The command result callback */ function removeUser(db, username, options, callback) { - const Db = require('../db'); + let Db = loadDb(); // Attempt to execute command executeAuthRemoveUserCommand(db, username, options, (err, result) => { diff --git a/lib/operations/mongo_client_ops.js b/lib/operations/mongo_client_ops.js index 4f7da1c24d..04d606ea6a 100644 --- a/lib/operations/mongo_client_ops.js +++ b/lib/operations/mongo_client_ops.js @@ -3,6 +3,7 @@ const authenticate = require('../authenticate'); const deprecate = require('util').deprecate; const Logger = require('mongodb-core').Logger; +const makeLazyLoader = require('../utils').makeLazyLoader; const MongoError = require('mongodb-core').MongoError; const Mongos = require('../topologies/mongos'); const parse = require('mongodb-core').parseConnectionString; @@ -11,6 +12,8 @@ const ReplSet = require('../topologies/replset'); const Server = require('../topologies/server'); const ServerSessionPool = require('mongodb-core').Sessions.ServerSessionPool; +const loadClient = makeLazyLoader(`${__dirname}/../mongo_client`); + const monitoringEvents = [ 'timeout', 'close', @@ -127,7 +130,7 @@ function clearAllEvents(topology) { // Collect all events in order from SDAM function collectEvents(mongoClient, topology) { - const MongoClient = require('../mongo_client'); + let MongoClient = loadClient(); const collectedEvents = []; if (mongoClient instanceof MongoClient) { diff --git a/lib/utils.js b/lib/utils.js index 90e6b7fda3..37f386547b 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -694,6 +694,16 @@ function deprecateOptions(config, fn) { return deprecated; } +function makeLazyLoader(modulePath) { + let mod; + return function lazyLoad() { + if (!mod) { + mod = require(modulePath); + } + return mod; + }; +} + module.exports = { filterOptions, mergeOptions, @@ -719,5 +729,6 @@ module.exports = { isPromiseLike, decorateWithCollation, decorateWithReadConcern, - deprecateOptions + deprecateOptions, + makeLazyLoader };