UNPKG

typescript-mysql-model

Version:

{ "version": "1.3.0", "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": "

64 lines 2.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const changeCase = require("change-case"); const fs = require("fs"); const ql_enum_builder_1 = require("./ql-enum-builder"); class EnumBuilder { run(schema, outputDir, qlOutputDir) { const tableEnums = []; for (const tableKey in schema.tables) { const table = schema.tables[tableKey]; const enums = this.enumArr(table, tableKey); tableEnums.push(...enums); this.renderEnumString(enums, tableKey, outputDir); ql_enum_builder_1.QlEnumBuilder.render(enums, tableKey, qlOutputDir); } // for (const tableKey in schema.views) { // const table = schema.views[tableKey]; // let enums = this.enumArr(table, tableKey); // this.renderEnumString(enums, tableKey, outputDir); // } } renderEnumString(enums, tableName, outputDir) { const enumStrArr = enums.map(e => this.createEnum(e.field, e.options)); if (enumStrArr.length) { const wrapper = this.wrapInNameSpace(tableName, enumStrArr); const path = outputDir + "/" + changeCase.paramCase(tableName) + ".generated.ts"; fs.writeFileSync(path, wrapper); } return enumStrArr; } enumArr(table, tableName) { const enums = []; for (const colKey in table) { const column = table[colKey]; if (column.type === "enum" && column.enumValues) { enums.push({ field: column.field, table: tableName, options: column.enumValues }); } } return enums; } wrapInNameSpace(table, enumStr) { const name = changeCase.constantCase(table); return `export namespace ${name} { ${enumStr.join("\n\n")} }`; } createEnum(column, values) { // export namespace COMMENT { // export enum PARENT_TYPE { // STUDENT = "student" // } // }; const rows = values.map(v => `${v} = "${v}"`); return `export enum ${changeCase.constantCase(column)} { \t\t${rows.join(",\n\t\t")} }`; } } exports.EnumBuilder = EnumBuilder; //# sourceMappingURL=enum-builder.js.map