UNPKG

mongoose-management

Version:
88 lines (87 loc) 4.07 kB
"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 chalk_1 = __importDefault(require("chalk")); const inquirer_1 = require("inquirer"); const table_1 = require("table"); const prompts_1 = require("../../prompts"); const abstract_1 = __importDefault(require("./abstract")); const column_1 = __importDefault(require("./column")); class CollectionMenu extends abstract_1.default { constructor(prompts) { super(prompts); this.prompts = prompts; this.columnMenu = new column_1.default(prompts); } exec(collection) { return __awaiter(this, void 0, void 0, function* () { const choicesColumns = this.columnMenu.getChoiceList(collection.flatColumns()); const choicesIndexes = this.getChoiceIndexList(collection.getIndexes()); const result = yield this.prompts.menu('Choose a column/index or a command:', [ new inquirer_1.Separator(`Collection: ${chalk_1.default.bold(collection.getName())}`), new inquirer_1.Separator(' '), new inquirer_1.Separator(chalk_1.default.underline('Columns list')), new inquirer_1.Separator(' '), ...choicesColumns, new inquirer_1.Separator(chalk_1.default.underline('Indexes list')), new inquirer_1.Separator(' '), ...choicesIndexes, new inquirer_1.Separator(), this.getMenuChoiceCreate('column', 'createColumn'), this.getMenuChoiceCreate('index', 'createIndex'), this.getMenuChoiceEdit('collection'), this.getMenuChoiceRemove('collection'), this.getMenuChoiceBack(), new inquirer_1.Separator(' '), ]); return result; }); } getChoiceIndexList(indexes) { const rows = this.createIndexTable(indexes); const choices = []; const indexChoices = indexes.map((d, i) => { if (d.isReadonly()) { return new inquirer_1.Separator(rows[i + 1]); } return { name: rows[i + 1], value: { data: d }, short: `Index - ${d.getName()}`, }; }); if (indexChoices.length === 0) { choices.push(new inquirer_1.Separator('- No indexes defined -')); } else { choices.push(new inquirer_1.Separator(rows[0])); choices.push(...indexChoices); } choices.push(new inquirer_1.Separator(' ')); return choices; } createIndexTable(indexes) { const header = ['Name', 'Column(s)', 'Unique', 'Sparse']; const values = indexes.map((index) => [ index.getName(), index .getColumns() .map(([k, v]) => `${k.getFullname(false, false)}: ${v}`) .join(', '), index.getProperty('unique') ? '✔' : '', index.getProperty('sparse') ? '✔' : '', ]); return table_1.table([header.map((s) => chalk_1.default.underline(s)), ...values], Object.assign({}, prompts_1.promptTableOptions, { columns: { 2: { alignment: 'center' }, 3: { alignment: 'center' } } })).split('\n'); } } exports.default = CollectionMenu;