mongoose-management
Version:
Mongoose schemas management tool
200 lines (199 loc) • 7.79 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 columnOpts = {
required: 'boolean',
default: 'any',
lowercase: 'boolean',
uppercase: 'boolean',
trim: 'boolean',
match: 'string',
enum: 'string',
minLength: 'number',
maxLength: 'number',
min: 'number',
max: 'number',
};
exports.call = (prompts, answersMain, column) => __awaiter(this, void 0, void 0, function* () {
const questions = exports.getQuestions(answersMain, column);
if (questions.length === 0) {
return { options: [] };
}
const answersOptions = yield prompts.call(questions);
return answersOptions;
});
exports.getQuestions = (answersMain, column) => {
const choices = [];
const questions = [
{
type: 'checkbox',
name: 'options',
message: 'Choose the scheme options:',
choices,
validate: exports.validateOptions,
},
];
[
exports.getColumnOptionsTypeAny(answersMain, column),
exports.getColumnOptionsTypeString(answersMain, column),
exports.getColumnOptionsTypeNumber(answersMain, column),
].forEach((data) => {
choices.push(...data.choices);
questions.push(...data.questions);
});
return choices.length === 0 ? [] : questions;
};
exports.evaluation = (answers) => {
return (column) => {
Object.entries(columnOpts).forEach(([key, type]) => {
if (type === 'boolean') {
column.set(key, answers.options.indexOf(key) >= 0 ? true : undefined);
}
else {
column.set(key, answers[key]);
}
});
return column;
};
};
exports.getColumnOptionsTypeAny = (answersMain, column) => {
const specialTypes = Object.keys(mongo_1.schemaTypesSpecial);
const choices = [];
const withRequired = column && column.isset('required');
const withDefault = column && column.isset('default');
if (specialTypes.filter((t) => t !== '2dsphere').indexOf(answersMain.type) === -1) {
choices.push({ name: 'required', short: 'required', value: 'required', checked: withRequired });
}
if (specialTypes.indexOf(answersMain.type) === -1) {
choices.push({ name: 'default', short: 'default', value: 'default', checked: withDefault });
}
return {
choices,
questions: [
{
type: 'input',
name: 'default',
message: 'Default value for the column (e.g. Date.now or "Hello World"):',
default: column && column.get('default'),
when: exports.whenCommon('default'),
},
],
};
};
exports.getColumnOptionsTypeString = (answersMain, column) => {
if (answersMain.type !== 'string') {
return { choices: [], questions: [] };
}
const withTrim = column && column.isset('trim');
const withLowerCase = column && column.isset('lowercase');
const withUpperCase = column && column.isset('uppercase');
const withMatch = column && column.isset('match', false);
const withEnum = column && column.isset('enum', false);
const withMinLength = column && column.isset('minLength');
const withMaxLength = column && column.isset('maxLength');
return {
choices: [
{ name: 'enum', short: 'enum', value: 'enum', checked: withEnum },
{ name: 'match (regexp)', short: 'match', value: 'match', checked: withMatch },
{ name: 'trim', short: 'trim', value: 'trim', checked: withTrim },
{ name: 'lowerCase', short: 'lowercase', value: 'lowercase', checked: withLowerCase },
{ name: 'upperCase', short: 'uppercase', value: 'uppercase', checked: withUpperCase },
{ name: 'minLength', short: 'minLength', value: 'minLength', checked: withMinLength },
{ name: 'maxLength', short: 'maxLength', value: 'maxLength', checked: withMaxLength },
],
questions: [
{
type: 'input',
name: 'enum',
message: 'Allowed enum strings (semicolon [;] as separator):',
default: column && column.get('enum'),
when: exports.whenCommon('enum'),
filter: exports.filterEnum,
},
{
type: 'input',
name: 'match',
message: 'RegExp match value (e.g. ^[a-zA-Z0-9]+$ or [a-z]+):',
default: column && column.get('match'),
when: exports.whenCommon('match'),
},
{
type: 'number',
name: 'minLength',
message: 'Minimum number of characters:',
default: column && column.get('minLength'),
when: exports.whenCommon('minLength'),
},
{
type: 'number',
name: 'maxLength',
message: 'Maximum number of characters:',
default: column && column.get('maxLength'),
when: exports.whenCommon('maxLength'),
validate: exports.validateMaxLength,
},
],
};
};
exports.getColumnOptionsTypeNumber = (answersMain, column) => {
if (answersMain.type !== 'number') {
return { choices: [], questions: [] };
}
const withNumberMin = column && column.isset('min');
const withNumberax = column && column.isset('max');
return {
choices: [
{ name: 'min', short: 'min', value: 'min', checked: withNumberMin },
{ name: 'max', short: 'max', value: 'max', checked: withNumberax },
],
questions: [
{
type: 'number',
name: 'min',
message: 'Value must greater than or equal:',
default: column && column.get('min'),
when: exports.whenCommon('min'),
},
{
type: 'number',
name: 'max',
message: 'Value must less than or equal:',
default: column && column.get('max'),
when: exports.whenCommon('max'),
validate: exports.validateMax,
},
],
};
};
exports.whenCommon = (type) => ({ options }) => options.indexOf(type) >= 0;
exports.validateOptions = (v) => {
if (v.indexOf('lowercase') >= 0 && v.indexOf('uppercase') >= 0) {
return 'Either "lowercase" or "uppercase" can be selected!';
}
return true;
};
exports.validateMaxLength = (v, { minLength }) => {
if (minLength && parseInt(minLength, 10) > parseInt(v, 10)) {
return `Length must be greater or equal than to minimum length (>= ${minLength})!`;
}
return true;
};
exports.validateMax = (v, { min }) => {
if (min && parseInt(min, 10) > parseInt(v, 10)) {
return `Value must be greater or equal than to minimum value (>= ${min})!`;
}
return true;
};
exports.filterEnum = (value) => value
.split(';')
.map((s) => s.trim())
.filter((s) => s !== '')
.join('; ');