-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathtypescript.ts
More file actions
97 lines (80 loc) · 3.06 KB
/
Copy pathtypescript.ts
File metadata and controls
97 lines (80 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* Generate typescript interface from table schema
* Created by xiamx on 2016-08-10.
*/
import * as _ from 'lodash'
import { TableDefinition } from './schemaInterfaces'
import Options from './options'
function nameIsReservedKeyword (name: string): boolean {
const reservedKeywords = [
'string',
'number',
'package'
]
return reservedKeywords.indexOf(name) !== -1
}
function normalizeName (name: string, options: Options): string {
if (nameIsReservedKeyword(name)) {
return name + '_'
} else {
return name
}
}
export function generateTableInterface (tableNameRaw: string, tableDefinition: TableDefinition, options: Options) {
const tableName = options.transformTypeName(tableNameRaw)
let members = ''
Object.keys(tableDefinition).map(c => options.transformColumnName(c)).forEach((columnName) => {
members += `${columnName}: ${tableName}Fields.${normalizeName(columnName, options)};\n`
})
return `
interface ${normalizeName(tableName, options)}Meta {
${members}
}
`
}
export function generateEnumType (enumObject: any, options: Options) {
let enumString = ''
for (let enumNameRaw in enumObject) {
const enumName = options.transformTypeName(enumNameRaw)
enumString += `export type ${enumName} = `
enumString += enumObject[enumNameRaw].map((v: string) => `'${v}'`).join(' | ')
enumString += ';\n'
}
return enumString
}
export function generateTableTypes (tableNameRaw: string, tableDefinition: TableDefinition, options: Options) {
const tableName = options.transformTypeName(tableNameRaw)
let fields = ''
Object.keys(tableDefinition).forEach((columnNameRaw) => {
const columnName = options.transformColumnName(columnNameRaw)
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 `
export namespace ${tableName}Fields {
${fields}
}
`
}
export function generateExports (tableNameRaw: string, tableDefinition: TableDefinition, options: Options) {
const tableName = options.transformTypeName(tableNameRaw)
if (options.exposeConstraintInfo()) {
// If `--exposeConstraintInfo` flag is passed, simply rename <table>Meta to <table>
return `
export type ${normalizeName(tableName, options)} = ${normalizeName(tableName, options)}Meta
`
}
// If no `--exposeConstraintInfo` flag is passed, transform the meta interfaces to simple interfaces
return `
export type ${normalizeName(tableName, options)} = SimpleSchema<${normalizeName(tableName, options)}Meta>
`
}