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": "
132 lines (127 loc) • 4.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const changeCase = require("change-case");
const Handlebars = require("handlebars");
const enum_matcher_1 = require("./enums/enum-matcher");
const template = `/**
* Autogenerated interface, DO NOT MODIFY
*/
/* tslint:disable */
import { {{imports}} } from "graphql";
{{{extraImportStr}}}
const {{name}}TypeFields = {
{{fields}}
};
const {{name}}InputType = new GraphQLInputObjectType({
fields: {{name}}TypeFields,
name: "{{name}}Input"
});
const {{name}}Type = new GraphQLObjectType({
fields: {{name}}TypeFields,
name: "{{name}}"
});
export { {{name}}Type, {{name}}InputType, {{name}}TypeFields };
`;
const GRAPH_QL_DATE_TIME = "GraphQLDateTime";
const GRAPH_QL_STRING = "GraphQLString";
class GraphQlBuilder {
constructor(schema) {
this.schema = schema;
this.matcher = new enum_matcher_1.EnumMatcher();
this.compiledTemplate = Handlebars.compile(template);
this.mysqlTypes = {
blob: "string",
bigint: "int",
char: "string",
date: "datetime",
enum: "enum",
datetime: "datetime",
decimal: "float",
double: "float",
float: "float",
int: "int",
longblob: "string",
longtext: "string",
mediumtext: "string",
set: "string",
smallint: "int",
text: "string",
timestamp: "datetime",
tinyint: "boolean",
varchar: "string"
};
}
renderTs(table, tableClass) {
let stdTypes = new Set(["GraphQLObjectType", "GraphQLInputObjectType"]);
let extraImports = new Set();
Object.keys(table).forEach(colName => {
var _a, _b, _c;
const column = table[colName];
if ((_a = column.enumValues) === null || _a === void 0 ? void 0 : _a.length) {
let importTable = tableClass.tableName;
let importColumn = column.field;
if (!tableClass.isTable) {
const eh = this.matcher.run(this.schema, column, tableClass.tableName);
importTable = (_b = eh) === null || _b === void 0 ? void 0 : _b.table;
importColumn = (_c = eh) === null || _c === void 0 ? void 0 : _c.field;
}
extraImports.add(this.importTableStatement(importTable, [importColumn]));
return;
}
const qlType = this.toGraphType(column.type);
if (qlType === GRAPH_QL_DATE_TIME) {
extraImports.add('import { GraphQLDateTime } from "graphql-iso-date";');
}
else {
stdTypes.add(qlType);
}
});
const imports = [...stdTypes].sort().join(", ");
const extraImportStr = [...extraImports].join("\n");
const rows = Object.keys(table).map(colName => this.buildTypeRow(table[colName], tableClass));
const fields = rows.join(", \n \t\t");
const name = changeCase.pascalCase(tableClass.tableName);
const t = this.compiledTemplate({ fields, name, imports, extraImportStr });
return t;
}
importTableStatement(tableName, columnNames) {
const PTABLE = changeCase.paramCase(tableName);
const vars = columnNames.map(c => changeCase.pascalCase(c) + "Enum").join(", ");
return `import { ${vars} } from "./enums/${PTABLE}-ql-enums.generated.ts";`;
}
toGraphType(mysql) {
const s = this.mysqlTypes[mysql];
switch (s) {
case "string":
return GRAPH_QL_STRING;
case "float":
return "GraphQLFloat";
case "datetime":
return GRAPH_QL_DATE_TIME;
case "int":
return "GraphQLInt";
case "boolean":
return "GraphQLBoolean";
default:
throw "unknown type " + mysql;
}
}
buildTypeRow(column, tableClass) {
var _a, _b;
let graphType = "";
if ((_a = column.enumValues) === null || _a === void 0 ? void 0 : _a.length) {
let importColumn = column.field;
if (!tableClass.isTable) {
const eh = this.matcher.run(this.schema, column, tableClass.tableName);
importColumn = (_b = eh) === null || _b === void 0 ? void 0 : _b.field;
}
graphType = changeCase.pascalCase(importColumn) + "Enum";
}
else {
graphType = this.toGraphType(column.type);
}
return `${column.field}: { type: ${graphType} }`;
}
}
exports.GraphQlBuilder = GraphQlBuilder;
//# sourceMappingURL=graphql-builder.js.map