Skip to content

Commit 1083973

Browse files
authored
Merge pull request #939 from yilinjuang/master
fix(typescript): Prioritize current schema for pg type generation
2 parents 8ca034c + e880b9f commit 1083973

File tree

1 file changed

+90
-26
lines changed

1 file changed

+90
-26
lines changed

src/server/templates/typescript.ts

+90-26
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export type Database = {
8484
${[
8585
...columnsByTableId[table.id].map(
8686
(column) =>
87-
`${JSON.stringify(column.name)}: ${pgTypeToTsType(column.format, {
87+
`${JSON.stringify(column.name)}: ${pgTypeToTsType(schema, column.format, {
8888
types,
8989
schemas,
9090
tables,
@@ -97,7 +97,12 @@ export type Database = {
9797
const type = types.find(({ id }) => id === fn.return_type_id)
9898
let tsType = 'unknown'
9999
if (type) {
100-
tsType = pgTypeToTsType(type.name, { types, schemas, tables, views })
100+
tsType = pgTypeToTsType(schema, type.name, {
101+
types,
102+
schemas,
103+
tables,
104+
views,
105+
})
101106
}
102107
return `${JSON.stringify(fn.name)}: ${tsType} | null`
103108
}),
@@ -121,7 +126,12 @@ export type Database = {
121126
output += ':'
122127
}
123128
124-
output += pgTypeToTsType(column.format, { types, schemas, tables, views })
129+
output += pgTypeToTsType(schema, column.format, {
130+
types,
131+
schemas,
132+
tables,
133+
views,
134+
})
125135
126136
if (column.is_nullable) {
127137
output += '| null'
@@ -138,7 +148,12 @@ export type Database = {
138148
return `${output}?: never`
139149
}
140150
141-
output += `?: ${pgTypeToTsType(column.format, { types, schemas, tables, views })}`
151+
output += `?: ${pgTypeToTsType(schema, column.format, {
152+
types,
153+
schemas,
154+
tables,
155+
views,
156+
})}`
142157
143158
if (column.is_nullable) {
144159
output += '| null'
@@ -189,7 +204,7 @@ export type Database = {
189204
Row: {
190205
${columnsByTableId[view.id].map(
191206
(column) =>
192-
`${JSON.stringify(column.name)}: ${pgTypeToTsType(column.format, {
207+
`${JSON.stringify(column.name)}: ${pgTypeToTsType(schema, column.format, {
193208
types,
194209
schemas,
195210
tables,
@@ -207,7 +222,12 @@ export type Database = {
207222
return `${output}?: never`
208223
}
209224
210-
output += `?: ${pgTypeToTsType(column.format, { types, schemas, tables, views })} | null`
225+
output += `?: ${pgTypeToTsType(schema, column.format, {
226+
types,
227+
schemas,
228+
tables,
229+
views,
230+
})} | null`
211231
212232
return output
213233
})}
@@ -220,7 +240,12 @@ export type Database = {
220240
return `${output}?: never`
221241
}
222242
223-
output += `?: ${pgTypeToTsType(column.format, { types, schemas, tables, views })} | null`
243+
output += `?: ${pgTypeToTsType(schema, column.format, {
244+
types,
245+
schemas,
246+
tables,
247+
views,
248+
})} | null`
224249
225250
return output
226251
})}
@@ -290,7 +315,12 @@ export type Database = {
290315
const type = types.find(({ id }) => id === type_id)
291316
let tsType = 'unknown'
292317
if (type) {
293-
tsType = pgTypeToTsType(type.name, { types, schemas, tables, views })
318+
tsType = pgTypeToTsType(schema, type.name, {
319+
types,
320+
schemas,
321+
tables,
322+
views,
323+
})
294324
}
295325
return { name, type: tsType, has_default }
296326
})
@@ -307,7 +337,12 @@ export type Database = {
307337
const type = types.find(({ id }) => id === type_id)
308338
let tsType = 'unknown'
309339
if (type) {
310-
tsType = pgTypeToTsType(type.name, { types, schemas, tables, views })
340+
tsType = pgTypeToTsType(schema, type.name, {
341+
types,
342+
schemas,
343+
tables,
344+
views,
345+
})
311346
}
312347
return { name, type: tsType }
313348
})
@@ -327,20 +362,29 @@ export type Database = {
327362
return `{
328363
${columnsByTableId[relation.id].map(
329364
(column) =>
330-
`${JSON.stringify(column.name)}: ${pgTypeToTsType(column.format, {
331-
types,
332-
schemas,
333-
tables,
334-
views,
335-
})} ${column.is_nullable ? '| null' : ''}`
365+
`${JSON.stringify(column.name)}: ${pgTypeToTsType(
366+
schema,
367+
column.format,
368+
{
369+
types,
370+
schemas,
371+
tables,
372+
views,
373+
}
374+
)} ${column.is_nullable ? '| null' : ''}`
336375
)}
337376
}`
338377
}
339378
340379
// Case 3: returns base/array/composite/enum type.
341380
const type = types.find(({ id }) => id === fns[0].return_type_id)
342381
if (type) {
343-
return pgTypeToTsType(type.name, { types, schemas, tables, views })
382+
return pgTypeToTsType(schema, type.name, {
383+
types,
384+
schemas,
385+
tables,
386+
views,
387+
})
344388
}
345389
346390
return 'unknown'
@@ -372,7 +416,12 @@ export type Database = {
372416
const type = types.find(({ id }) => id === type_id)
373417
let tsType = 'unknown'
374418
if (type) {
375-
tsType = `${pgTypeToTsType(type.name, { types, schemas, tables, views })} | null`
419+
tsType = `${pgTypeToTsType(schema, type.name, {
420+
types,
421+
schemas,
422+
tables,
423+
views,
424+
})} | null`
376425
}
377426
return `${JSON.stringify(name)}: ${tsType}`
378427
})}
@@ -519,6 +568,7 @@ export const Constants = {
519568

520569
// TODO: Make this more robust. Currently doesn't handle range types - returns them as unknown.
521570
const pgTypeToTsType = (
571+
schema: PostgresSchema,
522572
pgType: string,
523573
{
524574
types,
@@ -560,10 +610,16 @@ const pgTypeToTsType = (
560610
} else if (pgType === 'record') {
561611
return 'Record<string, unknown>'
562612
} else if (pgType.startsWith('_')) {
563-
return `(${pgTypeToTsType(pgType.substring(1), { types, schemas, tables, views })})[]`
613+
return `(${pgTypeToTsType(schema, pgType.substring(1), {
614+
types,
615+
schemas,
616+
tables,
617+
views,
618+
})})[]`
564619
} else {
565-
const enumType = types.find((type) => type.name === pgType && type.enums.length > 0)
566-
if (enumType) {
620+
const enumTypes = types.filter((type) => type.name === pgType && type.enums.length > 0)
621+
if (enumTypes.length > 0) {
622+
const enumType = enumTypes.find((type) => type.schema === schema.name) || enumTypes[0]
567623
if (schemas.some(({ name }) => name === enumType.schema)) {
568624
return `Database[${JSON.stringify(enumType.schema)}]['Enums'][${JSON.stringify(
569625
enumType.name
@@ -572,8 +628,12 @@ const pgTypeToTsType = (
572628
return enumType.enums.map((variant) => JSON.stringify(variant)).join('|')
573629
}
574630

575-
const compositeType = types.find((type) => type.name === pgType && type.attributes.length > 0)
576-
if (compositeType) {
631+
const compositeTypes = types.filter(
632+
(type) => type.name === pgType && type.attributes.length > 0
633+
)
634+
if (compositeTypes.length > 0) {
635+
const compositeType =
636+
compositeTypes.find((type) => type.schema === schema.name) || compositeTypes[0]
577637
if (schemas.some(({ name }) => name === compositeType.schema)) {
578638
return `Database[${JSON.stringify(
579639
compositeType.schema
@@ -582,8 +642,10 @@ const pgTypeToTsType = (
582642
return 'unknown'
583643
}
584644

585-
const tableRowType = tables.find((table) => table.name === pgType)
586-
if (tableRowType) {
645+
const tableRowTypes = tables.filter((table) => table.name === pgType)
646+
if (tableRowTypes.length > 0) {
647+
const tableRowType =
648+
tableRowTypes.find((type) => type.schema === schema.name) || tableRowTypes[0]
587649
if (schemas.some(({ name }) => name === tableRowType.schema)) {
588650
return `Database[${JSON.stringify(tableRowType.schema)}]['Tables'][${JSON.stringify(
589651
tableRowType.name
@@ -592,8 +654,10 @@ const pgTypeToTsType = (
592654
return 'unknown'
593655
}
594656

595-
const viewRowType = views.find((view) => view.name === pgType)
596-
if (viewRowType) {
657+
const viewRowTypes = views.filter((view) => view.name === pgType)
658+
if (viewRowTypes.length > 0) {
659+
const viewRowType =
660+
viewRowTypes.find((type) => type.schema === schema.name) || viewRowTypes[0]
597661
if (schemas.some(({ name }) => name === viewRowType.schema)) {
598662
return `Database[${JSON.stringify(viewRowType.schema)}]['Views'][${JSON.stringify(
599663
viewRowType.name

0 commit comments

Comments
 (0)