Skip to content

Commit

Permalink
Do not make default fields optional by default
Browse files Browse the repository at this point in the history
  • Loading branch information
nettofarah committed Jan 28, 2020
1 parent 291e15c commit cd602de
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
20 changes: 11 additions & 9 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('inferTable', () => {
const code = await inferTable(connectionString, 'agreements')
expect(code).toMatchInlineSnapshot(`
"/**
Schema Generated with mysql-schema-ts 1.0.0
Schema Generated with mysql-schema-ts 1.2.0
*/
export interface agreements {
Expand All @@ -71,7 +71,7 @@ describe('inferTable', () => {
const code = await inferTable(connectionString, 'requests')
expect(code).toMatchInlineSnapshot(`
"/**
Schema Generated with mysql-schema-ts 1.0.0
Schema Generated with mysql-schema-ts 1.2.0
*/
export interface requests {
Expand All @@ -88,16 +88,17 @@ describe('inferTable', () => {
const code = await inferTable(connectionString, 'complex')
expect(code).toMatchInlineSnapshot(`
"/**
Schema Generated with mysql-schema-ts 1.0.0
Schema Generated with mysql-schema-ts 1.2.0
*/
export interface complex {
id: string
name: string
nullable?: string | null
created_at?: Date
/** Defaults to: CURRENT_TIMESTAMP. */
created_at: Date
created_on: Date
/** This is an awesome field */
/** This is an awesome field */
documented_field?: string | null
}
"
Expand All @@ -108,7 +109,7 @@ describe('inferTable', () => {
const code = await inferTable(connectionString, 'table_with_json')
expect(code).toMatchInlineSnapshot(`
"/**
Schema Generated with mysql-schema-ts 1.0.0
Schema Generated with mysql-schema-ts 1.2.0
*/
export type JSONPrimitive = string | number | boolean | null
Expand All @@ -130,7 +131,7 @@ describe('inferSchema', () => {
const code = await inferSchema(connectionString)
expect(code).toMatchInlineSnapshot(`
"/**
Schema Generated with mysql-schema-ts 1.0.0
Schema Generated with mysql-schema-ts 1.2.0
*/
export type JSONPrimitive = string | number | boolean | null
Expand All @@ -148,9 +149,10 @@ describe('inferSchema', () => {
id: string
name: string
nullable?: string | null
created_at?: Date
/** Defaults to: CURRENT_TIMESTAMP. */
created_at: Date
created_on: Date
/** This is an awesome field */
/** This is an awesome field */
documented_field?: string | null
}
export interface requests {
Expand Down
1 change: 1 addition & 0 deletions src/mysql-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export class MySQL {
udtName: isEnum ? enumNameFromColumn(dataType, columnName) : dataType,
comment: schemaItem.column_comment,
hasDefault: Boolean(schemaItem.column_default),
defaultValue: schemaItem.column_default,
nullable
}
})
Expand Down
12 changes: 8 additions & 4 deletions src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface Column {
udtName: string
nullable: boolean
hasDefault: boolean
defaultValue: string | null
comment: string | null
tsType?: string
}
Expand All @@ -22,12 +23,15 @@ function normalize(name: string): string {
export function tableToTS(name: string, table: Table): string {
const members = Object.keys(table).map(column => {
const type = table[column].tsType
const hasDefault = table[column].hasDefault
const nullable = table[column].nullable ? '| null' : ''
const comment = table[column].comment ? `\n/** ${table[column].comment} */\n` : ''

const isOptional = hasDefault || nullable
return `${comment}${normalize(column)}${isOptional ? '?' : ''}: ${type}${nullable}\n`
const hasDefault = table[column].hasDefault
const defaultComment = hasDefault ? `Defaults to: ${table[column].defaultValue}.` : ''
const comment = `${table[column].comment} ${defaultComment}`
const tsComment = comment.trim().length > 0 ? `\n/** ${comment} */\n` : ''

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

return `
Expand Down

0 comments on commit cd602de

Please sign in to comment.