-
-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathpython.ts
More file actions
416 lines (370 loc) · 11.2 KB
/
python.ts
File metadata and controls
416 lines (370 loc) · 11.2 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import type {
PostgresColumn,
PostgresMaterializedView,
PostgresSchema,
PostgresTable,
PostgresType,
PostgresView,
} from '../../lib/index.js'
import type { GeneratorMetadata } from '../../lib/generators.js'
export const apply = ({
schemas,
tables,
views,
materializedViews,
columns,
types,
}: GeneratorMetadata): string => {
const ctx = new PythonContext(types, columns, schemas)
// Used for efficient lookup of types by schema name
const schemasNames = new Set(schemas.map((schema) => schema.name))
const py_tables = tables.flatMap((table) => {
const py_class_and_methods = ctx.tableToClass(table)
return py_class_and_methods
})
const composite_types = types
// We always include system schemas, so we need to filter out types that are not in the included schemas
.filter((type) => type.attributes.length > 0 && schemasNames.has(type.schema))
.map((type) => ctx.typeToClass(type))
const py_views = views.map((view) => ctx.viewToClass(view))
const py_matviews = materializedViews.map((matview) => ctx.matViewToClass(matview))
let output = `
from __future__ import annotations
import datetime
import uuid
from typing import (
Annotated,
Any,
List,
Literal,
NotRequired,
Optional,
TypeAlias,
TypedDict,
)
from pydantic import BaseModel, Field, Json
${concatLines(Object.values(ctx.user_enums))}
${concatLines(py_tables)}
${concatLines(py_views)}
${concatLines(py_matviews)}
${concatLines(composite_types)}
`.trim()
return output
}
interface Serializable {
serialize(): string
}
class PythonContext {
types: { [k: string]: PostgresType }
user_enums: { [k: string]: PythonEnum }
columns: Record<number, PostgresColumn[]>
schemas: { [k: string]: PostgresSchema }
constructor(types: PostgresType[], columns: PostgresColumn[], schemas: PostgresSchema[]) {
this.schemas = Object.fromEntries(schemas.map((schema) => [schema.name, schema]))
this.types = Object.fromEntries(types.map((type) => [type.name, type]))
this.columns = columns
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
.reduce(
(acc, curr) => {
acc[curr.table_id] ??= []
acc[curr.table_id].push(curr)
return acc
},
{} as Record<number, PostgresColumn[]>
)
this.user_enums = Object.fromEntries(
types.filter((type) => type.enums.length > 0).map((type) => [type.name, new PythonEnum(type)])
)
}
resolveTypeName(name: string): string {
if (name in this.user_enums) {
return this.user_enums[name].name
}
if (name in PY_TYPE_MAP) {
return PY_TYPE_MAP[name]
}
if (name in this.types) {
const type = this.types[name]
const schema = type!.schema
return `${formatForPyClassName(schema)}${formatForPyClassName(type.name)}`
}
return 'Any'
}
parsePgType(pg_type: string): PythonType {
if (pg_type.startsWith('_')) {
const inner_str = pg_type.slice(1)
const inner = this.parsePgType(inner_str)
return new PythonListType(inner)
} else {
const type_name = this.resolveTypeName(pg_type)
return new PythonSimpleType(type_name)
}
}
typeToClass(type: PostgresType): PythonBaseModel {
const types = Object.values(this.types)
const attributes = type.attributes.map((attribute) => {
const type = types.find((type) => type.id === attribute.type_id)
return {
...attribute,
type,
}
})
const attributeEntries: PythonBaseModelAttr[] = attributes.map((attribute) => {
const type = this.parsePgType(attribute.type!.name)
return new PythonBaseModelAttr(attribute.name, type, false)
})
const schema = this.schemas[type.schema]
return new PythonBaseModel(type.name, schema, attributeEntries)
}
columnsToClassAttrs(table_id: number): PythonBaseModelAttr[] {
const attrs = this.columns[table_id] ?? []
return attrs.map((col) => {
const type = this.parsePgType(col.format)
return new PythonBaseModelAttr(col.name, type, col.is_nullable)
})
}
columnsToDictAttrs(table_id: number, not_required: boolean): PythonTypedDictAttr[] {
const attrs = this.columns[table_id] ?? []
return attrs.map((col) => {
const type = this.parsePgType(col.format)
return new PythonTypedDictAttr(
col.name,
type,
col.is_nullable,
not_required || col.is_nullable || col.is_identity || col.default_value !== null
)
})
}
tableToClass(table: PostgresTable): [PythonBaseModel, PythonTypedDict, PythonTypedDict] {
const schema = this.schemas[table.schema]
const select = new PythonBaseModel(table.name, schema, this.columnsToClassAttrs(table.id))
const insert = new PythonTypedDict(
table.name,
'Insert',
schema,
this.columnsToDictAttrs(table.id, false)
)
const update = new PythonTypedDict(
table.name,
'Update',
schema,
this.columnsToDictAttrs(table.id, true)
)
return [select, insert, update]
}
viewToClass(view: PostgresView): PythonBaseModel {
const attributes = this.columnsToClassAttrs(view.id)
return new PythonBaseModel(view.name, this.schemas[view.schema], attributes)
}
matViewToClass(matview: PostgresMaterializedView): PythonBaseModel {
const attributes = this.columnsToClassAttrs(matview.id)
return new PythonBaseModel(matview.name, this.schemas[matview.schema], attributes)
}
}
class PythonEnum implements Serializable {
name: string
variants: string[]
constructor(type: PostgresType) {
this.name = `${formatForPyClassName(type.schema)}${formatForPyClassName(type.name)}`
this.variants = type.enums
}
serialize(): string {
const variants = this.variants.map((item) => `"${item}"`).join(', ')
return `${this.name}: TypeAlias = Literal[${variants}]`
}
}
type PythonType = PythonListType | PythonSimpleType
class PythonSimpleType implements Serializable {
name: string
constructor(name: string) {
this.name = name
}
serialize(): string {
return this.name
}
}
class PythonListType implements Serializable {
inner: PythonType
constructor(inner: PythonType) {
this.inner = inner
}
serialize(): string {
return `List[${this.inner.serialize()}]`
}
}
class PythonBaseModelAttr implements Serializable {
name: string
pg_name: string
py_type: PythonType
nullable: boolean
constructor(name: string, py_type: PythonType, nullable: boolean) {
this.name = formatForPyAttributeName(name)
this.pg_name = name
this.py_type = py_type
this.nullable = nullable
}
serialize(): string {
const py_type = this.nullable
? `Optional[${this.py_type.serialize()}]`
: this.py_type.serialize()
return ` ${this.name}: ${py_type} = Field(alias="${this.pg_name}")`
}
}
class PythonBaseModel implements Serializable {
name: string
table_name: string
schema: PostgresSchema
class_attributes: PythonBaseModelAttr[]
constructor(name: string, schema: PostgresSchema, class_attributes: PythonBaseModelAttr[]) {
this.schema = schema
this.class_attributes = class_attributes
this.table_name = name
this.name = `${formatForPyClassName(schema.name)}${formatForPyClassName(name)}`
}
serialize(): string {
const attributes =
this.class_attributes.length > 0
? this.class_attributes.map((attr) => attr.serialize()).join('\n')
: ' pass'
return `class ${this.name}(BaseModel, frozen=True):\n${attributes}`
}
}
class PythonTypedDictAttr implements Serializable {
name: string
pg_name: string
py_type: PythonType
nullable: boolean
not_required: boolean
constructor(name: string, py_type: PythonType, nullable: boolean, required: boolean) {
this.name = formatForPyAttributeName(name)
this.pg_name = name
this.py_type = py_type
this.nullable = nullable
this.not_required = required
}
serialize(): string {
const py_type = this.nullable
? `Optional[${this.py_type.serialize()}]`
: this.py_type.serialize()
const annotation = `Annotated[${py_type}, Field(alias="${this.pg_name}")]`
const rhs = this.not_required ? `NotRequired[${annotation}]` : annotation
return ` ${this.name}: ${rhs}`
}
}
class PythonTypedDict implements Serializable {
name: string
table_name: string
parent_class: string
schema: PostgresSchema
dict_attributes: PythonTypedDictAttr[]
operation: 'Insert' | 'Update'
constructor(
name: string,
operation: 'Insert' | 'Update',
schema: PostgresSchema,
dict_attributes: PythonTypedDictAttr[],
parent_class: string = 'BaseModel'
) {
this.schema = schema
this.dict_attributes = dict_attributes
this.table_name = name
this.name = `${formatForPyClassName(schema.name)}${formatForPyClassName(name)}`
this.parent_class = parent_class
this.operation = operation
}
serialize(): string {
const attributes =
this.dict_attributes.length > 0
? this.dict_attributes.map((attr) => attr.serialize()).join('\n')
: ' pass'
return `class ${this.name}${this.operation}(TypedDict):\n${attributes}`
}
}
function concatLines(items: Serializable[]): string {
return items.map((item) => item.serialize()).join('\n\n')
}
const PY_TYPE_MAP: Record<string, string> = {
// Bool
bool: 'bool',
// Numbers
int2: 'int',
int4: 'int',
int8: 'int',
float4: 'float',
float8: 'float',
numeric: 'float',
// Strings
bytea: 'bytes',
bpchar: 'str',
varchar: 'str',
string: 'str',
date: 'datetime.date',
text: 'str',
citext: 'str',
time: 'datetime.time',
timetz: 'datetime.time',
timestamp: 'datetime.datetime',
timestamptz: 'datetime.datetime',
uuid: 'uuid.UUID',
vector: 'list[Any]',
interval: 'str',
// JSON
json: 'Json[Any]',
jsonb: 'Json[Any]',
// Range types (can be adjusted to more complex types if needed)
int4range: 'str',
int4multirange: 'str',
int8range: 'str',
int8multirange: 'str',
numrange: 'str',
nummultirange: 'str',
tsrange: 'str',
tsmultirange: 'str',
tstzrange: 'str',
tstzmultirange: 'str',
daterange: 'str',
datemultirange: 'str',
// Miscellaneous types
void: 'None',
record: 'dict[str, Any]',
} as const
/**
* Converts a Postgres name to PascalCase.
*
* @example
* ```ts
* formatForPyTypeName('pokedex') // Pokedex
* formatForPyTypeName('pokemon_center') // PokemonCenter
* formatForPyTypeName('victory-road') // VictoryRoad
* formatForPyTypeName('pokemon league') // PokemonLeague
* ```
*/
function formatForPyClassName(name: string): string {
return name
.split(/[^a-zA-Z0-9]/)
.map((word) => {
if (word) {
return `${word[0].toUpperCase()}${word.slice(1)}`
} else {
return ''
}
})
.join('')
}
/**
* Converts a Postgres name to snake_case.
*
* @example
* ```ts
* formatForPyTypeName('Pokedex') // pokedex
* formatForPyTypeName('PokemonCenter') // pokemon_enter
* formatForPyTypeName('victory-road') // victory_road
* formatForPyTypeName('pokemon league') // pokemon_league
* ```
*/
function formatForPyAttributeName(name: string): string {
return name
.split(/[^a-zA-Z0-9]+/) // Split on non-alphanumeric characters (like spaces, dashes, etc.)
.map((word) => word.toLowerCase()) // Convert each word to lowercase
.join('_') // Join with underscores
}