Skip to content

Commit

Permalink
feat(client-ops): allow bypassing creation of topologies on connect
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Aug 24, 2018
1 parent 3cb3da3 commit fe39b93
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions lib/operations/mongo_client_ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const monitoringEvents = [
'ping',
'ha',
'all',
'fullsetup'
'fullsetup',
'open'
];
const ignoreOptionNames = ['native_parser'];
const legacyOptionNames = ['server', 'replset', 'replSet', 'mongos', 'db'];
Expand Down Expand Up @@ -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 });
}
});
});
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down

0 comments on commit fe39b93

Please sign in to comment.