mongoose-management
Version:
Mongoose schemas management tool
123 lines (122 loc) • 4.73 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(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 group_1 = __importDefault(require("../dataset/group"));
const errors_1 = require("../errors");
class AbstractLevel {
constructor(dataset, menu, options) {
this.dataset = dataset;
this.menu = menu;
this.options = options;
this.prompts = options.prompts;
}
showMenu() {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this.menu.exec(this.dataset);
return result;
});
}
create(action) {
return __awaiter(this, void 0, void 0, function* () {
const dataset = yield this.promptCreate(this.prompts, this.dataset);
return dataset;
});
}
edit(dataset) {
return __awaiter(this, void 0, void 0, function* () {
const parent = dataset.getParent();
if (!parent) {
throw Error('Parent dataset is not defined');
}
yield this.promptEdit(this.prompts, parent, dataset);
return false;
});
}
remove(dataset) {
return __awaiter(this, void 0, void 0, function* () {
const confirm = yield this.prompts.remove(dataset.getName());
if (confirm) {
dataset.remove();
}
return !confirm;
});
}
show(dataset) {
throw new Error('Next menu level is not defined');
}
exec() {
return __awaiter(this, void 0, void 0, function* () {
let status;
do {
this.prompts.clear();
status = yield this.run();
} while (status);
});
}
// tslint:disable-next-line:cyclomatic-complexity
run() {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this.showMenu();
let status = true;
try {
switch (result.action) {
case 'exit':
yield this.prompts.exit();
break;
case 'back':
return false;
case 'backToCollection':
throw new errors_1.BackToCollectionError('back');
case 'write':
const d = this.dataset;
if (d instanceof group_1.default) {
yield this.options.storage.write(false);
yield this.options.creater.exec(d.getPath(), d.getObject().collections);
}
break;
case 'save':
yield this.options.storage.write();
break;
case 'remove':
status = yield this.remove(this.dataset);
break;
case 'edit':
status = yield this.edit(this.dataset);
break;
case 'create':
case 'createColumn':
case 'createIndex':
const dataset = yield this.create(result.action);
if (dataset) {
yield this.show(dataset);
}
break;
default:
if (result.data) {
yield this.show(result.data);
}
}
}
catch (err) {
if (!(err instanceof Error) || err instanceof errors_1.BackToCollectionError) {
throw err;
}
if (!(err instanceof errors_1.CancelPromptError)) {
yield this.prompts.pressKey(err);
}
}
return status;
});
}
}
exports.default = AbstractLevel;