ts-sql-codegen
Version:
Generates ts-sql-query table mappings from tbls schema output
165 lines (164 loc) • 8.23 kB
TypeScript
import * as z from "zod";
import { GeneratedFieldType, ImportedItem } from "./field-mappings";
import { GeneratorOpts, GeneratorOptsSchema, NamingOptions } from "./generator-options";
import { Logger } from "./logger";
import { Column, Table } from "./tbls-types";
type ColumnMethod = "column" | "optionalColumn" | "columnWithDefaultValue" | "optionalColumnWithDefaultValue" | "computedColumn" | "optionalComputedColumn" | "primaryKey" | "autogeneratedPrimaryKey";
interface FieldTmplInput {
name: string;
isPK: boolean;
columnMethod: ColumnMethod;
columnName: string;
isOptional: boolean;
hasDefault: boolean;
fieldType: GeneratedFieldType;
includeDBTypeWhenIsOptional: boolean;
}
interface ImportTmplInput {
importPath: string;
imported: string[];
isDefault: boolean;
}
interface RepoInput {
className: string;
methods: Record<string, string>;
}
/**
* Generator class for programmatic codegen.
*
* Most common usage involves creating an instance and calling generate function:
*
* ```ts
* const options = {
* schemaPath: './schema.yaml',
* connectionSourcePath: './connection-source.ts'
* }
* const generator = new Generator(options);
* await generator.generate();
* ```
*
* See [GeneratorOpts](../interfaces/GeneratorOpts.md) for configuration options.
*
* For advanced use-cases, you can extend this class.
* This enables you to use custom templates, pre/post processing of generated code
* and custom logic for table/column/field mapping.
*/
export declare class Generator {
protected opts: z.output<typeof GeneratorOptsSchema>;
protected naming: NamingOptions;
private writtenFiles;
logger: Logger;
constructor(opts: GeneratorOpts);
protected getFieldMappings: (() => {
generatedField: false | {
type?: {
kind?: "custom" | "customComparable" | "enum" | "customInt" | "customDouble" | "customUuid" | "customLocalDate" | "customLocalTime" | "customLocalDateTime" | null | undefined;
dbType?: {
name: string;
} | null | undefined;
tsType?: {
name: string;
importPath?: string | null | undefined;
isDefault?: boolean | null | undefined;
isRelative?: boolean | null | undefined;
} | null | undefined;
adapter?: {
name: string;
importPath?: string | null | undefined;
isDefault?: boolean | null | undefined;
isRelative?: boolean | null | undefined;
} | null | undefined;
} | null | undefined;
name?: string | null | undefined;
isComputed?: boolean | null | undefined;
isOptional?: boolean | null | undefined;
hasDefault?: boolean | null | undefined;
};
columnName?: string | RegExp | null | undefined;
tableName?: string | RegExp | null | undefined;
columnType?: string | RegExp | null | undefined;
comment?: string | null | undefined;
}[]) & import("lodash").MemoizedFunction;
protected getTemplatePath: (() => string) & import("lodash").MemoizedFunction;
protected getCompiledTemplate: (() => Promise<HandlebarsTemplateDelegate<any>>) & import("lodash").MemoizedFunction;
private resolvePath;
generate(): Promise<void>;
protected shouldProcess(table: Table): boolean;
protected getTableKind(table: Table): TableKind | null;
protected getTableTemplateInput(table: Table, tableKind: TableKind, filePath: string): Promise<any>;
protected getFieldsInput(table: Table, pkCol: Column | null): FieldTmplInput[];
protected getFieldInput(col: Column, table: Table, pkCol: Column | null): {
name: string;
columnName: string;
comment: string[] | null;
isOptional: boolean;
hasDefault: boolean;
columnMethod: ColumnMethod;
fieldType: GeneratedFieldType;
includeDBTypeWhenIsOptional: boolean;
isPK: boolean;
};
protected getRepoInput(tableName: string, tableKind: TableKind, pkField?: FieldTmplInput): RepoInput | null;
protected generateTableMapper(table: Table): Promise<void>;
protected getIdPrefix(table: Table): string | null | undefined;
protected formatComment(comments: (string | null | undefined)[]): string[] | null;
protected getConnectionSourceImportPath(outputFilePath: string): string;
protected getAdapterImports(outputFilePath: string, fields: FieldTmplInput[]): ImportTmplInput[];
private accumulateImports;
protected getTypeImports(outputFilePath: string, fields: FieldTmplInput[], generateRepo: boolean): ImportTmplInput[];
protected getUtilImports(colSetName: string | null, generateRepo: boolean): ImportTmplInput[];
protected getImportPathForOutputPath(filePath: string, importPath: string, importedItem: ImportedItem): string;
protected getAdapterImportPath(adapter: ImportedItem, outputFilePath: string): string;
protected preProcessTemplateInput(input: any): Promise<any>;
protected postProcessOutput(output: string, _table: Table): Promise<string>;
protected getCrudRepoName(tableName: string): string;
protected getTableMapperClassName(tableName: string, tableKind: TableKind): string;
protected getRowTypePrefix(tableName: string): Capitalize<string>;
protected getTableMapperInstanceName(tableName: string, tableKind: TableKind): string;
protected getColumnsObjectName(tableName: string, tableKind: TableKind): string;
private getPascalCasedTableName;
private getCamelCasedTableName;
protected isColumnOmitted(tableName: string, col: Column): boolean;
protected isColumnOptional(tableName: string, col: Column): boolean;
protected doesColumnHaveDefault(tableName: string, col: Column): boolean;
protected isColumnComputed(tableName: string, col: Column): boolean;
protected getColComment(tableName: string, col: Column): string | undefined;
protected getFieldNameForColumn(tableName: string, col: Column): string;
protected getFieldType(tableName: string, col: Column): GeneratedFieldType;
protected getOutputFilePath(table: Table, tableKind: TableKind): string;
protected getOutputFileName(table: Table, tableKind: TableKind): string;
protected findPrimaryKey(table: Table): {
type: string;
name: string;
comment?: string | null | undefined;
nullable?: boolean | undefined;
default?: any;
} | null;
protected wrapType(typeExpr: string, wrapper?: string | null): string;
protected getTypeWrapper(typeName: string): {
name: string;
importPath?: string | null | undefined;
isDefault?: boolean | null | undefined;
isRelative?: boolean | null | undefined;
} | undefined;
protected extractTableName(configTableName: string): string;
protected getSelectedRowTypeName(tableName: string): string;
protected getInsertableRowTypeName(tableName: string): string;
protected getUpdatableRowTypeName(tableName: string): string;
protected getSelectedValuesTypeName(tableName: string): string;
protected getInsertableValuesTypeName(tableName: string): string;
protected getUpdatableValuesTypeName(tableName: string): string;
protected getColMappingObjName(tableName: string): string;
protected getWrappedTypeInput(name: string, baseExpr: string, imports: ImportTmplInput[], isInterface?: boolean): {
name: string;
isInterface: boolean;
expr: string;
};
protected getRowTypeInputs(tableName: string, tableKind: TableKind, mapperClassName: string, imports: ImportTmplInput[]): any;
protected getValuesTypeInputs(tableName: string, tableKind: TableKind, mapperClassName: string, imports: ImportTmplInput[]): any;
protected getColSetName(tableName: string, tableKind: TableKind): string | null;
protected getTableMapperInstName(tableName: string, tableKind: TableKind): string | null;
protected getColMappingInput(tableName: string, didGenerateRepo: boolean): any;
}
type TableKind = "Table" | "View";
export {};