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 | * Autogenerated, do not modify
|
8 | */
|
9 | /* tslint: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, limit?: number): 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 | if(limit){
|
40 | query.limit(limit);
|
41 | }
|
42 | return await query;
|
43 | }
|
44 |
|
45 | private async countTable(tableName: string, fn?: (knex: Knex.QueryBuilder) => Knex.QueryBuilder | void): Promise<number> {
|
46 | let query = this.knex(tableName).select(this.knex.raw("count(*) as c"));
|
47 | if (fn) {
|
48 | const subQuery = fn(query);
|
49 | if(subQuery){
|
50 | query = subQuery;
|
51 | }
|
52 | }
|
53 | const reply: Array<{ c: number }> = await query;
|
54 | return reply[0].c;
|
55 | }
|
56 |
|
57 | {{#each getters}}{{{this}}}{{/each}}
|
58 | {{#each counters}}{{{this}}}{{/each}}
|
59 | {{#each singulars}}{{{this}}}{{/each}}
|
60 | }`;
|
61 | const GET_TEMPLATE = `
|
62 | public list{{fnName}}(fn?: (knex: Knex.QueryBuilder) => Knex.QueryBuilder | void, limit?: number): Promise<{{prefixedClassName}}[]> {
|
63 | return this.getFromTable("{{tableName}}", fn, limit);
|
64 | }
|
65 | `;
|
66 | const GET_SINGULAR = `
|
67 | public getSingle{{fnName}}({{#each params}}{{{this}}}{{#unless @last}},{{/unless}}{{/each}}, fn?: (knex: Knex.QueryBuilder) => Knex.QueryBuilder | void): Promise<{{prefixedClassName}}> {
|
68 | return this.getSingle("{{tableName}}", { {{#each fields}}{{{this}}}{{#unless @last}},{{/unless}}{{/each}} } ,fn);
|
69 | }
|
70 | `;
|
71 | const COUNT_TEMPLATE = `
|
72 | public count{{fnName}}(fn?: (knex: Knex.QueryBuilder) => Knex.QueryBuilder | void): Promise<number> {
|
73 | return this.countTable("{{tableName}}", fn);
|
74 | }
|
75 | `;
|
76 | class GettersBuilder extends schema_operator_1.SchemaOperator {
|
77 | constructor(model, typeMap) {
|
78 | super();
|
79 | this.typeMap = typeMap;
|
80 | this.definition = model;
|
81 | this.compiledTemplate = handlebars.compile(TEMPLATE);
|
82 | this.compiledGetTemplate = handlebars.compile(GET_TEMPLATE);
|
83 | this.compiledCountInsertTemplate = handlebars.compile(COUNT_TEMPLATE);
|
84 | this.compailedGetSingularTemplate = handlebars.compile(GET_SINGULAR);
|
85 | }
|
86 | render(tables, relativePath = "./") {
|
87 | tables = [...tables];
|
88 | tables.forEach(t => {
|
89 | t.fnName = change_case.upperCaseFirst(t.fnName);
|
90 | t.fnPlural = change_case.upperCaseFirst(t.fnPlural);
|
91 | });
|
92 | const input = {
|
93 | getters: tables.map(t => this.compiledGetTemplate(t)),
|
94 | imports: tables.map(t => this.renderImportRow(t, relativePath)),
|
95 | counters: tables.map(t => this.compiledCountInsertTemplate(t)),
|
96 | singulars: tables.filter(t => t.isTable).map(t => this.compailedGetSingularTemplate(this.renderGetSingularTemplateInput(t)))
|
97 | };
|
98 | return this.compiledTemplate(input);
|
99 | }
|
100 | renderGetSingularTemplateInput(t) {
|
101 | let d = t;
|
102 | d["fields"] = this.getPrimaryKeyNames(t.tableName);
|
103 | d["params"] = this.getPkCols(t.tableName).map(col => {
|
104 | return col.field + ": " + this.typeMap.get(col.type);
|
105 | });
|
106 | return d;
|
107 | }
|
108 | renderImportRow(table, relativePath) {
|
109 | table = Object.assign({}, table);
|
110 | table.filename = table.filename.replace(".ts", "");
|
111 | return `import {${table.prefixedClassName}} from "${relativePath}${table.filename}"`;
|
112 | }
|
113 | }
|
114 | exports.GettersBuilder = GettersBuilder;
|
115 |
|
\ | No newline at end of file |