2.1.0 • Published 6 years ago

mongodb-topology-manager v2.1.0

Weekly downloads
3,888
License
Apache-2.0
Repository
github
Last release
6 years ago

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.
2.1.0

6 years ago

2.0.0

6 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

8 years ago

1.0.9

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago