Skip to content

Commit

Permalink
Fix name clashes for UDT, resolves SweetIQ#86
Browse files Browse the repository at this point in the history
  • Loading branch information
bradleyayers committed Mar 10, 2018
1 parent 7c3d3e1 commit e63953f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/schemaMysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.`)
Expand Down
4 changes: 2 additions & 2 deletions src/schemaPostgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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.`)
Expand Down
9 changes: 9 additions & 0 deletions src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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 = ''
Expand All @@ -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
}
Expand Down
8 changes: 6 additions & 2 deletions test/unit/typescript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down

0 comments on commit e63953f

Please sign in to comment.