Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Column Index Support #14

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 = {
index: 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 index,
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] = {
index: schemaItem.index,
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 {
index: number
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].index - table[b].index)
.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