Skip to content

Commit

Permalink
Output verbose schema information in "Fields" interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
frankpf committed Feb 10, 2018
1 parent 96239a7 commit 301bbc2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/schemaInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import Options from './options'
export interface ColumnDefinition {
udtName: string,
nullable: boolean,
tsType?: string
tsType?: string,
primaryKey?: boolean,
unique?: boolean
}

export interface TableDefinition {
Expand Down
20 changes: 18 additions & 2 deletions src/schemaPostgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class PostgresDatabase implements Database {

public async getTableDefinition (tableName: string, tableSchema: string) {
let tableDefinition: TableDefinition = {}
type T = { column_name: string, udt_name: string, is_nullable: string }
type T = { column_name: string, udt_name: string, is_nullable: string, constraint_type: 'UNIQUE' | 'PRIMARY KEY' }
await this.db.each<T>(
'SELECT column_name, udt_name, is_nullable ' +
'FROM information_schema.columns ' +
Expand All @@ -128,9 +128,25 @@ export class PostgresDatabase implements Database {
(schemaItem: T) => {
tableDefinition[schemaItem.column_name] = {
udtName: schemaItem.udt_name,
nullable: schemaItem.is_nullable === 'YES'
nullable: schemaItem.is_nullable === 'YES',
primaryKey: false,
unique: false
}
})

// Fill in PRIMARY KEY and UNIQUE constraint details
await this.db.each<T>(
'SELECT kcu.column_name, tc.constraint_type ' +
'FROM information_schema.table_constraints AS tc ' +
'JOIN information_schema.key_column_usage AS kcu ' +
'ON tc.constraint_name = kcu.constraint_name ' +
'WHERE tc.table_name = $1 and tc.table_schema = $2',
[tableName, tableSchema],
(schemaItem: T) => {
tableDefinition[schemaItem.column_name].unique = schemaItem.constraint_type === 'UNIQUE'
tableDefinition[schemaItem.column_name].primaryKey = schemaItem.constraint_type === 'PRIMARY KEY'
}
)
return tableDefinition
}

Expand Down
19 changes: 16 additions & 3 deletions src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,23 @@ export function generateTableTypes (tableNameRaw: string, tableDefinition: Table
const tableName = options.transformTypeName(tableNameRaw)
let fields = ''
Object.keys(tableDefinition).forEach((columnNameRaw) => {
let type = tableDefinition[columnNameRaw].tsType
let nullable = tableDefinition[columnNameRaw].nullable ? '| null' : ''
const columnName = options.transformColumnName(columnNameRaw)
fields += `export type ${normalizeName(columnName, options)} = ${type}${nullable};\n`
fields += `export type ${normalizeName(columnName, options)} = {`

const { tsType, nullable, primaryKey, unique } = tableDefinition[columnNameRaw]

// Mapped TS type
fields += `type: ${tsType}`
fields += nullable ? '| null' : ''
fields += `,`

// Primary key constraint
fields += primaryKey !== undefined ? `primaryKey: ${primaryKey},` : ''

// Unique constraint
fields += unique !== undefined ? `unique: ${unique},` : ''

fields += '};\n'
})

return `
Expand Down

0 comments on commit 301bbc2

Please sign in to comment.