Skip to content

Commit

Permalink
Improve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nettofarah committed Jan 28, 2020
1 parent 3624f4c commit 53cc191
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 8 deletions.
96 changes: 92 additions & 4 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,24 @@ describe('inferTable', () => {
Schema Generated with mysql-schema-ts 1.2.0
*/
/**
* Exposes all fields present in agreements as a typescript
* interface.
*/
export interface Agreements {
id: string
billing_plan_id: string
category: string
name: string
}
/**
* Exposes the same fields as Agreements,
* but makes every field containing a DEFAULT value optional.
*
* This is especially useful when generating inserts, as you
* should be able to ommit these fields if you'd like
*/
export interface AgreementsWithDefaults {
id: string
billing_plan_id: string
Expand All @@ -81,13 +92,24 @@ describe('inferTable', () => {
Schema Generated with mysql-schema-ts 1.2.0
*/
/**
* Exposes all fields present in requests as a typescript
* interface.
*/
export interface Requests {
id: number
name: string
url: string
integration_type: 'source' | 'destination'
}
/**
* Exposes the same fields as Requests,
* but makes every field containing a DEFAULT value optional.
*
* This is especially useful when generating inserts, as you
* should be able to ommit these fields if you'd like
*/
export interface RequestsWithDefaults {
id: number
name: string
Expand All @@ -105,6 +127,10 @@ describe('inferTable', () => {
Schema Generated with mysql-schema-ts 1.2.0
*/
/**
* Exposes all fields present in complex as a typescript
* interface.
*/
export interface Complex {
id: string
name: string
Expand All @@ -116,6 +142,13 @@ describe('inferTable', () => {
documented_field?: string | null
}
/**
* Exposes the same fields as Complex,
* but makes every field containing a DEFAULT value optional.
*
* This is especially useful when generating inserts, as you
* should be able to ommit these fields if you'd like
*/
export interface ComplexWithDefaults {
id: string
name: string
Expand All @@ -142,12 +175,23 @@ describe('inferTable', () => {
export type JSONObject = { [member: string]: JSONValue }
export interface JSONArray extends Array<JSONValue> {}
export interface Table_with_json {
/**
* Exposes all fields present in table_with_json as a typescript
* interface.
*/
export interface TableWithJson {
id: string
data?: JSONValue | null
}
export interface Table_with_jsonWithDefaults {
/**
* Exposes the same fields as TableWithJson,
* but makes every field containing a DEFAULT value optional.
*
* This is especially useful when generating inserts, as you
* should be able to ommit these fields if you'd like
*/
export interface TableWithJsonWithDefaults {
id: string
data?: JSONValue | null
}
Expand All @@ -169,19 +213,34 @@ describe('inferSchema', () => {
export type JSONObject = { [member: string]: JSONValue }
export interface JSONArray extends Array<JSONValue> {}
/**
* Exposes all fields present in agreements as a typescript
* interface.
*/
export interface Agreements {
id: string
billing_plan_id: string
category: string
name: string
}
/**
* Exposes the same fields as Agreements,
* but makes every field containing a DEFAULT value optional.
*
* This is especially useful when generating inserts, as you
* should be able to ommit these fields if you'd like
*/
export interface AgreementsWithDefaults {
id: string
billing_plan_id: string
category: string
name: string
}
/**
* Exposes all fields present in complex as a typescript
* interface.
*/
export interface Complex {
id: string
name: string
Expand All @@ -193,6 +252,13 @@ describe('inferSchema', () => {
documented_field?: string | null
}
/**
* Exposes the same fields as Complex,
* but makes every field containing a DEFAULT value optional.
*
* This is especially useful when generating inserts, as you
* should be able to ommit these fields if you'd like
*/
export interface ComplexWithDefaults {
id: string
name: string
Expand All @@ -203,25 +269,47 @@ describe('inferSchema', () => {
/** This is an awesome field */
documented_field?: string | null
}
/**
* Exposes all fields present in requests as a typescript
* interface.
*/
export interface Requests {
id: number
name: string
url: string
integration_type: 'source' | 'destination'
}
/**
* Exposes the same fields as Requests,
* but makes every field containing a DEFAULT value optional.
*
* This is especially useful when generating inserts, as you
* should be able to ommit these fields if you'd like
*/
export interface RequestsWithDefaults {
id: number
name: string
url: string
integration_type: 'source' | 'destination'
}
export interface Table_with_json {
/**
* Exposes all fields present in table_with_json as a typescript
* interface.
*/
export interface TableWithJson {
id: string
data?: JSONValue | null
}
export interface Table_with_jsonWithDefaults {
/**
* Exposes the same fields as TableWithJson,
* but makes every field containing a DEFAULT value optional.
*
* This is especially useful when generating inserts, as you
* should be able to ommit these fields if you'd like
*/
export interface TableWithJsonWithDefaults {
id: string
data?: JSONValue | null
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"url": "https://github.com/nettofarah/mysql-schema-ts.git"
},
"dependencies": {
"camelcase": "^5.3.1",
"lodash": "^4.17.15",
"mysql": "^2.17.1",
"prettier": "^1.19.1",
Expand Down
21 changes: 17 additions & 4 deletions src/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import camelcase from 'camelcase'

export interface Column {
udtName: string
nullable: boolean
Expand All @@ -11,8 +13,8 @@ export interface Table {
[columnName: string]: Column
}

function capitalize(s: string) {
return s.charAt(0).toUpperCase() + s.slice(1)
function camelize(s: string): string {
return camelcase(s, { pascalCase: true })
}

function normalize(name: string): string {
Expand Down Expand Up @@ -45,11 +47,22 @@ export function tableToTS(name: string, table: Table): string {
})

return `
export interface ${capitalize(normalize(name))} {
/**
* Exposes all fields present in ${name} as a typescript
* interface.
*/
export interface ${camelize(normalize(name))} {
${members(false)}
}
export interface ${capitalize(normalize(name))}WithDefaults {
/**
* Exposes the same fields as ${camelize(normalize(name))},
* but makes every field containing a DEFAULT value optional.
*
* This is especially useful when generating inserts, as you
* should be able to ommit these fields if you'd like
*/
export interface ${camelize(normalize(name))}WithDefaults {
${members(true)}
}
`.trim()
Expand Down

0 comments on commit 53cc191

Please sign in to comment.