UNPKG

mongoose-management

Version:
105 lines (104 loc) 4.84 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 collection_1 = __importDefault(require("../dataset/collection")); class ColumnMenu extends abstract_1.default { exec(column) { return __awaiter(this, void 0, void 0, function* () { const choices = this.getChoiceList(column.flatColumns(), column); let result; if (column.get('type') === 'array' || column.get('type') === 'object') { const menuChoices = [ this.getMenuChoiceCreate('subcolumn'), this.getMenuChoiceEdit('column'), this.getMenuChoiceRemove('column'), ]; if (!(column.getParent() instanceof collection_1.default)) { menuChoices.push(this.getMenuChoiceBackToCollection()); } result = yield this.prompts.menu(`Choose a subcolumn or a command for the column "${column.getFullname(true)}":`, [ new inquirer_1.Separator(chalk_1.default.underline('Columns list')), new inquirer_1.Separator(' '), ...choices, ...menuChoices, this.getMenuChoiceBack(), new inquirer_1.Separator(' '), ]); } else { result = yield this.prompts.menu(`Choose a command for the column "${column.getFullname(true)}":`, [ this.getMenuChoiceEdit('column'), this.getMenuChoiceRemove('column'), this.getMenuChoiceBack(), new inquirer_1.Separator(' '), ]); } return result; }); } getChoiceList(columns, selected) { const rows = this.createTable(columns, selected); const choices = []; const columnChoices = columns.map((d, i) => { const parent = d.getParent(); if (d.isReadonly() || (parent !== selected && !(parent instanceof collection_1.default))) { return new inquirer_1.Separator(rows[i + 1]); } return { name: rows[i + 1], value: { data: d }, short: `Column - ${d.getName()}`, }; }); if (columnChoices.length === 0) { choices.push(new inquirer_1.Separator('- No columns defined -')); } else { choices.push(new inquirer_1.Separator(rows[0])); choices.push(...columnChoices); } choices.push(new inquirer_1.Separator(' ')); if (!selected) { choices.push(new inquirer_1.Separator('Note: Columns "_id", "createdAt" and "updatedAt" are created automatically')); choices.push(new inquirer_1.Separator(' ')); } return choices; } createTable(columns, selected) { const header = ['Name', 'Type', 'Required', 'Default', 'Reference', 'Index', 'Unique', 'Sparse']; const values = columns.map((c) => [ c.getTableName(selected), c.getTableType(), c.get('required') ? '✔' : '', c.get('default'), c.getPopulateName(), ...this.createTableIndexRow(c), ]); return table_1.table([header.map((s) => chalk_1.default.underline(s)), ...values], Object.assign({}, prompts_1.promptTableOptions, { columns: { 2: { alignment: 'center' }, 6: { alignment: 'center' }, 7: { alignment: 'center' }, } })).split('\n'); } createTableIndexRow(column) { const value = column.getIndexValue(); const type = column.getIndexType(); return [value, type === 'unique' ? '✔' : '', type === 'sparse' ? '✔' : '']; } } exports.default = ColumnMenu;