Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/mysql-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type EnumRecord = {
}

type TableColumnType = {
position: number
column_name: string
data_type: string
is_nullable: string
Expand Down Expand Up @@ -93,7 +94,7 @@ export class MySQL {
this.connection,
sql`SELECT
column_name as column_name,
column_type as column_type,
column_type as column_type
FROM information_schema.columns
WHERE data_type IN ('enum', 'set')
AND table_schema = ${this.schema()}
Expand All @@ -115,11 +116,12 @@ export class MySQL {
const tableColumns = await query<TableColumnType>(
this.connection,
sql`SELECT
ordinal_position as position,
column_name as column_name,
data_type as data_type,
is_nullable as is_nullable,
column_default as column_default,
column_comment as column_comment
column_comment as column_comment,
extra as extra
FROM information_schema.columns
WHERE table_name = ${tableName}
Expand All @@ -131,9 +133,10 @@ export class MySQL {
const dataType = schemaItem.data_type
const isEnum = /^(enum|set)$/i.test(dataType)
const nullable = schemaItem.is_nullable === 'YES'
const hasImplicitDefault = (!nullable && schemaItem.extra === 'auto_increment')
const hasImplicitDefault = !nullable && schemaItem.extra === 'auto_increment'

Table[columnName] = {
position: schemaItem.position,
udtName: isEnum ? enumNameFromColumn(dataType, columnName) : dataType,
comment: schemaItem.column_comment,
hasDefault: Boolean(schemaItem.column_default) || hasImplicitDefault,
Expand Down
27 changes: 15 additions & 12 deletions src/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import camelcase from 'camelcase'

export interface Column {
position: number
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about we call this index?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :~)

udtName: string
nullable: boolean
hasDefault: boolean
Expand Down Expand Up @@ -29,20 +30,22 @@ function normalize(name: string): string {

export function tableToTS(name: string, prefix: string, table: Table): string {
const members = (withDefaults: boolean): string[] =>
Object.keys(table).map(column => {
const type = table[column].tsType
const nullable = table[column].nullable ? '| null' : ''
const defaultComment = table[column].defaultValue ? `Defaults to: ${table[column].defaultValue}.` : ''
const comment = `${table[column].comment} ${defaultComment}`
const tsComment = comment.trim().length > 0 ? `\n/** ${comment} */\n` : ''
Object.keys(table)
.sort((a, b) => table[a].position - table[b].position)
.map(column => {
const type = table[column].tsType
const nullable = table[column].nullable ? '| null' : ''
const defaultComment = table[column].defaultValue ? `Defaults to: ${table[column].defaultValue}.` : ''
const comment = `${table[column].comment} ${defaultComment}`
const tsComment = comment.trim().length > 0 ? `\n/** ${comment} */\n` : ''

let isOptional = table[column].nullable
if (withDefaults) {
isOptional = isOptional || table[column].hasDefault
}
let isOptional = table[column].nullable
if (withDefaults) {
isOptional = isOptional || table[column].hasDefault
}

return `${tsComment}${normalize(column)}${isOptional ? '?' : ''}: ${type}${nullable}\n`
})
return `${tsComment}${normalize(column)}${isOptional ? '?' : ''}: ${type}${nullable}\n`
})

const tableName = (prefix || '') + camelize(normalize(name))

Expand Down