-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtstype.ts
178 lines (158 loc) · 5.27 KB
/
tstype.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import * as ts from 'typescript'
import * as Option from 'fp-ts/lib/Option'
import * as Task from 'fp-ts/lib/Task'
import { pipe } from 'fp-ts/lib/function'
import { NamedValue, ValueType, Oid, TsType } from './types'
import * as postgres from './postgres'
import { schemaClient } from './schema'
export interface TypeClient {
tsType(valueType: ValueType, nullable: boolean): Task.Task<TsType>
columnType(column: NamedValue): Task.Task<{ name: string; type: TsType }>
}
export async function typeClient(
postgresClient: postgres.Sql<{}>
): Promise<TypeClient> {
let arrayTypes: Map<Oid, Oid> | null = null
let enums: Map<Oid, string> | null = null
function valueTsType(oid: Oid): string {
return nodePgBuiltinTypes.get(oid) || enums?.get(oid) || defaultType
}
function arrayTsType(oid: Oid, elemNullable: boolean): Option.Option<string> {
if (!nodePgBuiltinArrayTypes.get(oid)) {
// node-postgres won't convert this value to an array
return Option.none
}
const elemOid = arrayTypes?.get(oid)
if (!elemOid) {
// This type is not an array according to
// makeArrayTypesMap. Should not happen.
return Option.none
}
const elemType = valueTsType(elemOid) + (elemNullable ? ' | null' : '')
return Option.some(`Array<${elemType}>`)
}
function tsType(valueType: ValueType, nullable: boolean): Task.Task<TsType> {
return async () => {
if (arrayTypes == null)
arrayTypes = await makeArrayTypesMap(postgresClient)
if (enums == null) enums = await makeEnumMap(postgresClient)
const result = ValueType.walk(valueType, {
array: ({ oid, elemNullable }) =>
pipe(
arrayTsType(oid, elemNullable),
Option.getOrElse(() => defaultType)
),
any: ({ oid }) =>
pipe(
// If it's an array type its items can be NULL, unless we
// know how the array was constructed (see the array handler
// above).
arrayTsType(oid, true),
Option.getOrElse(() => valueTsType(oid))
),
})
return nullable ? `${result} | null` : result
}
}
function columnType(
column: NamedValue
): Task.Task<{ name: string; type: TsType }> {
return pipe(
tsType(column.type, column.nullable),
Task.map((type) => ({
name: column.name,
type,
}))
)
}
return {
tsType,
columnType,
}
}
// Equals to default type conversion of node-postgres:
// https://github.com/brianc/node-pg-types/blob/master/lib/textParsers.js
//
// Note that array types have been removed, since we handle them
// specially.
//
export const nodePgBuiltinTypes = new Map<Oid, TsType>([
[20, 'string'], // int8
[21, 'number'], // int2
[23, 'number'], // int4
[26, 'number'], // oid
[700, 'number'], // float4/real
[701, 'number'], // float8/double
[16, 'boolean'], // bool
[1082, 'Date'], // date
[1114, 'Date'], // timestamp without timezone
[1184, 'Date'], // timestamp
[600, '{ x: number; y: number }'], // point
[718, '{ x: number; y: number; radius: number }'], // circle
[1186, '{ hours: number; minutes: number; seconds: number }'], // interval
[17, 'Buffer'], // bytea
// TODO: JSON could be typed more accurately because it only has
// string, number, boolean, null, and objects and arrays of them
[114, 'any'], // json
[3802, 'any'], // jsonb
])
export const nodePgBuiltinArrayTypes = new Map<Oid, TsType>([
[651, 'string[]'], // cidr[]
[1000, 'boolean[]'], // bool array
[1001, 'Buffer[]'], // bytea array
[1005, 'number[]'], // _int2
[1007, 'number[]'], // _int4
[1028, 'number[]'], // oid[]
[1016, 'string[]'], // _int8
[1017, '{ x: number; y: number }[]'], // point[]
[1021, 'number[]'], // _float4
[1022, 'number[]'], // _float8
[1231, 'string[]'], // _numeric
[1014, 'string[]'], // char
[1015, 'string[]'], // varchar
[1008, 'string[]'],
[1009, 'string[]'],
[1040, 'string[]'], // macaddr[]
[1041, 'string[]'], // inet[]
[1115, 'Date[]'], // timestamp without time zone[]
[1182, 'Date[]'], // _date
[1185, 'Date[]'], // timestamp with time zone[]
[1187, '{ hours: number; minutes: number, seconds: number }[]'], // interval array
[199, 'any[]'], // json[]
[3807, 'any[]'], // jsonb[]
[3907, 'string[]'], // numrange[]
[2951, 'string[]'], // uuid[]
[791, 'string[]'], // money[]
[1183, 'string[]'], // time[]
[1270, 'string[]'], // timetz[]
])
export const defaultType: TsType = 'string'
async function makeArrayTypesMap(
postgresClient: postgres.Sql<{}>
): Promise<Map<Oid, Oid>> {
const arrayTypes = await schemaClient(postgresClient).getArrayTypes()
return new Map(arrayTypes.map(({ oid, elemType }) => [oid, elemType]))
}
async function makeEnumMap(
postgresClient: postgres.Sql<{}>
): Promise<Map<Oid, TsType>> {
const enums = await schemaClient(postgresClient).getEnums()
return new Map(
enums.map((enumType) => [
enumType.oid,
tsUnion(enumType.labels.map(tsStringLiteral)),
])
)
}
function tsUnion(arr: TsType[]): TsType {
return arr.join(' | ')
}
function tsStringLiteral(str: string): string {
return ts
.createPrinter()
.printNode(
ts.EmitHint.Expression,
ts.factory.createStringLiteral(str),
ts.createSourceFile('', '', ts.ScriptTarget.Latest)
)
}