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
- 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?
: null
| string
; isDefault?
: null
| boolean
; isRelative?
: null
| boolean
; name
: string
} ; dbType?
: null
| { name
: string
} ; kind?
: null
| "custom"
| "customComparable"
| "enum"
| "customInt"
| "customDouble"
| "customUuid"
| "customLocalDate"
| "customLocalTime"
| "customLocalDateTime"
; tsType?
: null
| { importPath?
: null
| string
; isDefault?
: null
| boolean
; isRelative?
: null
| boolean
; name
: string
} } } & undefined
| false
| { hasDefault?
: null
| boolean
; isComputed?
: null
| boolean
; isOptional?
: null
| boolean
; name?
: null
| string
; type?
: null
| { adapter?
: null
| { importPath?
: null
| string
; isDefault?
: null
| boolean
; isRelative?
: null
| boolean
; name
: string
} ; dbType?
: null
| { name
: string
} ; kind?
: null
| "custom"
| "customComparable"
| "enum"
| "customInt"
| "customDouble"
| "customUuid"
| "customLocalDate"
| "customLocalTime"
| "customLocalDateTime"
; tsType?
: null
| { importPath?
: null
| string
; isDefault?
: null
| boolean
; isRelative?
: null
| boolean
; name
: string
} } } ; tableName?
: null
| string
| RegExp
}[] & MemoizedFunction
• Protected
getTemplatePath: () => string
& MemoizedFunction
• logger: Logger
= console
• Protected
naming: NamingOptions
• Protected
opts: GeneratorOpts
• 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 |
{ 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 |
{ 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 |
{ 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 |
{ 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 |
{ 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 |
{ 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 |
{ 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
): string
Name | Type |
---|---|
tableName |
string |
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
): string
Name | Type |
---|---|
tableName |
string |
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 |
{ 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 |
{ 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 |
{ 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
>
▸ 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 |
{ 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