diff --git a/lib/operations/collection_ops.js b/lib/operations/collection_ops.js index 5dd81dd33d..1314723522 100644 --- a/lib/operations/collection_ops.js +++ b/lib/operations/collection_ops.js @@ -18,13 +18,24 @@ 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`); +let collection; +function loadCollection() { + if (!collection) { + collection = require('../collection'); + } + return collection; +} +let db; +function loadDb() { + if (!db) { + db = require('../db'); + } + return db; +} /** * Group function helper diff --git a/lib/operations/cursor_ops.js b/lib/operations/cursor_ops.js index 67f8db9f4e..fa6db957b8 100644 --- a/lib/operations/cursor_ops.js +++ b/lib/operations/cursor_ops.js @@ -3,11 +3,16 @@ 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`); +let cursor; +function loadCursor() { + if (!cursor) { + cursor = require('../cursor'); + } + return cursor; +} /** * Get the count of documents for this cursor. diff --git a/lib/operations/db_ops.js b/lib/operations/db_ops.js index 7986b09640..8cf5dcdd52 100644 --- a/lib/operations/db_ops.js +++ b/lib/operations/db_ops.js @@ -6,7 +6,6 @@ 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; @@ -18,8 +17,20 @@ 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`); +let collection; +function loadCollection() { + if (!collection) { + collection = require('../collection'); + } + return collection; +} +let db; +function loadDb() { + if (!db) { + db = require('../db'); + } + return db; +} const debugFields = [ 'authSource', diff --git a/lib/operations/mongo_client_ops.js b/lib/operations/mongo_client_ops.js index 04d606ea6a..d53ee7c235 100644 --- a/lib/operations/mongo_client_ops.js +++ b/lib/operations/mongo_client_ops.js @@ -3,7 +3,6 @@ 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; @@ -12,7 +11,13 @@ const ReplSet = require('../topologies/replset'); const Server = require('../topologies/server'); const ServerSessionPool = require('mongodb-core').Sessions.ServerSessionPool; -const loadClient = makeLazyLoader(`${__dirname}/../mongo_client`); +let client; +function loadClient() { + if (!client) { + client = require('../mongo_client'); + } + return client; +} const monitoringEvents = [ 'timeout', diff --git a/lib/utils.js b/lib/utils.js index 37f386547b..90e6b7fda3 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -694,16 +694,6 @@ 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, @@ -729,6 +719,5 @@ module.exports = { isPromiseLike, decorateWithCollation, decorateWithReadConcern, - deprecateOptions, - makeLazyLoader + deprecateOptions };