copybase
Version:
Copy or backup databases quickly
99 lines (98 loc) • 4.7 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 path_1 = __importDefault(require("path"));
const BaseProvider_1 = __importDefault(require("./BaseProvider"));
class MongoDb extends BaseProvider_1.default {
constructor(config) {
super(Object.assign({ port: 27017 }, config));
}
/**
* Dump a database into the given folder
*
* @param outputFolder
* @returns
*/
dump(outputFolder) {
return __awaiter(this, void 0, void 0, function* () {
yield this.checkCommandExists("mongodump", ["--version"]);
const { hostname, port, uri, username, password, mongodump: mongodumpOptions, } = this.config;
const customOptions = this.getCustomOptions(mongodumpOptions);
let { database } = this.config;
this.logInfo(`Dump ${database} from ${hostname || uri}`);
const exitCode = yield this.execCommand("mongodump", [
this.config.verbose ? "" : "--quiet",
port && !uri ? `--port=${port}` : "",
username && !uri ? `--username=${username}` : "",
password && !uri ? `--password=${password}` : "",
hostname && !uri ? `--host=${hostname}` : "",
uri ? `--uri='${uri}'` : `--db=${database}`,
` --out=${outputFolder}`,
...customOptions,
].filter((o) => o));
if (exitCode === 0 && database) {
// mongodump stores the files into a folder having the same name than the database
yield this.execCommand("mv", [
path_1.default.join(outputFolder, database, "*"),
outputFolder,
]);
yield this.execCommand("rm", ["-rf", path_1.default.join(outputFolder, database)]);
}
return exitCode;
});
}
/**
* Restore a database from the given folder
*/
restore(inputFolder) {
return __awaiter(this, void 0, void 0, function* () {
yield this.checkCommandExists("mongorestore", ["--version"]);
const { database, hostname, port, uri, username, password, mongorestore: mongorestoreOptions, } = this.config;
const customOptions = this.getCustomOptions(mongorestoreOptions);
this.logInfo(`Restore ${database} on ${hostname || uri}`);
return this.execCommand("mongorestore", [
"--drop",
this.config.verbose ? "" : "--quiet",
username && !uri ? `--username=${username}` : "",
password && !uri ? `--password=${password}` : "",
hostname && !uri ? `--host=${hostname}` : "",
port && !uri ? `--port=${port}` : "",
uri ? `'${uri}'` : ` --db=${database}`,
inputFolder,
...customOptions,
].filter((o) => o));
});
}
/**
* List all tables in the database
* @returns
*/
listTables() {
return __awaiter(this, void 0, void 0, function* () {
yield this.checkCommandExists("mongo", ["--version"]);
const { database, hostname, port, uri, username, password, mongo: mongoOptions, } = this.config;
const customOptions = this.getCustomOptions(mongoOptions);
return this.execCommand("mongo", [
port && !uri ? `--port=${port}` : "",
username && !uri ? `--username=${username}` : "",
password && !uri ? `--password=${password}` : "",
hostname && !uri ? `--host=${hostname}` : "",
`--eval='printjson(db.getCollectionNames())'`,
uri ? `'${uri}'` : database,
...customOptions,
].filter((o) => o));
});
}
}
exports.default = MongoDb;