mongoose-management
Version:
Mongoose schemas management tool
65 lines (64 loc) • 2.53 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const mongo_1 = require("../../mongo");
const specialTypeKeys = Object.keys(mongo_1.schemaTypesSpecial);
exports.call = (prompts, answersMain, column) => __awaiter(this, void 0, void 0, function* () {
if (answersMain.type === '2dsphere') {
return { type: 'index', value: '2dsphere' };
}
if (specialTypeKeys.indexOf(answersMain.type) >= 0) {
return { type: 'no' };
}
const questions = exports.getQuestions(column);
const answersIndex = yield prompts.call(questions);
return answersIndex;
});
exports.getQuestions = (column) => {
const value = column && column.getIndexValue();
const cType = column && column.getIndexType();
const typeValue = cType ? Object.keys(mongo_1.schemaIndexTypes).indexOf(cType) : undefined;
const typeValues = Object.entries(mongo_1.schemaIndexTypes).map(([k, v]) => ({
name: v,
value: k,
short: v,
}));
const valueValue = value ? mongo_1.indexColumnValues.indexOf(value) : undefined;
const valueValues = mongo_1.indexColumnValues;
return [
{
type: 'list',
name: 'type',
message: 'Choose a index',
choices: typeValues,
default: typeValue,
},
{
type: 'list',
name: 'value',
message: 'Choose a index type',
choices: valueValues,
default: valueValue,
when: exports.whenValue,
},
];
};
exports.evaluation = (answers) => {
return (column) => {
if (answers.type === 'no' || typeof answers.value === 'undefined') {
column.removeIndex();
}
else {
column.setIndex(answers.value, answers.type);
}
return column;
};
};
exports.whenValue = ({ type }) => typeof type === 'string' && type !== 'no';