@nickmaina/mongors
Version:
A very opinionated cli to run a MongoDB replica set locally for development
65 lines (64 loc) • 2.83 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const mongodb_topology_manager_1 = require("mongodb-topology-manager");
const mkdirp_1 = __importDefault(require("mkdirp"));
const fs_1 = __importDefault(require("fs"));
const utils_1 = require("./utils");
const rimraf_1 = __importDefault(require("rimraf"));
class ReplicaSet {
constructor(options) {
this.setServer = () => {
const ports = [];
for (let i = 0; i < 3; i++) {
ports.push(this.options.port + i);
}
ports.forEach(port => {
const path = utils_1.thunkDataPath(`${port}`);
rimraf_1.default(path, fs_1.default, () => { });
mkdirp_1.default(path);
});
const nodes = ports.map((port, index) => {
const config = {
options: {
bind_ip: utils_1.DEFAULT_HOSTNAME,
port,
dbpath: utils_1.thunkDataPath(`${port}`)
}
};
if (ports.length - 1 === index) {
config["arbiter"] = true;
}
return config;
});
this.server = new mongodb_topology_manager_1.ReplSet("mongod", nodes, {
replSet: "rs"
});
};
this.purge = () => this.server.purge();
this.discover = () => this.server.discover();
this.url = () => `"mongodb://${this.server.url()}?replicaSet=rs"`;
this.start = () => __awaiter(this, void 0, void 0, function* () {
if (this.options.keep) {
yield this.purge();
}
this.server.start();
});
this.stop = () => this.server.stop();
this.options = options;
this.setServer();
}
}
exports.ReplicaSet = ReplicaSet;
exports.default = ReplicaSet;