-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.ts
98 lines (92 loc) · 2.49 KB
/
index.ts
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
import * as Task from 'fp-ts/lib/Task'
import * as TaskEither from 'fp-ts/lib/TaskEither'
import { pipe } from 'fp-ts/lib/function'
import { Clients } from './clients'
import {
generateTypeScript,
validateStatement,
TsModule,
TsModuleDir,
generateIndexModule,
CodegenTarget,
} from './codegen'
import { describeStatement } from './describe'
import { inferStatementNullability } from './infer'
import { preprocessSQL } from './preprocess'
import { runPrettier } from './prettify'
import { StatementDescription } from './types'
import * as Warn from './Warn'
export function sqlToStatementDescription(
clients: Clients,
sql: string
): TaskEither.TaskEither<string, Warn.Warn<StatementDescription>> {
return pipe(
Task.of(sql),
Task.map(preprocessSQL),
TaskEither.chain((processed) =>
describeStatement(clients.postgres, processed.sql, processed.paramNames)
),
TaskEither.chain((stmt) => Task.of(validateStatement(stmt))),
TaskEither.chain((stmt) =>
TaskEither.rightTask(inferStatementNullability(clients.schema, stmt))
)
)
}
export function generateTSCode(
clients: Clients,
sourceFileName: string,
stmt: StatementDescription,
funcName: string,
options?: {
prettierFileName?: string | undefined
target?: CodegenTarget | undefined
module?: string | undefined
verbose?: boolean | undefined
terminalColumns?: number | undefined
}
): TaskEither.TaskEither<string, string> {
const {
prettierFileName = null,
target = 'pg',
module = 'pg',
} = options || {}
return pipe(
TaskEither.right(stmt),
TaskEither.chain((stmt) =>
TaskEither.rightTask(
generateTypeScript(
clients.types,
sourceFileName,
target,
module,
funcName,
stmt
)
)
),
TaskEither.chain((tsCode) =>
prettierFileName != null
? TaskEither.rightTask(() => runPrettier(prettierFileName, tsCode))
: TaskEither.right(tsCode)
)
)
}
export { TsModule, TsModuleDir } from './codegen'
export function indexModuleTS(
dirPath: string,
nestedDirs: TsModuleDir[],
tsModules: TsModule[],
options?: {
prettierFileName?: string | null | undefined
}
): Task.Task<string> {
const { prettierFileName = null } = options || {}
return pipe(
Task.of(generateIndexModule(dirPath, nestedDirs, tsModules)),
Task.chain((tsCode) =>
prettierFileName != null
? () => runPrettier(prettierFileName, tsCode)
: Task.of(tsCode)
)
)
}