mongoose-management
Version:
Mongoose schemas management tool
82 lines (81 loc) • 3.35 kB
JavaScript
;
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 mongo_1 = require("../../mongo");
const prompts_1 = require("../../prompts");
const index_1 = __importDefault(require("../dataset/index"));
const errors_1 = require("../errors");
exports.call = (prompts, collection, index) => __awaiter(this, void 0, void 0, function* () {
const questions = exports.getQuestions(collection, index);
const answersMain = yield prompts.call(questions);
if (answersMain.name === '' || answersMain.columns.length === 0) {
throw new errors_1.CancelPromptError('cancel');
}
return answersMain;
});
exports.getQuestions = (collection, index) => {
const indexes = collection.getIndexes();
const nameValue = index && index.getName();
const nameValues = indexes.filter((d) => d !== index).map((d) => d.getName().toLowerCase());
const columnNames = index ? index.getColumns().map(([column]) => column.getFullname(false, false)) : [];
const columnValues = collection.flatColumns().map((column) => exports.getChoiceItem(column, columnNames));
return [
{
type: 'input',
name: 'name',
message: 'Index name:',
default: nameValue,
validate: exports.validateName(nameValues),
},
{
type: 'checkbox',
name: 'columns',
message: 'Choose a columns:',
choices: columnValues,
when: exports.whenColumns(),
},
];
};
exports.evaluation = (answers, collection) => {
return (index) => {
if (!index) {
const data = { name: answers.name, columns: {}, properties: {} };
return collection.addIndex(new index_1.default(data, collection));
}
index.setName(answers.name);
return index;
};
};
exports.getChoiceItem = (column, columns) => {
const name = column.getFullname(false, false);
const disabled = Object.keys(mongo_1.schemaTypesSpecial).indexOf(column.get('type')) >= 0;
const checked = !disabled && columns.length > 0 && columns.indexOf(name) >= 0;
return {
name,
value: column,
short: name,
checked,
disabled,
};
};
exports.validateName = (nameValues) => (value) => {
const name = value.trim();
if (!prompts_1.regexpName.test(name)) {
return prompts_1.regexpNameMessage;
}
if (nameValues.indexOf(name.toLowerCase()) >= 0) {
return 'A index with the name already exists!';
}
return true;
};
exports.whenColumns = () => ({ name }) => name.trim() !== '';