UNPKG

typescript-mysql-model

Version:

{ "version": "1.2.46", "name": "typescript-mysql-model", "description": "", "main": "index.js", "types": "index.d.ts", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", "url":

58 lines 2.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const changeCase = require("change-case"); class InterfaceBuilder { constructor(settings, mysqlTypes) { this.settings = settings; this.mysqlTypes = mysqlTypes; } renderTs(tableClass, table) { const extraImports = []; let stringBuilder = this.settings.defaultClassModifier + " " + tableClass.prefixedClassName + " { \n"; for (const colName in table) { const col = table[colName]; stringBuilder += this.buildTypeRow(col); if (this.isEnum(col)) { extraImports.push(changeCase.constant(col.field)); } } stringBuilder += "}\n"; let importState = ""; if (extraImports.length) { const reltaivePath = "../enums/" + changeCase.paramCase(tableClass.tableName) + ".generated"; importState = `\nimport { ${extraImports.join(", ")} } from "${reltaivePath}"\n\n`; } return this.getMetaText() + importState + stringBuilder; } getMetaText() { let meta = `/**\n * Autogenerated interface, DO NOT MODIFY\n */\n`; meta += "/* tslint:disable */\n"; return meta; } buildTypeRow(col) { const tabs = "\t"; const optional = this.settings.optionalParameters ? "?" : ""; const tsType = this.getTsType(col); const field = col.field; return `${tabs}"${field}"${optional}: ${tsType};\n`; } ; isEnum(col) { var _a; return !!((_a = col.enumValues) === null || _a === void 0 ? void 0 : _a.length); } getTsType(col) { if (this.isEnum(col)) { return changeCase.constantCase(col.field); } let ts = this.mysqlTypes[col.type]; if (!ts) { // tslint:disable-next-line:no-console console.error("Unknown type " + col.type); return "unknown"; } return ts; } } exports.InterfaceBuilder = InterfaceBuilder; //# sourceMappingURL=interface-builder.js.map