From bce68ec4883ac8a248ebb99beb2776a3249418b3 Mon Sep 17 00:00:00 2001 From: HoldYourWaffle Date: Tue, 4 Jun 2019 00:00:12 +0200 Subject: [PATCH 1/5] Seperate camelCase options (types/columns) --- src/options.ts | 6 +- .../postgres/osm-camelcase-columns.ts | 134 ++++++++++++++++++ test/expected/postgres/osm-camelcase-types.ts | 133 +++++++++++++++++ .../postgres/osm-camelcase-columns.json | 6 + .../fixture/postgres/osm-camelcase-types.json | 6 + test/integration/schematGeneration.test.ts | 16 +++ 6 files changed, 299 insertions(+), 2 deletions(-) create mode 100644 test/expected/postgres/osm-camelcase-columns.ts create mode 100644 test/expected/postgres/osm-camelcase-types.ts create mode 100644 test/fixture/postgres/osm-camelcase-columns.json create mode 100644 test/fixture/postgres/osm-camelcase-types.json diff --git a/src/options.ts b/src/options.ts index 139aeaa..07c15c9 100644 --- a/src/options.ts +++ b/src/options.ts @@ -7,6 +7,8 @@ const DEFAULT_OPTIONS: OptionValues = { export type OptionValues = { camelCase?: boolean + camelCaseTypes?: boolean + camelCaseColumns?: boolean writeHeader?: boolean // write schemats description header } @@ -18,10 +20,10 @@ export default class Options { } transformTypeName (typename: string) { - return this.options.camelCase ? upperFirst(camelCase(typename)) : typename + return this.options.camelCaseTypes || this.options.camelCase ? upperFirst(camelCase(typename)) : typename } transformColumnName (columnName: string) { - return this.options.camelCase ? camelCase(columnName) : columnName + return this.options.camelCaseColumns || this.options.camelCase ? camelCase(columnName) : columnName } } diff --git a/test/expected/postgres/osm-camelcase-columns.ts b/test/expected/postgres/osm-camelcase-columns.ts new file mode 100644 index 0000000..5d379f3 --- /dev/null +++ b/test/expected/postgres/osm-camelcase-columns.ts @@ -0,0 +1,134 @@ +/* tslint:disable */ + +export type FormatEnum = 'html' | 'markdown' | 'text'; +export type UserStatusEnum = 'active' | 'confirmed' | 'deleted' | 'pending' | 'suspended'; + +export namespace usersFields { + export type email = string; + export type id = number; + export type passCrypt = string; + export type creationTime = Date; + export type displayName = string; + export type dataPublic = boolean; + export type description = string; + export type homeLat = number | null; + export type homeLon = number | null; + export type homeZoom = number | null; + export type nearby = number | null; + export type passSalt = string | null; + export type imageFileName = string | null; + export type emailValid = boolean; + export type newEmail = string | null; + export type creationIp = string | null; + export type languages = string | null; + export type status = UserStatusEnum; + export type termsAgreed = Date | null; + export type considerPd = boolean; + export type preferredEditor = string | null; + export type termsSeen = boolean; + export type authUid = string | null; + export type descriptionFormat = FormatEnum; + export type imageFingerprint = string | null; + export type changesetsCount = number; + export type tracesCount = number; + export type diaryEntriesCount = number; + export type imageUseGravatar = boolean; + export type imageContentType = string | null; + export type authProvider = string | null; + export type uuidColumn = string | null; + export type number_ = number | null; + export type string_ = string | null; + export type moneyCol = number | null; + export type charCol = string | null; + export type timeCol = string | null; + export type inetCol = string | null; + export type jsonbCol = Object | null; + export type numericCol = number | null; + export type byteaCol = string | null; + export type boolArrayCol = Array | null; + export type varcharArrayCol = Array | null; + export type int2ArrayCol = Array | null; + export type int4ArrayCol = Array | null; + export type int8ArrayCol = Array | null; + export type uuidArrayCol = Array | null; + export type textArrayCol = Array | null; + export type byteaArrayCol = Array | null; + export type realCol = number | null; + export type doubleCol = number | null; + export type timeWithTz = string | null; + export type oidCol = number | null; + export type intervalCol = string | null; + export type jsonCol = Object | null; + export type dateCol = Date | null; + export type unspportedPathType = any | null; + export type nameTypeCol = string | null; + export type jsonArrayCol = Array | null; + export type jsonbArrayCol = Array | null; + export type timestamptzArrayCol = Array | null; + +} + +export interface Users { + email: usersFields.email; + id: usersFields.id; + passCrypt: usersFields.passCrypt; + creationTime: usersFields.creationTime; + displayName: usersFields.displayName; + dataPublic: usersFields.dataPublic; + description: usersFields.description; + homeLat: usersFields.homeLat; + homeLon: usersFields.homeLon; + homeZoom: usersFields.homeZoom; + nearby: usersFields.nearby; + passSalt: usersFields.passSalt; + imageFileName: usersFields.imageFileName; + emailValid: usersFields.emailValid; + newEmail: usersFields.newEmail; + creationIp: usersFields.creationIp; + languages: usersFields.languages; + status: usersFields.status; + termsAgreed: usersFields.termsAgreed; + considerPd: usersFields.considerPd; + preferredEditor: usersFields.preferredEditor; + termsSeen: usersFields.termsSeen; + authUid: usersFields.authUid; + descriptionFormat: usersFields.descriptionFormat; + imageFingerprint: usersFields.imageFingerprint; + changesetsCount: usersFields.changesetsCount; + tracesCount: usersFields.tracesCount; + diaryEntriesCount: usersFields.diaryEntriesCount; + imageUseGravatar: usersFields.imageUseGravatar; + imageContentType: usersFields.imageContentType; + authProvider: usersFields.authProvider; + uuidColumn: usersFields.uuidColumn; + number: usersFields.number_; + string: usersFields.string_; + moneyCol: usersFields.moneyCol; + charCol: usersFields.charCol; + timeCol: usersFields.timeCol; + inetCol: usersFields.inetCol; + jsonbCol: usersFields.jsonbCol; + numericCol: usersFields.numericCol; + byteaCol: usersFields.byteaCol; + boolArrayCol: usersFields.boolArrayCol; + varcharArrayCol: usersFields.varcharArrayCol; + int2ArrayCol: usersFields.int2ArrayCol; + int4ArrayCol: usersFields.int4ArrayCol; + int8ArrayCol: usersFields.int8ArrayCol; + uuidArrayCol: usersFields.uuidArrayCol; + textArrayCol: usersFields.textArrayCol; + byteaArrayCol: usersFields.byteaArrayCol; + realCol: usersFields.realCol; + doubleCol: usersFields.doubleCol; + timeWithTz: usersFields.timeWithTz; + oidCol: usersFields.oidCol; + intervalCol: usersFields.intervalCol; + jsonCol: usersFields.jsonCol; + dateCol: usersFields.dateCol; + unspportedPathType: usersFields.unspportedPathType; + nameTypeCol: usersFields.nameTypeCol; + jsonArrayCol: usersFields.jsonArrayCol; + jsonbArrayCol: usersFields.jsonbArrayCol; + timestamptzArrayCol: usersFields.timestamptzArrayCol; + +} diff --git a/test/expected/postgres/osm-camelcase-types.ts b/test/expected/postgres/osm-camelcase-types.ts new file mode 100644 index 0000000..91536f9 --- /dev/null +++ b/test/expected/postgres/osm-camelcase-types.ts @@ -0,0 +1,133 @@ +/* tslint:disable */ + +export type format_enum = 'html' | 'markdown' | 'text'; +export type user_status_enum = 'active' | 'confirmed' | 'deleted' | 'pending' | 'suspended'; + +export namespace UsersFields { + export type email = string; + export type id = number; + export type pass_crypt = string; + export type creation_time = Date; + export type display_name = string; + export type data_public = boolean; + export type description = string; + export type home_lat = number | null; + export type home_lon = number | null; + export type home_zoom = number | null; + export type nearby = number | null; + export type pass_salt = string | null; + export type image_file_name = string | null; + export type email_valid = boolean; + export type new_email = string | null; + export type creation_ip = string | null; + export type languages = string | null; + export type status = user_status_enum; + export type terms_agreed = Date | null; + export type consider_pd = boolean; + export type preferred_editor = string | null; + export type terms_seen = boolean; + export type auth_uid = string | null; + export type description_format = format_enum; + export type image_fingerprint = string | null; + export type changesets_count = number; + export type traces_count = number; + export type diary_entries_count = number; + export type image_use_gravatar = boolean; + export type image_content_type = string | null; + export type auth_provider = string | null; + export type uuid_column = string | null; + export type number_ = number | null; + export type string_ = string | null; + export type money_col = number | null; + export type char_col = string | null; + export type time_col = string | null; + export type inet_col = string | null; + export type jsonb_col = Object | null; + export type numeric_col = number | null; + export type bytea_col = string | null; + export type bool_array_col = Array | null; + export type varchar_array_col = Array | null; + export type int2_array_col = Array | null; + export type int4_array_col = Array | null; + export type int8_array_col = Array | null; + export type uuid_array_col = Array | null; + export type text_array_col = Array | null; + export type bytea_array_col = Array | null; + export type real_col = number | null; + export type double_col = number | null; + export type time_with_tz = string | null; + export type oid_col = number | null; + export type interval_col = string | null; + export type json_col = Object | null; + export type date_col = Date | null; + export type unspported_path_type = any | null; + export type name_type_col = string | null; + export type json_array_col = Array | null; + export type jsonb_array_col = Array | null; + export type timestamptz_array_col = Array | null; + +} + +export interface users { + email: UsersFields.email; + id: UsersFields.id; + pass_crypt: UsersFields.pass_crypt; + creation_time: UsersFields.creation_time; + display_name: UsersFields.display_name; + data_public: UsersFields.data_public; + description: UsersFields.description; + home_lat: UsersFields.home_lat; + home_lon: UsersFields.home_lon; + home_zoom: UsersFields.home_zoom; + nearby: UsersFields.nearby; + pass_salt: UsersFields.pass_salt; + image_file_name: UsersFields.image_file_name; + email_valid: UsersFields.email_valid; + new_email: UsersFields.new_email; + creation_ip: UsersFields.creation_ip; + languages: UsersFields.languages; + status: UsersFields.status; + terms_agreed: UsersFields.terms_agreed; + consider_pd: UsersFields.consider_pd; + preferred_editor: UsersFields.preferred_editor; + terms_seen: UsersFields.terms_seen; + auth_uid: UsersFields.auth_uid; + description_format: UsersFields.description_format; + image_fingerprint: UsersFields.image_fingerprint; + changesets_count: UsersFields.changesets_count; + traces_count: UsersFields.traces_count; + diary_entries_count: UsersFields.diary_entries_count; + image_use_gravatar: UsersFields.image_use_gravatar; + image_content_type: UsersFields.image_content_type; + auth_provider: UsersFields.auth_provider; + uuid_column: UsersFields.uuid_column; + number: UsersFields.number_; + string: UsersFields.string_; + money_col: UsersFields.money_col; + char_col: UsersFields.char_col; + time_col: UsersFields.time_col; + inet_col: UsersFields.inet_col; + jsonb_col: UsersFields.jsonb_col; + numeric_col: UsersFields.numeric_col; + bytea_col: UsersFields.bytea_col; + bool_array_col: UsersFields.bool_array_col; + varchar_array_col: UsersFields.varchar_array_col; + int2_array_col: UsersFields.int2_array_col; + int4_array_col: UsersFields.int4_array_col; + int8_array_col: UsersFields.int8_array_col; + uuid_array_col: UsersFields.uuid_array_col; + text_array_col: UsersFields.text_array_col; + bytea_array_col: UsersFields.bytea_array_col; + real_col: UsersFields.real_col; + double_col: UsersFields.double_col; + time_with_tz: UsersFields.time_with_tz; + oid_col: UsersFields.oid_col; + interval_col: UsersFields.interval_col; + json_col: UsersFields.json_col; + date_col: UsersFields.date_col; + unspported_path_type: UsersFields.unspported_path_type; + name_type_col: UsersFields.name_type_col; + json_array_col: UsersFields.json_array_col; + jsonb_array_col: UsersFields.jsonb_array_col; + timestamptz_array_col: UsersFields.timestamptz_array_col; +} diff --git a/test/fixture/postgres/osm-camelcase-columns.json b/test/fixture/postgres/osm-camelcase-columns.json new file mode 100644 index 0000000..fe35f4a --- /dev/null +++ b/test/fixture/postgres/osm-camelcase-columns.json @@ -0,0 +1,6 @@ +{ + "tables": ["users"], + "schema": null, + "camelCaseColumns": true, + "writeHeader": false +} diff --git a/test/fixture/postgres/osm-camelcase-types.json b/test/fixture/postgres/osm-camelcase-types.json new file mode 100644 index 0000000..d3b3af5 --- /dev/null +++ b/test/fixture/postgres/osm-camelcase-types.json @@ -0,0 +1,6 @@ +{ + "tables": ["users"], + "schema": null, + "camelCaseTypes": true, + "writeHeader": false +} diff --git a/test/integration/schematGeneration.test.ts b/test/integration/schematGeneration.test.ts index 06e06d2..3779f14 100644 --- a/test/integration/schematGeneration.test.ts +++ b/test/integration/schematGeneration.test.ts @@ -29,6 +29,22 @@ describe('schemat generation integration testing', () => { await writeTsFile(inputSQLFile, config, outputFile, db) return assert(await compare(expectedFile, outputFile)) }) + it('Camelcase (types only) generation', async () => { + const inputSQLFile = 'test/fixture/postgres/osm.sql' + const outputFile = './test/actual/postgres/osm-camelcase-types.ts' + const expectedFile = './test/expected/postgres/osm-camelcase-types.ts' + const config: any = './fixture/postgres/osm-camelcase-types.json' + await writeTsFile(inputSQLFile, config, outputFile, db) + return assert(await compare(expectedFile, outputFile)) + }) + it('Camelcase (columns only) generation', async () => { + const inputSQLFile = 'test/fixture/postgres/osm.sql' + const outputFile = './test/actual/postgres/osm-camelcase-columns.ts' + const expectedFile = './test/expected/postgres/osm-camelcase-columns.ts' + const config: any = './fixture/postgres/osm-camelcase-columns.json' + await writeTsFile(inputSQLFile, config, outputFile, db) + return assert(await compare(expectedFile, outputFile)) + }) }) describe('mysql', () => { From af16ebe51b8d1292eba7d8fe67f1d8d739d11dff Mon Sep 17 00:00:00 2001 From: HoldYourWaffle Date: Tue, 4 Jun 2019 00:00:32 +0200 Subject: [PATCH 2/5] Fix strict undefined check error on build --- test/integration/cli.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/cli.test.ts b/test/integration/cli.test.ts index 298c14e..32dc347 100644 --- a/test/integration/cli.test.ts +++ b/test/integration/cli.test.ts @@ -11,7 +11,7 @@ describe('schemats cli tool integration testing', () => { it('should run without error', () => { let {status, stdout, stderr} = spawnSync('node', [ 'bin/schemats', 'generate', - '-c', process.env.POSTGRES_URL, + '-c', process.env.POSTGRES_URL as string, '-o', '/tmp/schemats_cli_postgres.ts' ], { encoding: 'utf-8' }) console.log('opopopopop', stdout, stderr) @@ -27,7 +27,7 @@ describe('schemats cli tool integration testing', () => { it('should run without error', () => { let {status} = spawnSync('node', [ 'bin/schemats', 'generate', - '-c', process.env.MYSQL_URL, + '-c', process.env.MYSQL_URL as string, '-s', 'test', '-o', '/tmp/schemats_cli_postgres.ts' ]) From 50b726eedcae6f2d2e970ecc0802b4432c2e3835 Mon Sep 17 00:00:00 2001 From: HoldYourWaffle Date: Tue, 4 Jun 2019 00:25:17 +0200 Subject: [PATCH 3/5] Update CLI options passing --- bin/schemats.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/bin/schemats.ts b/bin/schemats.ts index d46ae74..b7bda7f 100755 --- a/bin/schemats.ts +++ b/bin/schemats.ts @@ -15,6 +15,8 @@ interface SchematsConfig { schema: string, output: string, camelCase: boolean, + camelCaseTypes: boolean, + camelCaseColumns: boolean, noHeader: boolean, } @@ -39,7 +41,11 @@ let argv: SchematsConfig = yargs .nargs('s', 1) .describe('s', 'schema name') .alias('C', 'camelCase') - .describe('C', 'Camel-case columns') + .describe('C', 'Camel-case columns and types') + .alias('CT', 'camelCaseTypes') + .describe('CT', 'Camel-case only types') + .alias('CC', 'camelCaseColumns') + .describe('CC', 'Camel-case only columns') .describe('noHeader', 'Do not write header') .demand('o') .nargs('o', 1) @@ -61,7 +67,7 @@ let argv: SchematsConfig = yargs } let formattedOutput = await typescriptOfSchema( - argv.conn, argv.table, argv.schema, { camelCase: argv.camelCase, writeHeader: !argv.noHeader }) + argv.conn, argv.table, argv.schema, { camelCase: argv.camelCase, camelCaseColumns: argv.camelCaseColumns, camelCaseTypes: argv.camelCaseTypes, writeHeader: !argv.noHeader }) fs.writeFileSync(argv.output, formattedOutput) } catch (e) { From 9cd7e01d5a59203d72372234365eec77c76194b3 Mon Sep 17 00:00:00 2001 From: HoldYourWaffle Date: Wed, 12 Jun 2019 23:04:52 +0200 Subject: [PATCH 4/5] Overload existing camelCase option instead of creating new ones --- bin/schemats.ts | 11 +++-------- src/options.ts | 8 +++----- test/fixture/postgres/osm-camelcase-columns.json | 2 +- test/fixture/postgres/osm-camelcase-types.json | 2 +- 4 files changed, 8 insertions(+), 15 deletions(-) diff --git a/bin/schemats.ts b/bin/schemats.ts index b7bda7f..f3ee62b 100755 --- a/bin/schemats.ts +++ b/bin/schemats.ts @@ -14,9 +14,7 @@ interface SchematsConfig { table: string[] | string, schema: string, output: string, - camelCase: boolean, - camelCaseTypes: boolean, - camelCaseColumns: boolean, + camelCase: boolean | 'columns' | 'types', noHeader: boolean, } @@ -41,11 +39,8 @@ let argv: SchematsConfig = yargs .nargs('s', 1) .describe('s', 'schema name') .alias('C', 'camelCase') + .choices('C', [true, 'columns', 'types']) .describe('C', 'Camel-case columns and types') - .alias('CT', 'camelCaseTypes') - .describe('CT', 'Camel-case only types') - .alias('CC', 'camelCaseColumns') - .describe('CC', 'Camel-case only columns') .describe('noHeader', 'Do not write header') .demand('o') .nargs('o', 1) @@ -67,7 +62,7 @@ let argv: SchematsConfig = yargs } let formattedOutput = await typescriptOfSchema( - argv.conn, argv.table, argv.schema, { camelCase: argv.camelCase, camelCaseColumns: argv.camelCaseColumns, camelCaseTypes: argv.camelCaseTypes, writeHeader: !argv.noHeader }) + argv.conn, argv.table, argv.schema, { camelCase: argv.camelCase, writeHeader: !argv.noHeader }) fs.writeFileSync(argv.output, formattedOutput) } catch (e) { diff --git a/src/options.ts b/src/options.ts index 07c15c9..008340c 100644 --- a/src/options.ts +++ b/src/options.ts @@ -6,9 +6,7 @@ const DEFAULT_OPTIONS: OptionValues = { } export type OptionValues = { - camelCase?: boolean - camelCaseTypes?: boolean - camelCaseColumns?: boolean + camelCase?: boolean | 'columns' | 'types', writeHeader?: boolean // write schemats description header } @@ -20,10 +18,10 @@ export default class Options { } transformTypeName (typename: string) { - return this.options.camelCaseTypes || this.options.camelCase ? upperFirst(camelCase(typename)) : typename + return this.options.camelCase === true || this.options.camelCase === 'types' ? upperFirst(camelCase(typename)) : typename } transformColumnName (columnName: string) { - return this.options.camelCaseColumns || this.options.camelCase ? camelCase(columnName) : columnName + return this.options.camelCase === true || this.options.camelCase === 'columns' ? camelCase(columnName) : columnName } } diff --git a/test/fixture/postgres/osm-camelcase-columns.json b/test/fixture/postgres/osm-camelcase-columns.json index fe35f4a..a15e301 100644 --- a/test/fixture/postgres/osm-camelcase-columns.json +++ b/test/fixture/postgres/osm-camelcase-columns.json @@ -1,6 +1,6 @@ { "tables": ["users"], "schema": null, - "camelCaseColumns": true, + "camelCaseColumns": "columns", "writeHeader": false } diff --git a/test/fixture/postgres/osm-camelcase-types.json b/test/fixture/postgres/osm-camelcase-types.json index d3b3af5..2d543aa 100644 --- a/test/fixture/postgres/osm-camelcase-types.json +++ b/test/fixture/postgres/osm-camelcase-types.json @@ -1,6 +1,6 @@ { "tables": ["users"], "schema": null, - "camelCaseTypes": true, + "camelCase": "types", "writeHeader": false } From a09ba8a1fddef75456f01052d982bbc8e6901bad Mon Sep 17 00:00:00 2001 From: Ravi van Rooijen Date: Thu, 29 Aug 2019 16:03:33 +0200 Subject: [PATCH 5/5] Add 'undefined' to the possibilities for the camelCase CLI option --- bin/schemats.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/schemats.ts b/bin/schemats.ts index f3ee62b..ab14868 100755 --- a/bin/schemats.ts +++ b/bin/schemats.ts @@ -39,7 +39,7 @@ let argv: SchematsConfig = yargs .nargs('s', 1) .describe('s', 'schema name') .alias('C', 'camelCase') - .choices('C', [true, 'columns', 'types']) + .choices('C', [undefined, true, 'columns', 'types']) .describe('C', 'Camel-case columns and types') .describe('noHeader', 'Do not write header') .demand('o')