ts-sql-codegen / Generator
Generator class for programmatic codegen.
Most common usage involves creating an instance and calling generate function:
const options = {
schemaPath: './schema.yaml',
connectionSourcePath: './connection-source.ts'
}
const generator = new Generator(options);
await generator.generate();See GeneratorOpts 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.
- accumulateImports
- doesColumnHaveDefault
- extractTableName
- findPrimaryKey
- formatComment
- generate
- generateTableMapper
- getAdapterImportPath
- getAdapterImports
- getCamelCasedTableName
- getColComment
- getColMappingInput
- getColMappingObjName
- getColSetName
- getColumnsObjectName
- getConnectionSourceImportPath
- getCrudRepoName
- getFieldInput
- getFieldNameForColumn
- getFieldType
- getFieldsInput
- getIdPrefix
- getImportPathForOutputPath
- getInsertableRowTypeName
- getInsertableValuesTypeName
- getOutputFileName
- getOutputFilePath
- getPascalCasedTableName
- getRepoInput
- getRowTypeInputs
- getRowTypePrefix
- getSelectedRowTypeName
- getSelectedValuesTypeName
- getTableKind
- getTableMapperClassName
- getTableMapperInstName
- getTableMapperInstanceName
- getTableTemplateInput
- getTypeImports
- getTypeWrapper
- getUpdatableRowTypeName
- getUpdatableValuesTypeName
- getUtilImports
- getValuesTypeInputs
- getWrappedTypeInput
- isColumnComputed
- isColumnOmitted
- isColumnOptional
- postProcessOutput
- preProcessTemplateInput
- resolvePath
- shouldProcess
- wrapType
• new Generator(opts): Generator
| Name | Type |
|---|---|
opts |
GeneratorOpts |
• Protected getCompiledTemplate: () => Promise<HandlebarsTemplateDelegate<any>> & MemoizedFunction
• Protected getFieldMappings: () => { columnName?: null | string | RegExp ; columnType?: null | string | RegExp ; comment?: null | string ; generatedField: false | { hasDefault?: null | boolean ; isComputed?: null | boolean ; isOptional?: null | boolean ; name?: null | string ; type?: null | { adapter?: null | { importPath?: ... | ... | ... ; isDefault?: ... | ... | ... | ... ; isRelative?: ... | ... | ... | ... ; name: string } ; dbType?: null | { name: string } ; kind?: null | "custom" | "customComparable" | "enum" | "customInt" | "customDouble" | "customUuid" | "customLocalDate" | "customLocalTime" | "customLocalDateTime" ; tsType?: null | { importPath?: ... | ... | ... ; isDefault?: ... | ... | ... | ... ; isRelative?: ... | ... | ... | ... ; name: string } } } ; tableName?: null | string | RegExp }[] & MemoizedFunction
• Protected getTemplatePath: () => string & MemoizedFunction
• logger: Logger = console
• Protected naming: NamingOptions
• Protected opts: Object
| Name | Type | Description |
|---|---|---|
common? |
null | { customTypes?: null | { importPath: string } ; primaryKey?: null | { isAutoGenerated?: null | boolean ; name?: null | string } ; typeAdapter?: null | { importPath: string } } |
Convenience utility for common cases where all tables follow same conventions See CommonOptions |
connectionSource? |
null | { path?: null | string ; resolveRelative?: null | boolean } |
Connection source configuration See ConnectionSourceOptions |
connectionSourcePath |
string |
Path to module that exports DBConnection object used in table mappers Deprecated See connectionSource |
dryRun? |
null | boolean |
Simulate the generation and print the outcome without actually modifying any files |
export? |
null | { columnTypeMappingInterface?: boolean ; crudRepository?: boolean ; extractedColumns?: boolean ; rowTypes?: boolean | { asInterface: boolean } ; tableClasses?: boolean ; tableInstances?: boolean ; valuesTypes?: boolean | { asInterface: boolean } } |
Customize what all entities are exported from generated file See ExportOptions |
fieldMappings? |
null | { columnName?: null | string | RegExp ; columnType?: null | string | RegExp ; comment?: null | string ; generatedField: false | { hasDefault?: null | boolean ; isComputed?: null | boolean ; isOptional?: null | boolean ; name?: null | string ; type?: null | { adapter?: null | { importPath?: ... | ... | ... ; isDefault?: ... | ... | ... | ... ; isRelative?: ... | ... | ... | ... ; name: string } ; dbType?: null | { name: string } ; kind?: null | "custom" | "customComparable" | "enum" | "customInt" | "customDouble" | "customUuid" | "customLocalDate" | "customLocalTime" | "customLocalDateTime" ; tsType?: null | { importPath?: ... | ... | ... ; isDefault?: ... | ... | ... | ... ; isRelative?: ... | ... | ... | ... ; name: string } } } ; tableName?: null | string | RegExp }[] |
Customize how table columns are mapped to typescript fields See FieldMapping |
includeDBTypeWhenIsOptional? |
null | boolean |
The fields marked as "custom", "customComparable" or "enum" receive a second generic argument that need to be the same of the db type in the database or redefined for the field If you set to true this property that second generic argument will be generated. |
moduleRoot? |
null | string |
Root path of module - used for resolving relative paths. If unspecified, assumed to be cwd |
naming? |
null | { columnTypeMappingInterfaceNameSuffix?: string ; crudRepositoryClassNamePrefix?: string ; crudRepositoryClassNameSuffix?: string ; insertableRowTypeNamePrefix?: string ; insertableRowTypeNameSuffix?: string ; insertableValuesTypeNamePrefix?: string ; insertableValuesTypeNameSuffix?: string ; selectedRowTypeNamePrefix?: string ; selectedRowTypeNameSuffix?: string ; selectedValuesTypeNamePrefix?: string ; selectedValuesTypeNameSuffix?: string ; tableClassNamePrefix?: string ; tableClassNameSuffix?: string ; tableColumnsNamePrefix?: string ; tableColumnsNameSuffix?: string ; tableInstanceNamePrefix?: string ; tableInstanceNameSuffix?: string ; updatableRowTypeNamePrefix?: string ; updatableRowTypeNameSuffix?: string ; updatableValuesTypeNamePrefix?: string ; updatableValuesTypeNameSuffix?: string ; viewClassNamePrefix?: string ; viewClassNameSuffix?: string ; viewColumnsNamePrefix?: string ; viewColumnsNameSuffix?: string ; viewInstanceNamePrefix?: string ; viewInstanceNameSuffix?: string } |
Customize the naming rules of the generated items See NamingOptions |
output? |
null | { import?: null | { extension?: null | string } } |
Shared options that affect all generated output |
outputDirPath |
string |
Path to output directory where a typescript class file will be generated for each table |
rawContent? |
null | { after?: null | string ; before?: null | string } |
Support injection of raw content in the generated files. This is useful for adding things like eslint-disable, additional exports etc. See RawContent |
removeExtraneous? |
null | "never" | "interactively" | "all" |
Remove extraneous files after code generation completes - this prevents you from having to manually clean up files after eg. any table has been deleted, but it is your responsibility to ensure that the outputDir used solely for files generated through this utility and all files are written as part of single run. Defauls to retaining all extraneous files. |
schemaPath |
string |
Path to yaml schema dumped by tbls |
tableMapping? |
null | { idPrefix?: null | string ; useQualifiedTableName?: null | boolean } |
Customize how tables are mapped See TableMapping |
tables? |
null | { exclude?: null | (string | RegExp)[] ; include?: null | (string | RegExp)[] } |
Restrict the generator to process only a subset of tables available See TableInclusion |
typeWrappers? |
null | { typeName: string | RegExp = StrOrRegExpSchema; wrapper: { importPath?: null | string ; isDefault?: null | boolean ; isRelative?: null | boolean ; name: string } = ImportedItemSchema }[] |
Wrap inferred types before exporting - this is useful to restrict the types used for insert/update etc. beyond what the database permits. Eg. We can hint that updatedAt must be set whenever record is updated See TypeWapper |
• Private writtenFiles: Set<string>
▸ accumulateImports(imports, defaultImports): ImportTmplInput[]
| Name | Type |
|---|---|
imports |
Map<string, Set<string>> |
defaultImports |
Map<string, Set<string>> |
ImportTmplInput[]
▸ doesColumnHaveDefault(tableName, col): boolean
| Name | Type |
|---|---|
tableName |
string |
col |
Object |
col.comment? |
null | string |
col.default? |
any |
col.name |
string |
col.nullable? |
boolean |
col.type |
string |
boolean
▸ extractTableName(configTableName): string
| Name | Type |
|---|---|
configTableName |
string |
string
▸ findPrimaryKey(table): null | { comment?: null | string ; default?: any ; name: string ; nullable?: boolean ; type: string }
| Name | Type |
|---|---|
table |
Object |
table.columns |
{ comment?: null | string ; default?: any ; name: string ; nullable?: boolean ; type: string }[] |
table.comment? |
null | string |
table.constraints? |
null | { columns: string[] ; comment?: null | string ; name: string ; referencedColumns?: null | string[] ; referencedTable?: null | string ; table: string ; type: string }[] |
table.name |
string |
table.type |
string |
null | { comment?: null | string ; default?: any ; name: string ; nullable?: boolean ; type: string }
▸ formatComment(comments): null | string[]
| Name | Type |
|---|---|
comments |
(undefined | null | string)[] |
null | string[]
▸ generate(): Promise<void>
Promise<void>
▸ generateTableMapper(table): Promise<void>
| Name | Type |
|---|---|
table |
Object |
table.columns |
{ comment?: null | string ; default?: any ; name: string ; nullable?: boolean ; type: string }[] |
table.comment? |
null | string |
table.constraints? |
null | { columns: string[] ; comment?: null | string ; name: string ; referencedColumns?: null | string[] ; referencedTable?: null | string ; table: string ; type: string }[] |
table.name |
string |
table.type |
string |
Promise<void>
▸ getAdapterImportPath(adapter, outputFilePath): string
| Name | Type |
|---|---|
adapter |
ImportedItem |
outputFilePath |
string |
string
▸ getAdapterImports(outputFilePath, fields): ImportTmplInput[]
| Name | Type |
|---|---|
outputFilePath |
string |
fields |
FieldTmplInput[] |
ImportTmplInput[]
▸ getCamelCasedTableName(tableName): string
| Name | Type |
|---|---|
tableName |
string |
string
▸ getColComment(tableName, col): undefined | string
| Name | Type |
|---|---|
tableName |
string |
col |
Object |
col.comment? |
null | string |
col.default? |
any |
col.name |
string |
col.nullable? |
boolean |
col.type |
string |
undefined | string
▸ getColMappingInput(tableName, didGenerateRepo): any
| Name | Type |
|---|---|
tableName |
string |
didGenerateRepo |
boolean |
any
▸ getColMappingObjName(tableName): string
| Name | Type |
|---|---|
tableName |
string |
string
▸ getColSetName(tableName, tableKind): null | string
| Name | Type |
|---|---|
tableName |
string |
tableKind |
TableKind |
null | string
▸ getColumnsObjectName(tableName, tableKind): string
| Name | Type |
|---|---|
tableName |
string |
tableKind |
TableKind |
string
▸ getConnectionSourceImportPath(outputFilePath): string
| Name | Type |
|---|---|
outputFilePath |
string |
string
▸ getCrudRepoName(tableName): string
| Name | Type |
|---|---|
tableName |
string |
string
▸ getFieldInput(col, table, pkCol): Object
| Name | Type |
|---|---|
col |
Object |
col.comment? |
null | string |
col.default? |
any |
col.name |
string |
col.nullable? |
boolean |
col.type |
string |
table |
Object |
table.columns |
{ comment?: null | string ; default?: any ; name: string ; nullable?: boolean ; type: string }[] |
table.comment? |
null | string |
table.constraints? |
null | { columns: string[] ; comment?: null | string ; name: string ; referencedColumns?: null | string[] ; referencedTable?: null | string ; table: string ; type: string }[] |
table.name |
string |
table.type |
string |
pkCol |
null | { comment?: null | string ; default?: any ; name: string ; nullable?: boolean ; type: string } |
Object
| Name | Type |
|---|---|
columnMethod |
ColumnMethod |
columnName |
string |
comment |
null | string[] |
fieldType |
GeneratedFieldType |
hasDefault |
boolean |
includeDBTypeWhenIsOptional |
boolean |
isOptional |
boolean |
isPK |
boolean |
name |
string |
▸ getFieldNameForColumn(tableName, col): string
| Name | Type |
|---|---|
tableName |
string |
col |
Object |
col.comment? |
null | string |
col.default? |
any |
col.name |
string |
col.nullable? |
boolean |
col.type |
string |
string
▸ getFieldType(tableName, col): GeneratedFieldType
| Name | Type |
|---|---|
tableName |
string |
col |
Object |
col.comment? |
null | string |
col.default? |
any |
col.name |
string |
col.nullable? |
boolean |
col.type |
string |
▸ getFieldsInput(table, pkCol): FieldTmplInput[]
| Name | Type |
|---|---|
table |
Object |
table.columns |
{ comment?: null | string ; default?: any ; name: string ; nullable?: boolean ; type: string }[] |
table.comment? |
null | string |
table.constraints? |
null | { columns: string[] ; comment?: null | string ; name: string ; referencedColumns?: null | string[] ; referencedTable?: null | string ; table: string ; type: string }[] |
table.name |
string |
table.type |
string |
pkCol |
null | { comment?: null | string ; default?: any ; name: string ; nullable?: boolean ; type: string } |
FieldTmplInput[]
▸ getIdPrefix(table): undefined | null | string
| Name | Type |
|---|---|
table |
Object |
table.columns |
{ comment?: null | string ; default?: any ; name: string ; nullable?: boolean ; type: string }[] |
table.comment? |
null | string |
table.constraints? |
null | { columns: string[] ; comment?: null | string ; name: string ; referencedColumns?: null | string[] ; referencedTable?: null | string ; table: string ; type: string }[] |
table.name |
string |
table.type |
string |
undefined | null | string
▸ getImportPathForOutputPath(filePath, importPath, importedItem): string
| Name | Type |
|---|---|
filePath |
string |
importPath |
string |
importedItem |
ImportedItem |
string
▸ getInsertableRowTypeName(tableName): string
| Name | Type |
|---|---|
tableName |
string |
string
▸ getInsertableValuesTypeName(tableName): string
| Name | Type |
|---|---|
tableName |
string |
string
▸ getOutputFileName(table, tableKind): string
| Name | Type |
|---|---|
table |
Object |
table.columns |
{ comment?: null | string ; default?: any ; name: string ; nullable?: boolean ; type: string }[] |
table.comment? |
null | string |
table.constraints? |
null | { columns: string[] ; comment?: null | string ; name: string ; referencedColumns?: null | string[] ; referencedTable?: null | string ; table: string ; type: string }[] |
table.name |
string |
table.type |
string |
tableKind |
TableKind |
string
▸ getOutputFilePath(table, tableKind): string
| Name | Type |
|---|---|
table |
Object |
table.columns |
{ comment?: null | string ; default?: any ; name: string ; nullable?: boolean ; type: string }[] |
table.comment? |
null | string |
table.constraints? |
null | { columns: string[] ; comment?: null | string ; name: string ; referencedColumns?: null | string[] ; referencedTable?: null | string ; table: string ; type: string }[] |
table.name |
string |
table.type |
string |
tableKind |
TableKind |
string
▸ getPascalCasedTableName(tableName): Capitalize<string>
| Name | Type |
|---|---|
tableName |
string |
Capitalize<string>
▸ getRepoInput(tableName, tableKind, pkField?): null | RepoInput
| Name | Type |
|---|---|
tableName |
string |
tableKind |
TableKind |
pkField? |
FieldTmplInput |
null | RepoInput
▸ getRowTypeInputs(tableName, tableKind, mapperClassName, imports): any
| Name | Type |
|---|---|
tableName |
string |
tableKind |
TableKind |
mapperClassName |
string |
imports |
ImportTmplInput[] |
any
▸ getRowTypePrefix(tableName): Capitalize<string>
| Name | Type |
|---|---|
tableName |
string |
Capitalize<string>
▸ getSelectedRowTypeName(tableName): string
| Name | Type |
|---|---|
tableName |
string |
string
▸ getSelectedValuesTypeName(tableName): string
| Name | Type |
|---|---|
tableName |
string |
string
▸ getTableKind(table): null | TableKind
| Name | Type |
|---|---|
table |
Object |
table.columns |
{ comment?: null | string ; default?: any ; name: string ; nullable?: boolean ; type: string }[] |
table.comment? |
null | string |
table.constraints? |
null | { columns: string[] ; comment?: null | string ; name: string ; referencedColumns?: null | string[] ; referencedTable?: null | string ; table: string ; type: string }[] |
table.name |
string |
table.type |
string |
null | TableKind
▸ getTableMapperClassName(tableName, tableKind): string
| Name | Type |
|---|---|
tableName |
string |
tableKind |
TableKind |
string
▸ getTableMapperInstName(tableName, tableKind): null | string
| Name | Type |
|---|---|
tableName |
string |
tableKind |
TableKind |
null | string
▸ getTableMapperInstanceName(tableName, tableKind): string
| Name | Type |
|---|---|
tableName |
string |
tableKind |
TableKind |
string
▸ getTableTemplateInput(table, tableKind, filePath): Promise<any>
| Name | Type |
|---|---|
table |
Object |
table.columns |
{ comment?: null | string ; default?: any ; name: string ; nullable?: boolean ; type: string }[] |
table.comment? |
null | string |
table.constraints? |
null | { columns: string[] ; comment?: null | string ; name: string ; referencedColumns?: null | string[] ; referencedTable?: null | string ; table: string ; type: string }[] |
table.name |
string |
table.type |
string |
tableKind |
TableKind |
filePath |
string |
Promise<any>
▸ getTypeImports(outputFilePath, fields, generateRepo): ImportTmplInput[]
| Name | Type |
|---|---|
outputFilePath |
string |
fields |
FieldTmplInput[] |
generateRepo |
boolean |
ImportTmplInput[]
▸ getTypeWrapper(typeName): undefined | { importPath?: null | string ; isDefault?: null | boolean ; isRelative?: null | boolean ; name: string }
| Name | Type |
|---|---|
typeName |
string |
undefined | { importPath?: null | string ; isDefault?: null | boolean ; isRelative?: null | boolean ; name: string }
▸ getUpdatableRowTypeName(tableName): string
| Name | Type |
|---|---|
tableName |
string |
string
▸ getUpdatableValuesTypeName(tableName): string
| Name | Type |
|---|---|
tableName |
string |
string
▸ getUtilImports(colSetName, generateRepo): ImportTmplInput[]
| Name | Type |
|---|---|
colSetName |
null | string |
generateRepo |
boolean |
ImportTmplInput[]
▸ getValuesTypeInputs(tableName, tableKind, mapperClassName, imports): any
| Name | Type |
|---|---|
tableName |
string |
tableKind |
TableKind |
mapperClassName |
string |
imports |
ImportTmplInput[] |
any
▸ getWrappedTypeInput(name, baseExpr, imports, isInterface?): Object
| Name | Type | Default value |
|---|---|---|
name |
string |
undefined |
baseExpr |
string |
undefined |
imports |
ImportTmplInput[] |
undefined |
isInterface |
boolean |
false |
Object
| Name | Type |
|---|---|
expr |
string |
isInterface |
boolean |
name |
string |
▸ isColumnComputed(tableName, col): boolean
| Name | Type |
|---|---|
tableName |
string |
col |
Object |
col.comment? |
null | string |
col.default? |
any |
col.name |
string |
col.nullable? |
boolean |
col.type |
string |
boolean
▸ isColumnOmitted(tableName, col): boolean
| Name | Type |
|---|---|
tableName |
string |
col |
Object |
col.comment? |
null | string |
col.default? |
any |
col.name |
string |
col.nullable? |
boolean |
col.type |
string |
boolean
▸ isColumnOptional(tableName, col): boolean
| Name | Type |
|---|---|
tableName |
string |
col |
Object |
col.comment? |
null | string |
col.default? |
any |
col.name |
string |
col.nullable? |
boolean |
col.type |
string |
boolean
▸ postProcessOutput(output, _table): Promise<string>
| Name | Type |
|---|---|
output |
string |
_table |
Object |
_table.columns |
{ comment?: null | string ; default?: any ; name: string ; nullable?: boolean ; type: string }[] |
_table.comment? |
null | string |
_table.constraints? |
null | { columns: string[] ; comment?: null | string ; name: string ; referencedColumns?: null | string[] ; referencedTable?: null | string ; table: string ; type: string }[] |
_table.name |
string |
_table.type |
string |
Promise<string>
▸ preProcessTemplateInput(input): Promise<any>
| Name | Type |
|---|---|
input |
any |
Promise<any>
▸ resolvePath(relPath): string
| Name | Type |
|---|---|
relPath |
string |
string
▸ shouldProcess(table): boolean
| Name | Type |
|---|---|
table |
Object |
table.columns |
{ comment?: null | string ; default?: any ; name: string ; nullable?: boolean ; type: string }[] |
table.comment? |
null | string |
table.constraints? |
null | { columns: string[] ; comment?: null | string ; name: string ; referencedColumns?: null | string[] ; referencedTable?: null | string ; table: string ; type: string }[] |
table.name |
string |
table.type |
string |
boolean
▸ wrapType(typeExpr, wrapper?): string
| Name | Type |
|---|---|
typeExpr |
string |
wrapper? |
null | string |
string