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":
50 lines • 1.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const changeCase = require("change-case");
const fs = require("fs");
class EnumBuilder {
run(schema, outputDir) {
for (const tableKey in schema.tables) {
const table = schema.tables[tableKey];
this.renderEnumString(table, tableKey, outputDir);
}
for (const tableKey in schema.views) {
const table = schema.views[tableKey];
this.renderEnumString(table, tableKey, outputDir);
}
}
renderEnumString(table, tableName, outputDir) {
const enumStrArr = [];
for (const colKey in table) {
const column = table[colKey];
if (column.type === "enum" && column.enumValues) {
enumStrArr.push(this.createEnum(column.field, column.enumValues));
}
}
if (enumStrArr.length) {
// const wrapper = this.wrapInNameSpace(tableKey, enumStr);
const path = outputDir + "/" + changeCase.paramCase(tableName) + ".generated.ts";
fs.writeFileSync(path, enumStrArr.join("\n"));
}
return enumStrArr;
}
// private wrapInNameSpace(table: string, enumStr: string[]) {
// 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${rows.join(",\n\t")}
}`;
}
}
exports.EnumBuilder = EnumBuilder;
//# sourceMappingURL=enum-builder.js.map