1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const change_case = require("change-case");
|
4 | const handlebars = require("handlebars");
|
5 | const schema_operator_1 = require("./schema-operator");
|
6 | const TEMPLATE = `/**
|
7 | * Auto generated, do not modify!
|
8 | */
|
9 | /* eslint-disable */
|
10 | import * as Knex from "knex";
|
11 | {{#each imports}}{{{this}}}
|
12 | {{/each}}
|
13 |
|
14 | export default class Getter {
|
15 | constructor(private knex: Knex) {
|
16 |
|
17 | }
|
18 |
|
19 | private async getSingle(tableName: string, keyObject: { [key: string]: any }, fn?: (knex: Knex.QueryBuilder) => Knex.QueryBuilder | void): Promise<any> {
|
20 | let query = this.knex(tableName).select().limit(1).where(keyObject);
|
21 | if (fn) {
|
22 | const subQuery = fn(query);
|
23 | if(subQuery){
|
24 | query = subQuery;
|
25 | }
|
26 | }
|
27 | const reply: any[] = await query;
|
28 | return reply.shift();
|
29 | }
|
30 |
|
31 | private async getFromTable(tableName: string, fn?: (knex: Knex.QueryBuilder) => Knex.QueryBuilder | void): Promise<any[]> {
|
32 | let query = this.knex(tableName).select();
|
33 | if (fn) {
|
34 | const subQuery = fn(query);
|
35 | if(subQuery){
|
36 | query = subQuery;
|
37 | }
|
38 | }
|
39 | return await query;
|
40 | }
|
41 |
|
42 | {{#each getters}}{{{this}}}{{/each}}
|
43 | {{#each singulars}}{{{this}}}{{/each}}
|
44 | }`;
|
45 | const GET_TEMPLATE = `
|
46 | public list{{fnName}}(fn?: (knex: Knex.QueryBuilder) => Knex.QueryBuilder | void): Promise<{{prefixedClassName}}[]> {
|
47 | return this.getFromTable("{{tableName}}", fn);
|
48 | }
|
49 | `;
|
50 | const GET_SINGULAR = `
|
51 | public getSingle{{fnName}}({{#each params}}{{{this}}}{{#unless @last}},{{/unless}}{{/each}}, fn?: (knex: Knex.QueryBuilder) => Knex.QueryBuilder | void): Promise<{{prefixedClassName}}> {
|
52 | return this.getSingle("{{tableName}}", { {{#each fields}}{{{this}}}{{#unless @last}},{{/unless}}{{/each}} } ,fn);
|
53 | }
|
54 | `;
|
55 | class GettersBuilder extends schema_operator_1.SchemaOperator {
|
56 | constructor(model, typeMap) {
|
57 | super();
|
58 | this.typeMap = typeMap;
|
59 | this.definition = model;
|
60 | this.compiledTemplate = handlebars.compile(TEMPLATE);
|
61 | this.compiledGetTemplate = handlebars.compile(GET_TEMPLATE);
|
62 | this.compailedGetSingularTemplate = handlebars.compile(GET_SINGULAR);
|
63 | }
|
64 | render(tables, relativePath = "./") {
|
65 | tables = [...tables];
|
66 | tables.forEach(t => {
|
67 | t.fnName = change_case.upperCaseFirst(t.fnName);
|
68 | t.fnPlural = change_case.upperCaseFirst(t.fnPlural);
|
69 | });
|
70 | const input = {
|
71 | getters: tables.map(t => this.compiledGetTemplate(t)),
|
72 | imports: tables.map(t => this.renderImportRow(t, relativePath)),
|
73 | singulars: tables.filter(t => t.isTable).map(t => this.compailedGetSingularTemplate(this.renderGetSingularTemplateInput(t)))
|
74 | };
|
75 | return this.compiledTemplate(input);
|
76 | }
|
77 | renderGetSingularTemplateInput(t) {
|
78 | let d = t;
|
79 | d["fields"] = this.getPrimaryKeyNames(t.tableName);
|
80 | d["params"] = this.getPkCols(t.tableName).map(col => {
|
81 | return col.field + ": " + this.typeMap.get(col.type);
|
82 | });
|
83 | return d;
|
84 | }
|
85 | renderImportRow(table, relativePath) {
|
86 | table = { ...table };
|
87 | table.filename = table.filename.replace(".ts", "");
|
88 | return `import {${table.prefixedClassName}} from "${relativePath}${table.filename}"`;
|
89 | }
|
90 | }
|
91 | exports.GettersBuilder = GettersBuilder;
|
92 |
|
\ | No newline at end of file |