-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathindex.ts
More file actions
131 lines (112 loc) · 3.97 KB
/
Copy pathindex.ts
File metadata and controls
131 lines (112 loc) · 3.97 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Schemats takes sql database schema and creates corresponding typescript definitions
* Created by xiamx on 2016-08-10.
*/
import { generateEnumType, generateTableTypes, generateTableInterface, generateExports } from './typescript'
import { getDatabase, Database } from './schema'
import Options, { OptionValues } from './options'
import { processString, Options as ITFOptions } from 'typescript-formatter'
const pkgVersion = require('../package.json').version
function getTime () {
let padTime = (value: number) => `0${value}`.slice(-2)
let time = new Date()
const yyyy = time.getFullYear()
const MM = padTime(time.getMonth() + 1)
const dd = padTime(time.getDate())
const hh = padTime(time.getHours())
const mm = padTime(time.getMinutes())
const ss = padTime(time.getSeconds())
return `${yyyy}-${MM}-${dd} ${hh}:${mm}:${ss}`
}
function buildHeader (db: Database, tables: string[], schema: string|null, options: OptionValues): string {
let commands = ['schemats', 'generate', '-c', db.connectionString.replace(/:\/\/.*@/,'://username:password@')]
if (options.camelCase) commands.push('-C')
if (tables.length > 0) {
tables.forEach((t: string) => {
commands.push('-t', t)
})
}
if (schema) {
commands.push('-s', schema)
}
return `
/**
* AUTO-GENERATED FILE @ ${getTime()} - DO NOT EDIT!
*
* This file was automatically generated by schemats v.${pkgVersion}
* $ ${commands.join(' ')}
*
*/
`
}
function helperTypes () {
return `
type HasTypeKey<T> = {
[K in keyof T]: {
type: any
}
}
type SimpleSchema<T extends HasTypeKey<T>> = {
[K in keyof T] : T[K]['type']
}
`
}
export async function typescriptOfTable (db: Database|string,
table: string,
schema: string,
options = new Options()) {
if (typeof db === 'string') {
db = getDatabase(db)
}
let interfaces = ''
let tableTypes = await db.getTableTypes(table, schema, options)
interfaces += generateTableTypes(table, tableTypes, options)
interfaces += generateTableInterface(table, tableTypes, options)
interfaces += generateExports(table, tableTypes, options)
return interfaces
}
export async function typescriptOfSchema (db: Database|string,
tables: string[] = [],
schema: string|null = null,
options: OptionValues = {}): Promise<string> {
if (typeof db === 'string') {
db = getDatabase(db)
}
if (!schema) {
schema = db.getDefaultSchema()
}
if (tables.length === 0) {
tables = await db.getSchemaTables(schema)
}
const optionsObject = new Options(options)
const enumTypes = generateEnumType(await db.getEnumTypes(schema), optionsObject)
const interfacePromises = tables.map((table) => typescriptOfTable(db, table, schema as string, optionsObject))
const interfaces = await Promise.all(interfacePromises)
.then(tsOfTable => tsOfTable.join(''))
let output = '/* tslint:disable */\n\n'
if (optionsObject.options.writeHeader) {
output += buildHeader(db, tables, schema, options)
}
if (!optionsObject.exposeConstraintInfo()) {
output += helperTypes()
}
output += enumTypes
output += interfaces
const formatterOption: ITFOptions = {
replace: false,
verify: false,
tsconfig: true,
tslint: true,
editorconfig: true,
tsfmt: true,
vscode: false,
tsconfigFile: null,
tslintFile: null,
vscodeFile: null,
tsfmtFile: null
}
const processedResult = await processString('schema.ts', output, formatterOption)
return processedResult.dest
}
export {Database, getDatabase} from './schema'
export {Options, OptionValues}