Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix name clashes for UDT, resolves #86 #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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