1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const changeCase = require("change-case");
|
4 | const enum_matcher_1 = require("./enums/enum-matcher");
|
5 | class 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.buildTypeRow(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 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | const bestMatches = extraImports.map(c => this.matcher.run(this.schema, c, tableClass.tableName));
|
32 | bestMatches.forEach(b => {
|
33 | let relativePath = "../enums/" + changeCase.paramCase(b.table) + ".generated";
|
34 | importStatements.add(this.importStatement([changeCase.constant(b.table)], relativePath));
|
35 | });
|
36 |
|
37 | }
|
38 | const importStr = [...importStatements].join("");
|
39 | return this.getMetaText() + "\n" + importStr + "\n" + stringBuilder;
|
40 | }
|
41 | importStatement(fieldNames, relativePath) {
|
42 | return `import { ${fieldNames.join(", ")} } from "${relativePath}"\n`;
|
43 | }
|
44 | getMetaText() {
|
45 | let meta = `/**
|
46 | * Auto generated, do not modify!
|
47 | */
|
48 | /* eslint-disable */
|
49 | `;
|
50 | return meta;
|
51 | }
|
52 | buildTypeRow(col, colName) {
|
53 | const tabs = "\t";
|
54 | const optional = this.settings.optionalParameters ? "?" : "";
|
55 | const tsType = this.getTsType(col, colName);
|
56 | const field = col.field;
|
57 | return `${tabs}${field}${optional}: ${tsType};\n`;
|
58 | }
|
59 | ;
|
60 | isEnum(col) {
|
61 | var _a;
|
62 | return !!((_a = col.enumValues) === null || _a === void 0 ? void 0 : _a.length);
|
63 | }
|
64 | getTsType(col, colName) {
|
65 | var _a;
|
66 | const maybeNull = col.null === "YES" ? " | null" : "";
|
67 | if (this.isEnum(col)) {
|
68 | const matches = this.matcher.run(this.schema, col, colName);
|
69 | if (!matches) {
|
70 | throw new Error("No matching column");
|
71 | }
|
72 | if ((_a = matches.replacementFor) === null || _a === void 0 ? void 0 : _a.length) {
|
73 | return changeCase.constantCase(matches.field) + maybeNull;
|
74 | }
|
75 | return changeCase.constantCase(matches.table) + "." + changeCase.constantCase(matches.field) + maybeNull;
|
76 | }
|
77 | let ts = this.mysqlTypes[col.type];
|
78 | if (!ts) {
|
79 |
|
80 | console.error("Unknown type " + col.type);
|
81 | return "unknown";
|
82 | }
|
83 | return ts + maybeNull;
|
84 | }
|
85 | }
|
86 | exports.InterfaceBuilder = InterfaceBuilder;
|
87 |
|
\ | No newline at end of file |