From e63953f83d0b4f98c58b4762c01c2071b1bf8e16 Mon Sep 17 00:00:00 2001 From: Bradley Ayers Date: Sat, 10 Mar 2018 15:13:30 +1100 Subject: [PATCH] Fix name clashes for UDT, resolves #86 --- src/schemaMysql.ts | 3 ++- src/schemaPostgres.ts | 4 ++-- src/typescript.ts | 9 +++++++++ test/unit/typescript.test.ts | 8 ++++++-- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/schemaMysql.ts b/src/schemaMysql.ts index f5beff1..dd9f0d7 100644 --- a/src/schemaMysql.ts +++ b/src/schemaMysql.ts @@ -3,6 +3,7 @@ import { mapValues, keys, isEqual } from 'lodash' import { parse as urlParse } from 'url' import { TableDefinition, Database } from './schemaInterfaces' import Options from './options' +import { transformEnumNameForReference } from './typescript' export class MysqlDatabase implements Database { private db: mysql.IConnection @@ -71,7 +72,7 @@ export class MysqlDatabase implements Database { return column default: if (customTypes.indexOf(column.udtName) !== -1) { - column.tsType = options.transformTypeName(column.udtName) + column.tsType = transformEnumNameForReference(column.udtName) return column } else { console.log(`Type [${column.udtName}] has been mapped to [any] because no specific type has been found.`) diff --git a/src/schemaPostgres.ts b/src/schemaPostgres.ts index 2acceb9..51c00b5 100644 --- a/src/schemaPostgres.ts +++ b/src/schemaPostgres.ts @@ -2,8 +2,8 @@ import * as PgPromise from 'pg-promise' import { mapValues } from 'lodash' import { keys } from 'lodash' import Options from './options' - import { TableDefinition, Database } from './schemaInterfaces' +import { transformEnumNameForReference } from './typescript' const pgp = PgPromise() @@ -81,7 +81,7 @@ export class PostgresDatabase implements Database { return column default: if (customTypes.indexOf(column.udtName) !== -1) { - column.tsType = options.transformTypeName(column.udtName) + column.tsType = transformEnumNameForReference(column.udtName) return column } else { console.log(`Type [${column.udtName} has been mapped to [any] because no specific type has been found.`) diff --git a/src/typescript.ts b/src/typescript.ts index 0add9e0..44e6d51 100644 --- a/src/typescript.ts +++ b/src/typescript.ts @@ -7,6 +7,7 @@ import * as _ from 'lodash' import { TableDefinition } from './schemaInterfaces' import Options from './options' +import { upperFirst, camelCase } from 'lodash' function nameIsReservedKeyword (name: string): boolean { const reservedKeywords = [ @@ -25,6 +26,10 @@ function normalizeName (name: string, options: Options): string { } } +export function transformEnumNameForReference (name: string): string { + return upperFirst(camelCase(name)) +} + export function generateTableInterface (tableNameRaw: string, tableDefinition: TableDefinition, options: Options) { const tableName = options.transformTypeName(tableNameRaw) let members = '' @@ -46,6 +51,10 @@ export function generateEnumType (enumObject: any, options: Options) { enumString += `export type ${enumName} = ` enumString += enumObject[enumNameRaw].map((v: string) => `'${v}'`).join(' | ') enumString += ';\n' + const enumNameReference = transformEnumNameForReference(enumName) + if (enumNameReference !== enumName) { + enumString += `export type ${enumNameReference} = ${enumName};\n` + } } return enumString } diff --git a/test/unit/typescript.test.ts b/test/unit/typescript.test.ts index 2be0bdb..3150a10 100644 --- a/test/unit/typescript.test.ts +++ b/test/unit/typescript.test.ts @@ -67,7 +67,9 @@ describe('Typescript', () => { }, options) assert.equal(enumType, 'export type enum1 = \'val1\' | \'val2\' | \'val3\' | \'val4\';\n' + - 'export type enum2 = \'val5\' | \'val6\' | \'val7\' | \'val8\';\n') + 'export type Enum1 = enum1;\n' + + 'export type enum2 = \'val5\' | \'val6\' | \'val7\' | \'val8\';\n' + + 'export type Enum2 = enum2;\n') }) }) describe('generateEnumType', () => { @@ -82,7 +84,9 @@ describe('Typescript', () => { }, options) assert.equal(enumType, 'export type enum1 = \'val1\' | \'val2\' | \'val3\' | \'val4\';\n' + - 'export type enum2 = \'val5\' | \'val6\' | \'val7\' | \'val8\';\n') + 'export type Enum1 = enum1;\n' + + 'export type enum2 = \'val5\' | \'val6\' | \'val7\' | \'val8\';\n' + + 'export type Enum2 = enum2;\n') }) }) describe('generateTableTypes', () => {