UNPKG

mongoose-management

Version:
81 lines (80 loc) 2.41 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const _2dsphere_1 = __importDefault(require("./converters/2dsphere")); const array_1 = __importDefault(require("./converters/array")); const arrayType_1 = __importDefault(require("./converters/arrayType")); const common_1 = __importDefault(require("./converters/common")); const object_1 = __importDefault(require("./converters/object")); /** * */ class Converter { constructor() { this.converter2dSphere = new _2dsphere_1.default(this); this.converterArray = new array_1.default(this); this.converterArrayType = new arrayType_1.default(this); this.converterCommon = new common_1.default(this); this.converterObject = new object_1.default(this); } /** * * @param columns */ getDefinitions(columns) { return this.converterObject.columnToDefinitions(columns); } /** * * @param columns */ getTypes(columns) { return this.converterObject.columnToTypes(columns); } getVirtuals(columns) { return ''; } /** * */ getIndexes(indexes) { return `[${indexes.map((index) => this.convertIndex(index)).join(', ')}]`; } /** * * @param index */ convertIndex(index) { const fields = Object.entries(index.columns).map(this.convertIndexField); const options = Object.entries(index.properties) .map(this.convertIndexOption) .filter(Boolean); return `{ fields: { ${fields.join(', ')} }, options: { name: '${index.name}', ${options.join(', ')} } }`; } /** * * @param param0 */ convertIndexField([key, value]) { if (Number.isInteger(value)) { return `'${key}': ${value}`; } return `'${key}': '${value}'`; } /** * * @param param0 */ convertIndexOption([key, value]) { switch (true) { case typeof value === 'undefined' || typeof value === 'boolean': return value === true ? `'${key}': true` : undefined; break; default: throw new Error('Index options type is unknown'); } } } exports.default = Converter;