From fe39b93302705c5f97f286b62ead8aebb2e70817 Mon Sep 17 00:00:00 2001 From: Matt Broadstone Date: Tue, 21 Aug 2018 18:51:43 -0400 Subject: [PATCH] feat(client-ops): allow bypassing creation of topologies on connect --- lib/operations/mongo_client_ops.js | 32 ++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/operations/mongo_client_ops.js b/lib/operations/mongo_client_ops.js index d614c062e9..e99d547023 100644 --- a/lib/operations/mongo_client_ops.js +++ b/lib/operations/mongo_client_ops.js @@ -31,7 +31,8 @@ const monitoringEvents = [ 'ping', 'ha', 'all', - 'fullsetup' + 'fullsetup', + 'open' ]; const ignoreOptionNames = ['native_parser']; const legacyOptionNames = ['server', 'replset', 'replSet', 'mongos', 'db']; @@ -132,11 +133,11 @@ function collectEvents(mongoClient, topology) { if (mongoClient instanceof MongoClient) { monitoringEvents.forEach(event => { topology.on(event, (object1, object2) => { - collectedEvents.push({ - event: event, - object1: object1, - object2: object2 - }); + if (event === 'open') { + collectedEvents.push({ event: event, object1: mongoClient }); + } else { + collectedEvents.push({ event: event, object1: object1, object2: object2 }); + } }); }); } @@ -346,11 +347,11 @@ function createServer(mongoClient, options, callback) { // Set default options const servers = translateOptions(options); - // Propagate the events to the client - const collectedEvents = collectEvents(mongoClient, servers[0]); - const server = servers[0]; + // Propagate the events to the client + const collectedEvents = collectEvents(mongoClient, server); + // Connect to topology server.connect(options, (err, topology) => { if (err) { @@ -389,8 +390,11 @@ function createTopology(mongoClient, topologyType, options, callback) { // Pass in the promise library options.promiseLibrary = mongoClient.s.promiseLibrary; + const translationOptions = {}; + if (topologyType === 'unified') translationOptions.createServers = false; + // Set default options - const servers = translateOptions(options); + const servers = translateOptions(options, translationOptions); // Create the topology let topology; @@ -571,7 +575,9 @@ function transformUrlOptions(_object) { return object; } -function translateOptions(options) { +function translateOptions(options, translationOptions) { + translationOptions = Object.assign({}, { createServers: true }, translationOptions); + // If we have a readPreference passed in by the db options if (typeof options.readPreference === 'string' || typeof options.read_preference === 'string') { options.readPreference = new ReadPreference(options.readPreference || options.read_preference); @@ -591,6 +597,10 @@ function translateOptions(options) { if (options.socketTimeoutMS == null) options.socketTimeoutMS = 360000; if (options.connectTimeoutMS == null) options.connectTimeoutMS = 30000; + if (!translationOptions.createServers) { + return; + } + // Create server instances return options.servers.map(serverObj => { return serverObj.domain_socket