diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index d96bc56..b149e9e 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -57,6 +57,10 @@ 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 @@ -64,6 +68,13 @@ describe('inferTable', () => { 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 @@ -81,6 +92,10 @@ 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 @@ -88,6 +103,13 @@ describe('inferTable', () => { 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 @@ -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 @@ -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 @@ -142,12 +175,23 @@ describe('inferTable', () => { export type JSONObject = { [member: string]: JSONValue } export interface JSONArray extends Array {} - 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 } @@ -169,6 +213,10 @@ describe('inferSchema', () => { export type JSONObject = { [member: string]: JSONValue } export interface JSONArray extends Array {} + /** + * Exposes all fields present in agreements as a typescript + * interface. + */ export interface Agreements { id: string billing_plan_id: string @@ -176,12 +224,23 @@ describe('inferSchema', () => { 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 @@ -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 @@ -203,6 +269,10 @@ 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 @@ -210,18 +280,36 @@ describe('inferSchema', () => { 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 } diff --git a/package.json b/package.json index defcf11..be47fcf 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/typescript.ts b/src/typescript.ts index 5ede064..355e10c 100644 --- a/src/typescript.ts +++ b/src/typescript.ts @@ -1,3 +1,5 @@ +import camelcase from 'camelcase' + export interface Column { udtName: string nullable: boolean @@ -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 { @@ -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()