diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 3bd587c..7fd7af6 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -54,7 +54,7 @@ describe('inferTable', () => { const code = await inferTable(connectionString, 'agreements') expect(code).toMatchInlineSnapshot(` "/** - Schema Generated with mysql-schema-ts 0.3.0 + Schema Generated with mysql-schema-ts 1.0.0 */ export interface agreements { @@ -71,7 +71,7 @@ describe('inferTable', () => { const code = await inferTable(connectionString, 'requests') expect(code).toMatchInlineSnapshot(` "/** - Schema Generated with mysql-schema-ts 0.3.0 + Schema Generated with mysql-schema-ts 1.0.0 */ export interface requests { @@ -88,17 +88,17 @@ describe('inferTable', () => { const code = await inferTable(connectionString, 'complex') expect(code).toMatchInlineSnapshot(` "/** - Schema Generated with mysql-schema-ts 0.3.0 + Schema Generated with mysql-schema-ts 1.0.0 */ export interface complex { id: string name: string - nullable: string | null + nullable?: string | null created_at?: Date created_on: Date /** This is an awesome field */ - documented_field: string | null + documented_field?: string | null } " `) @@ -108,7 +108,7 @@ describe('inferTable', () => { const code = await inferTable(connectionString, 'table_with_json') expect(code).toMatchInlineSnapshot(` "/** - Schema Generated with mysql-schema-ts 0.3.0 + Schema Generated with mysql-schema-ts 1.0.0 */ export type JSONPrimitive = string | number | boolean | null @@ -118,7 +118,7 @@ describe('inferTable', () => { export interface table_with_json { id: string - data: JSONValue | null + data?: JSONValue | null } " `) @@ -130,7 +130,7 @@ describe('inferSchema', () => { const code = await inferSchema(connectionString) expect(code).toMatchInlineSnapshot(` "/** - Schema Generated with mysql-schema-ts 0.3.0 + Schema Generated with mysql-schema-ts 1.0.0 */ export type JSONPrimitive = string | number | boolean | null @@ -147,11 +147,11 @@ describe('inferSchema', () => { export interface complex { id: string name: string - nullable: string | null + nullable?: string | null created_at?: Date created_on: Date /** This is an awesome field */ - documented_field: string | null + documented_field?: string | null } export interface requests { id: number @@ -161,7 +161,7 @@ describe('inferSchema', () => { } export interface table_with_json { id: string - data: JSONValue | null + data?: JSONValue | null } " `) diff --git a/src/typescript.ts b/src/typescript.ts index cf17d0d..88cc0fe 100644 --- a/src/typescript.ts +++ b/src/typescript.ts @@ -25,7 +25,9 @@ export function tableToTS(name: string, table: Table): string { const hasDefault = table[column].hasDefault const nullable = table[column].nullable ? '| null' : '' const comment = table[column].comment ? `\n/** ${table[column].comment} */\n` : '' - return `${comment}${normalize(column)}${hasDefault ? '?' : ''}: ${type}${nullable}\n` + + const isOptional = hasDefault || nullable + return `${comment}${normalize(column)}${isOptional ? '?' : ''}: ${type}${nullable}\n` }) return `