UNPKG

5.19 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const changeCase = require("change-case");
4const enum_matcher_1 = require("./enums/enum-matcher");
5class InterfaceBuilder {
6 constructor(settings, mysqlTypes, schema) {
7 this.settings = settings;
8 this.mysqlTypes = mysqlTypes;
9 this.schema = schema;
10 this.matcher = new enum_matcher_1.EnumMatcher();
11 }
12 renderTs(tableClass, table) {
13 const extraImports = [];
14 const maybeEquals = tableClass.useInterface === "type" ? "=" : "";
15 let stringBuilder = `${this.settings.defaultClassModifier} ${tableClass.useInterface} ${tableClass.prefixedClassName} ${maybeEquals} { \n`;
16 for (const colName in table) {
17 const col = table[colName];
18 stringBuilder += this.variableTypeRow(col, colName);
19 if (this.isEnum(col)) {
20 extraImports.push(col);
21 }
22 }
23 stringBuilder += "}\n";
24 const importStatements = new Set();
25 if (extraImports.length) {
26 const bestMatches = extraImports.map(c => this.matcher.run(this.schema, c, tableClass.tableName));
27 bestMatches.forEach(b => {
28 let relativePath = "../enums/" + changeCase.paramCase(b.table) + ".generated";
29 importStatements.add(this.importStatement([changeCase.constant(b.table)], relativePath));
30 });
31 }
32 const inputSection = tableClass.isTable ? "\n\n" + this.renderInsertType(tableClass, table) + "\n\n" + this.renderUpdateType(tableClass, table) : "";
33 const importStr = [...importStatements].join("");
34 return this.getMetaText() + "\n" + importStr + "\n" + stringBuilder + inputSection;
35 }
36 importStatement(fieldNames, relativePath) {
37 return `import { ${fieldNames.join(", ")} } from "${relativePath}"\n`;
38 }
39 getMetaText() {
40 let meta = `/**
41 * Auto generated, do not modify!
42 */
43/* eslint-disable */
44`;
45 return meta;
46 }
47 variableTypeRow(col, colName) {
48 const tabs = "\t";
49 const optional = this.settings.optionalParameters ? "?" : "";
50 const tsType = this.getTsType(col, colName);
51 const field = col.field;
52 return `${tabs}${field}${optional}: ${tsType};\n`;
53 }
54 variableTypeRowInsertItem(col, colName) {
55 const tabs = "\t";
56 const optional = (col.null === "YES" || col.default !== null) ? "?" : "";
57 const tsType = this.getTsType(col, colName);
58 const field = col.field;
59 return `${tabs}${field}${optional}: ${tsType};\n`;
60 }
61 variableTypeRowUpdateItem(col, colName) {
62 const tabs = "\t";
63 const optional = (col.isPrimary) ? "" : "?";
64 const tsType = this.getTsType(col, colName);
65 const field = col.field;
66 return `${tabs}${field}${optional}: ${tsType};\n`;
67 }
68 isEnum(col) {
69 var _a;
70 return !!((_a = col.enumValues) === null || _a === void 0 ? void 0 : _a.length);
71 }
72 getTsType(col, colName) {
73 var _a;
74 const maybeNull = col.null === "YES" ? " | null" : "";
75 if (this.isEnum(col)) {
76 const matches = this.matcher.run(this.schema, col, colName);
77 if (!matches) {
78 throw new Error("No matching column");
79 }
80 if ((_a = matches.replacementFor) === null || _a === void 0 ? void 0 : _a.length) {
81 return changeCase.constantCase(matches.field) + maybeNull;
82 }
83 return changeCase.constantCase(matches.table) + "." + changeCase.constantCase(matches.field) + maybeNull;
84 }
85 let ts = this.mysqlTypes[col.type];
86 if (!ts) {
87 // tslint:disable-next-line:no-console
88 console.error("Unknown type " + col.type);
89 return "unknown";
90 }
91 return ts + maybeNull;
92 }
93 renderUpdateType(tableClass, table) {
94 const maybeEquals = tableClass.useInterface === "type" ? "=" : "";
95 let stringBuilder = `${this.settings.defaultClassModifier} ${tableClass.useInterface} ${tableClass.className}UpdateType ${maybeEquals} { \n`;
96 for (const colName in table) {
97 const col = table[colName];
98 if (col.default !== "CURRENT_TIMESTAMP") {
99 stringBuilder += this.variableTypeRowUpdateItem(col, colName);
100 }
101 }
102 stringBuilder += "}\n";
103 return stringBuilder;
104 }
105 renderInsertType(tableClass, table) {
106 const maybeEquals = tableClass.useInterface === "type" ? "=" : "";
107 let stringBuilder = `${this.settings.defaultClassModifier} ${tableClass.useInterface} ${tableClass.className}InsertType ${maybeEquals} { \n`;
108 for (const colName in table) {
109 const col = table[colName];
110 const remove = col.extra === "auto_increment" || col.default === "CURRENT_TIMESTAMP";
111 if (!remove) {
112 stringBuilder += this.variableTypeRowInsertItem(col, colName);
113 }
114 }
115 stringBuilder += "}\n";
116 return stringBuilder;
117 }
118}
119exports.InterfaceBuilder = InterfaceBuilder;
120//# sourceMappingURL=interface-builder.js.map
\No newline at end of file