Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mongodb-topology-manager

Package Overview
Dependencies
Maintainers
2
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongodb-topology-manager

Localhost MongoDB Topology Management API

  • 2.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.1K
decreased by-18.05%
Maintainers
2
Weekly downloads
 
Created
Source

The MongoDB Topology Management API

The MongoDB Topology Management API is an API to allow to programatically spin up a MongoDB instance, Replicaset or Sharded cluster on your local machine.

Setting up a single instance

It's very simple to create a single running MongoDB instance. All examples are using ES6.

var Server = require('mongodb-topology-manager').Server;
// Create new instance
var server = new Server('binary', {
  dbpath: './db'
});

const init = async () => {
  // Perform discovery
  var result = await server.discover();
  // Purge the directory
  await server.purge();
  // Start process
  await server.start();
  // Stop the process
  await server.stop();
}

// start the server
init();

Setting up a replicaset

It's equally easy to create a new Replicaset instance.

var ReplSet = require('mongodb-topology-manager').ReplSet;

// Create new instance
var topology = new ReplSet('mongod', [{
  // mongod process options
  options: {
    bind_ip: 'localhost', port: 31000, dbpath: './db-1'
  }
}, {
  // mongod process options
  options: {
    bind_ip: 'localhost', port: 31001, dbpath: './db-2'
  }
}, {
  // Type of node
  arbiterOnly: true,
  // mongod process options
  options: {
    bind_ip: 'localhost', port: 31002, dbpath: './db-3'
  }
}], {
  replSet: 'rs'
});

const init = async () => {
  // Perform discovery
  var result = await server.discover();
  // Purge the directory
  await server.purge();
  // Start process
  await server.start();
  // Stop the process
  await server.stop();
}

// start the replica set
init();

Each of the node objects can take the following options at the top level.

FieldDescription
arbiternode should become an arbiter.
builIndexesshould build indexes on the node.
hiddennode should be hidden.
builIndexesshould build indexes on the node.
prioritynode should have the following priority.
tagstags for the node.
slaveDelaythe node slaveDelay for replication.
votesadditional votes for the specific node.

The object contains the options that are used to start up the actual mongod instance.

The Replicaset manager has the following methods

MethodDescription
Replset.prototype.discoverReturn the information from running mongod with --version flag.
Replset.prototype.startStart the Replicaset.
Replset.prototype.primaryReturn the current Primary server manager.
Replset.prototype.shardUrlReturn a add shard url string.
Replset.prototype.urlReturn a connection url.
Replset.prototype.arbitersReturn a list of arbiter managers.
Replset.prototype.secondariesReturn a list of secondary managers.
Replset.prototype.passivesReturn a list of secondary passive managers.
Replset.prototype.waitForPrimaryWait for a new primary to be elected or for a specific timeout period.
Replset.prototype.stepDownPrimaryStepdown the primary.
Replset.prototype.configurationReturn the replicaset configuration.
Replset.prototype.reconfigurePerform a reconfiguration of the replicaset.
Replset.prototype.serverConfigurationGet the initial node configuration for specific server manager.
Replset.prototype.addMemberAdd a new member to the set.
Replset.prototype.removeMemberRemove a member to the set.
Replset.prototype.maintenancePut a node into maintenance mode.
Replset.prototype.stopStop the replicaset.
Replset.prototype.restartRestart the replicaset.
Replset.prototype.purgePurge all the data directories for the replicaset.

Setting up a sharded system

It's a little bit more complicated to set up a Sharded system but not much more.

var Sharded = require('mongodb-topology-manager').Sharded;

const init = async () => {
  // Create new instance
  var topology = new Sharded({
    mongod: 'mongod', mongos: 'mongos'
  });

  // Add one shard
  await topology.addShard([{
    options: {
      bind_ip: 'localhost', port: 31000, dbpath: './db-1', shardsvr: null
    }
  }, {
    options: {
      bind_ip: 'localhost', port: 31001, dbpath: './db-2', shardsvr: null
  }, {
    // Type of node
    arbiter: true,
    // mongod process options
    options: {
      bind_ip: 'localhost', port: 31002, dbpath: './db-3', shardsvr: null
    }
  }], {
    replSet: 'rs1'
  });

  // Add one shard
  await topology.addShard([{
    options: {
      bind_ip: 'localhost', port: 31010, dbpath: './db-4', shardsvr: null
    }
  }, {
    options: {
      bind_ip: 'localhost', port: 31011, dbpath: './db-5', shardsvr: null
    }
  }, {
    // Type of node
    arbiter: true,
    // mongod process options
    options: {
      bind_ip: 'localhost', port: 31012, dbpath: './db-6', shardsvr: null
    }
  }], {
    replSet: 'rs2'
  });

  // Add configuration servers
  await topology.addConfigurationServers([{
    options: {
      bind_ip: 'localhost', port: 35000, dbpath: './db-7'
    }
  }, {
    options: {
      bind_ip: 'localhost', port: 35001, dbpath: './db-8'
    }
  }, {
    options: {
      bind_ip: 'localhost', port: 35002, dbpath: './db-9'
    }
  }], {
    replSet: 'rs3'
  });

  // Add proxies
  await topology.addProxies([{
    bind_ip: 'localhost', port: 51000, configdb: 'localhost:35000,localhost:35001,localhost:35002'
  }, {
    bind_ip: 'localhost', port: 51001, configdb: 'localhost:35000,localhost:35001,localhost:35002'
  }], {
    binary: 'mongos'
  });

  // Start up topology
  await topology.start();

  // Shard db
  await topology.enableSharding('test');

  // Shard a collection
  await topology.shardCollection('test', 'testcollection', {_id: 1});

  // Stop the topology
  await topology.stop();
}

// start the shards
init();

The Sharded manager has the following methods

MethodDescription
Replset.prototype.discoverReturn the information from running mongod with --version flag.
Replset.prototype.startStart the Sharded cluster.
Replset.prototype.stopStop the replicaset.
Replset.prototype.restartRestart the replicaset.
Replset.prototype.purgePurge all the data directories for the replicaset.
Replset.prototype.addShardAdd a new shard to the cluster.
Replset.prototype.addConfigurationServersAdd a set of nodes to be configuration servers.
ReplSet.prototype.addProxiesAdd a set of mongo proxies to the cluster.
ReplSet.prototype.enableShardingEnable sharding on a specific db.
ReplSet.prototype.shardCollectionShard a collection.

Keywords

FAQs

Package last updated on 25 Aug 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc