Skip to content

Commit

Permalink
Make nulls optional
Browse files Browse the repository at this point in the history
  • Loading branch information
nettofarah committed Jan 10, 2020
1 parent 7f9e965 commit 4035cc1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
22 changes: 11 additions & 11 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 0.3.0
Schema Generated with mysql-schema-ts 1.0.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 0.3.0
Schema Generated with mysql-schema-ts 1.0.0
*/
export interface requests {
Expand All @@ -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
}
"
`)
Expand All @@ -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
Expand All @@ -118,7 +118,7 @@ describe('inferTable', () => {
export interface table_with_json {
id: string
data: JSONValue | null
data?: JSONValue | null
}
"
`)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -161,7 +161,7 @@ describe('inferSchema', () => {
}
export interface table_with_json {
id: string
data: JSONValue | null
data?: JSONValue | null
}
"
`)
Expand Down
4 changes: 3 additions & 1 deletion src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 `
Expand Down

0 comments on commit 4035cc1

Please sign in to comment.